The Best Technical Blog Ever

twitterrss

Interface in Java

In java, interface provides a way to achieve complete abstraction. It is an abstract type that is used to specify a behavior that a class must implement. In simple words, it is a blueprint of a class. Interfaces are declared using the interface keyword, and may only contain static constant declarations and abstract methods.


The variables declared in an interface are public, static & final by default. so we don't need to provide access modifiers to these fields. All methods (implicitly public & abstract ) of an interface do not contain implementation (method bodies). Here, we don't need to provide the abstract keyword. Interface rules has been changed in Java 8. After Java 8, Interface definition may have implementation(method bodies) for default and static methods. Since methods in interfaces do not have method body, they have to be implemented by the class before accessing them. The class that implements interface must implement all the methods of that interface unless it's an abstract class.


Declaring Interfaces:

The interface keyword is used to declare an interface.


Syntax:
access_modifier interface InterfaceName [extends Other_Interfaces] {
       constant declarations
       abstract method declarations
}	
Example:
interface

Compiler automatically converts the methods of Interface as public and abstract, and the data members as public, static and final by default.


Interface implementation:

In java, a class uses the implements keyword to implement an interface. A Class can implement more than one Interface while it can extend only one Class. Similarly, interface can extend multiple Interfaces. But it can't extend any class.


Syntax:
public class InterfaceDemo implements InterfaceName {
 	.....
}				

Example:
interface

interface

interface

interface

interface


Like an abstract class, an interface cannot be instantiated. It can't have any constructors. Interface is used to achieve fully abstraction and multiple inheritance in Java.


Interface extends other Interface:

Interfaces have the ability to extend other interfaces in the same way that a class can extend another class. For that, extends keyword is used. When extending an interface, the child interface can inherit the methods of the parent interface.


Example:
interface

interface

interface

interface

interface


A class that implements an interface must implement all the method declared in that interface. If that interface extends from other interfaces, then the implementing class must also implement the methods in the interfaces that are being extended from. For example, in the above program we have a Dogs class that implements the Mammals interface which already extends Animals interface, then that class must have definitions for all the method declared in both the Mammals interface and the Animals interface.


In java, an interface can extend several other interfaces. For example,


Example:
public interface Dogs extends Mammals, Pet{
	.......
}				

Interfaces supports Multiple Inheritance:

In Java, class can only extend one parent class. So multiple inheritance is not supported through class in java. However it is achieved by interface. Since in java, a class can implement multiple interfaces and also an interface can extend multiple interfaces. That is called as multiple inheritance.


Example:
interface

interface

interface

interface


Tag or Marker interface:

An empty interface is known as tag or marker interface. For example Serializable, Cloneable, Remote, ThreadSafe... are tag interfaces. These interfaces do not have any fields and methods in it. Tag interface is used to indicate something to JVM that the class implementing any of these would have some special behavior.


Example:
public interface Serializable{  

}

Nested interfaces:

Declaring an interface inside of another interface is known as nested interface/ inner interface.


Example:
interface interface_name1{  
	void disp1();  
	interface interface_name2{  
	    void disp2();  
 	}  
}  

Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status