Search

Tuesday, May 18, 2021

Java interface | Interface in Java

 

Java interface

Java interface. If you want to get knowledge and clear understanding about interface in Java. Then its a best place for you, here i will teach you in detail about Java interface with very easy to understand examples.



What is Java Interface?

In Java language, we have concept of class which is a blue print for the objects but when talk about interface in Java, it is similar to class i.e. it also has a name, body but it is actually different from java class in many ways.

Interface in Java can have methods just like a class has methods but Java classes can have method body, and if we talk about interface, they have just method signature or we can say they have just abstract methods which means, methods will have no body.

Java interfaces can have variables just like a class has variables in its body but Java interfaces contain variables which are public in nature, they are also static and they should also be final.

 

Important points regarding Java interfaces


1.   Java interfaces cannot be instantiated

2.   All methods in Java interfaces should be abstract

3.   Java interfaces does not contain constructor

4.   Any class that uses Java interface must use implements keyword and not the extends because extends keyword is used in case of class (inheritance)

5.   Java interface can extends another interfaces

 

The main usage of Java interface

As we know that in Java language, we cannot perform multiple inheritance because Java does not support multiple inheritance. If we perform inheritance using extends keyword and try to extends two classes at a time, java will give us error because we can only extend single class, more than one classes cannot be extended in Java.

But there is a good thing that we can achieve multiple inheritance using Java interfaces and this is the best use of interface. As all methods in interfaces have no body, they are abstract, and class can use these interfaces and can provide body to interfaces methods. Java classes can also use more than one interfaces at a time.

 

How to declare Java interface?

To define a class in Java, we use class keyword. Similarly, if you want to make interface in Java, we use interface keyword

 

Syntax


Interface Name_Of_Interface

{

//declare java fields here and should be constant

//declare java abstract methods here

}

 

We also need to know that interface is by default abstract in nature and we can skip the abstract keyword when defining interface

Methods inside interfaces are also abstract and we can skip the abstract keyword

 

Example


interface Abc

{

        public void method1();

        public void method2();

}

Above is the interface example, which contains two abstract methods I,e, methods without body.

 

How to use interface

First of all, Java interfaces cannot be instantiated. They are used by the Java classes and Java classes should provide the methods definition that are present inside interfaces. When a class uses an interface, it must provide the definition of the interface’s methods. For this purpose, class uses implements keyword and this keyword occurs in the class declaration portion

 

Syntax


interface Abc

{

//code here

}

public class Xyz implements Abc

{

 

//code here

}

 

Example Program


interface A

{

        public void method1();

        public void method2();

}

public class B implements A

{

//class own code

public void method1()

{

        System.out.println(“Method 1 body”);

}

public void method2()

{

        System.out.println(“Method 2 body”);

}

 

public static void main (String ar[ ] )

{

        B obj = new B();

        obj.method1();

        obj.method2();

}

}


Output will be:


Method 1 body

Method 2 body

 

Program Explanation

In the above code, we have one interface known as A. It has two abstract methods. These methods do not have body. The body to these methods should be provided by the class who uses this interface. So we have a class known as class B and this class is implementing the interface A. This class is providing the body to the methods present inside the interface A. After that we have created object of the class B and calling those two methods and getting the result.

 

Example 2


interface A

{

        public void method1();

}

class B implements A

{

        public void method1()

        {

        System.out.println(“Class B implementation “);

        }

}

class C implements A

{

        public void method1()

        {

        System.out.println(“Class C implementation “);

        }

}

class D

{

        public static void main(String ar[ ] )

        {

        A obj = new C();

        obj.method1();

        }

}

 

Output will be:


Class C implementation

 

Program Explanation

In the above code, we have one interface that has only one abstract method. We have two class who are implementing this interface and providing body to the interface methods according to their own requirements. So we have defined the interface and two classes are providing the body to its method and finally we have a class in which we are actually creating a reference to the interface and calling the method based on our requirement.

 


Using interface to achieve Multiple Inheritance in Java

Whenever a class implements more than one interface, it is known as Multiple inheritance. It cannot be achieved using Java classes.

 

Example Program


 interface A

{

void a();

}

interface B

{

 

void b();

}

class C implements A,B

{

        public void a()

        {

        System.out.println(“First”);

        }

 

        public void b()

        {

        System.out.println(“Second”);

        }

 

        public static void main (String ar[ ])

        {

        C obj = new C();

        obj.a();

        obj.b();

        }

}

 

Output will be:


First

Second

 

Program Explanation

In the above code, we have two interfaces, both have their own abstract methods. These interfaces are implemented by a single class to take advantage of inheritance because multiple inheritance is not possible in Java using classes, but we can achieve it by implementing multiple interfaces.

In the above code, we have a class which is implementing two interfaces and giving body to the interfaces method. Using this technique, ambiguity cannot occur.


 

Interface extending another interface

It is possible that one interface can extends another interface. This is called interface inheritance. We have to use extends keyword here not the implements because, class implements an interface but if one interface is extending another interface, then we have to use extends keyword.

 

Syntax


Interface Interface_Name

{

//code here

}

Interface Interface_Name extends Interface_Name

{

//code here

}

 

Example Program


interface A

{

void a();

}

interface B extends A

{

void b();

}

class Testing implements B

{

        public void a()

        {

        System.out.println(“First”);

        }

        public void b()

        {

        System.out.println(“Second”);

        }

 

        public static void main (String ar[ ])

        {

        Testing obj = new Testing();

        obj.a();

        obj.b();

        }

}


Output will be:


First

Second

 

As in above code, one interface is extending another and Testing  class is implementing interface B, so this class has to provide body to both methods


No comments:

Post a Comment