Java Interfaces
Why we need Java Interfaces?
1. As an alternative to multiple inheritance.
2. Allows complete loosely coupled architecture -- by allowing separation between specifications (WHAT) & implementation(HOW)
Why use Java interface?
There are mainly three reasons to use interface. They are given below.
1. It is used to achieve fully abstraction.
2. By interface, we can support the functionality of multiple inheritance.
3. It can be used to achieve loose coupling.
Meaning of Interface in Java
: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.
Java Interface also represents IS-A relationship.
It cannot be instantiated just like abstract class.
The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members.
Syntax for declaring java Interfaces:
default/public interface NameOfInterface extends comma separated list of super Interfaces
{
// Data members --- public static final -- implicitely
//Method members --- public abstract --- implicitely
}
syntax for implementation class for java Interfaces :
default/public class NameOfClass extends SuperclsName implements comma separated list of interfaces
{
//which D.M can be accessed directly ? -- i/f constants
//mandatory requirements for non-abstract class - MUST implement(define i.e method def.) all methods from all i/fs.
}
Example:
i/f -- Computable --- area & peri of the bounded shapes
---members - PI, calc functionality declarations.
Point class ---- x,y co-ords , costr, toString
Circle --- x,y,rad , toString,calcArea,calcPeri
Rect --- x,y,w,h,toString ,calcArea,calcPeri
TestIntf --- Circle inst, Rect inst -- Initially using direct referencing.
Dynamic method dispatch via interfaces:
Interfaces can't be instantiated BUT can be used as a reference.
I/f reference can directly (without type casting) refer to implementation class instance & can access functionality declared within i/f.
------------------------
Enums --essentially enumerated constants BUT more powerful.
The very basic purpose of enums is to enforce compile time type safety.
Enumeration in java is supported by keyword enum. enums are a special type of class that always extends java.lang.Enum.
eg
public enum DIRECTION {
EAST,
WEST,
NORTH,
SOUTH; //optionally can end with ";"
}
Some important points about Enums:
1. Introduced in JDK 1.5
2. Declared similar to class or i/f -- actually contain features from both.
3. Basically helps to define constants
4. Adds type safety to constants.
5. Before enums were introduced
Interfaces were typically used to define constants.
Relationship between classes and interfaces
A class extends another class, an interface extends another interface but a class implements an interface.
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.
Multiple inheritance in java
interface Printable{
void print();
}
interface Showable{
void show();
}
class A implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
A7 obj = new A7();
obj.print();
obj.show();
}
}
Question
Multiple inheritance is not supported through class in java but it is possible by interface, why?
Multiple inheritance is not supported in case of class, isnce it can create an ambiguity. But it is supported in case of interface because there is no ambiguity as implementation is provided by the implementation class.
For example:
interface Printable{
void print();
}
interface Showable{
void print();
}
class TestTnterface1 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
TestTnterface1 obj = new TestTnterface1();
obj.print();
}
}
As you can see in the above example, Printable and Showable interface have same methods but its implementation is provided by class TestTnterface1, so there is no ambiguity.
Interface inheritance
A class implements interface but one interface extends another interface .
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class Testinterface2 implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
Testinterface2 obj = new Testinterface2();
obj.print();
obj.show();
}
}
Q) What is marker or tagged interface?
An interface that have no member is known as marker or tagged interface. For example: Serializable, Cloneable, Remote etc. They are used to provide some essential information to the JVM(Run time marker) so that JVM may perform some useful operation.
//How Serializable interface is written?
public interface Serializable{
}
Nested Interface in Java
Note: An interface can have another interface i.e. known as nested interface.
eg :
interface printable{
void print();
interface MessagePrintable{
void msg();
}
}
Sign up here with your email
1 comments:
Write commentsIt is really good explaination
ReplyConversionConversion EmoticonEmoticon