Interface in Java
- Interface
- Example of Interface
- Multiple inheritance by
Interface
- Why multiple inheritance
is supported in Interface while it is not supported in case of class.
- Marker Interface
- Nested Interface
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.
Why use Java interface?
There are mainly three reasons
to use interface. They are given below.
- It
is used to achieve fully abstraction.
- By
interface, we can support the functionality of multiple inheritance.
- It
can be used to achieve loose coupling.
The java
compiler adds public and abstract keywords before the interface method and
public, static and final keywords before data members.
In other words, Interface
fields are public, static and final bydefault, and methods are public and
abstract.
Understanding relationship between classes and interfaces
As shown in the figure given
below, a class extends another class, an interface extends another interface
but a class implements an
interface.
Simple example of Java interface
In
this example, Printable interface have only one method, its implementation is
provided in the A class.
|
1. interface printable{
2. void print();
3. }
4.
5. class A6 implements printable{
6. public void print(){System.out.println("Hello");}
7.
8. public static void main(String args[]){
9. A6 obj = new A6();
10. obj.print();
11. }
12. }
Output:Hello
Multiple inheritance in Java by interface
If a class implements multiple
interfaces, or an interface extends multiple interfaces i.e. known as multiple
inheritance.
1. interface Printable{
2. void print();
3. }
4.
5. interface Showable{
6. void show();
7. }
8.
9. class A7 implements Printable,Showable{
10.
11. public void print(){System.out.println("Hello");}
12. public void show(){System.out.println("Welcome");}
13.
14. public static void main(String args[]){
15. A7 obj = new A7();
16. obj.print();
17. obj.show();
18. }
19. }
Output:Hello
Welcome
Q)
Multiple inheritance is not supported through class in java but it is possible
by interface, why?
As we
have explained in the inheritance chapter, multiple inheritance is not
supported in case of class. But it is supported in case of interface because
there is no ambiguity as implementation is provided by the implementation
class. For example:
|
1. interface Printable{
2. void print();
3. }
4.
5. interface Showable{
6. void print();
7. }
8.
9. class testinterface1 implements Printable,Showable{
10.
11. public void print(){System.out.println("Hello");}
12.
13. public static void main(String args[]){
14. testinterface1 obj = new testinterface1();
15. obj.print();
16. }
17. }
Hello
As you can see in the above
example, Printable and Showable interface have same methods but its
implementation is provided by class A, so there is no ambiguity.
Interface inheritance
A class implements interface
but one interface extends another interface .
1. interface Printable{
2. void print();
3. }
4. interface Showable extends Printable{
5. void show();
6. }
7. class Testinterface2 implements Showable{
8.
9. public void print(){System.out.println("Hello");}
10. public void show(){System.out.println("Welcome");}
11.
12. public static void main(String args[]){
13. Testinterface2 obj = new Testinterface2();
14. obj.print();
15. obj.show();
16. }
17. }
Hello
Welcome
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 so that JVM may perform some useful operation.
1. //How Serializable interface is written?
2. public interface Serializable{
3. }
Nested Interface in Java
Note: An interface can have
another interface i.e. known as nested interface. We will learn it in detail in
the nested classes chapter. For example:
1. interface printable{
2. void print();
3. interface MessagePrintable{
4. void msg();
5. }
6. }
More about Nested Interface
No comments:
Post a Comment