Login
Register

Home

Trainings

Fusion Blog

EBS Blog

Authors

CONTACT US

Fusion Blog
  • Register

Oracle Gold Partners, our very popular training packages, training schedule is listed here
Designed by Five Star Rated Oracle Press Authors & Oracle ACE's.

webinar new

Search Courses

Objective:

In this previous article Abstract class in Java, we had learned about the abstract methods and classes in java. In this article we will learn about the Interface in detail.

Interface:

Interface is used to provide specifications.

Those specifications must be implemented by the respective class.

Interface is a contract , whenever required we can inject the contract.

when  we inject the contract it is necessary to complete the contract.

We can declare the methods and methods are inherently abstract.

we can declare the variables but we cant change the values. All the interface variables are static and final.

class can implement ‘n’ interfaces.

interface can extend ‘n; interfaces.

if the class is not implementing the abstract methods then calss should be marked with abstract keyword. (we need to declare a classs with abstract class)

An interface in java is a blueprint of a class. It has static constants and abstract methods only.

The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve fully abstraction and multiple inheritance in Java.

In other words, Interface fields are public, static and final bydefault, and methods are public and abstract.

f1.png

In this example, Printable interface have only one method, its implementation is provided in the A class.

 

Extending Interface:

When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. If a class does not perform all the behaviors of the interface, the class must declare itself as abstract. A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.

Example:

f2.png

//Output: Hello

Example:

f3.png

f4.png

f5.png

f6.png

//OUTPUT:

f7.png

Extending multiple Interface:

A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.

Tagging Interface:

The most common use of extending interfaces occurs when the parent interface does not contain any methods. For example, the MouseListener interface in the java.awt.event package extended java.util.EventListener, which is defined as:

package java.util;
public interface EventListener
{

…...

}

An interface with no methods in it is referred to as a tagging interface. There are two basic design purposes of tagging interfaces:

Creates a common parent: As with the EventListener interface, which is extended by dozens of other interfaces in the Java API, you can use a tagging interface to create a common parent among a group of interfaces. For example, when an interface extends EventListener, the JVM knows that this particular interface is going to be used in an event delegation scenario.

Adds a data type to a class: This situation is where the term tagging comes from. A class that implements a tagging interface does not need to define any methods (since the interface does not have any), but the class becomes an interface type through polymorphism.


Varun Kapila

Add comment


Security code
Refresh

About the Author

Varun Kapila

Search Trainings

Fully verifiable testimonials

Apps2Fusion - Event List

<<  Apr 2024  >>
 Mon  Tue  Wed  Thu  Fri  Sat  Sun 
  1  2  3  4  5  6  7
  8  91011121314
15161718192021
22232425262728
2930     

Enquire For Training

Fusion Training Packages

Get Email Updates


Powered by Google FeedBurner