Programming





Interface in Java

Interface in Java


by Administrator | 05-Apr-2020
Java

Tags:  Interface  Core Java  Marker Interface  Difference

Share: 


Interface is defined as a blueprint of class. Interface has static variables and abstract methods. It can be used to achieve fully abstraction and multiple inheritance. 

  1. Interfaces are also called as 100% abstract class.
  2. An interface is defined using interface keyword.
  3. Interfaces are used to achieve complete abstraction.
  4. By default, interfaces are public and abstract. However, you can still use public and abstract modifier with an interface.
  5. All the methods of an interface are public and abstract. final or static modifier can’t be used with the methods.
  6. By default the access modifier for the members of an interface is public. However, you can use public access modifier explicitly. The members of interface cannot have any other modifier than public.
  7. By default, all the variables are constants and are public, static and final. You can still mark them as public, static and final.
  8. Constructors and blocks cannot be defined in an interface.
  9. Inner classes cannot be defined in an interface.
  10. Interfaces cannot extend any class.
  11. Interfaces can extend multiple interfaces.
  12. A class can implement an interface using the implements keyword.
  13. A class can implement any number of interfaces.
  14. An interface cannot implement other interfaces.
  15. The first class implementing an interface must override all the methods.
  16. The static members of an interface can be accessed with the interface’s name using a dot operator.
  17. An interface without any member is known as marker/empty interface. This type of interface is used to mark the class to provide some special instructions to the JVM. 

Program

interface Animal {
    int count = 10;
    public void sound();
    public void run();
}
class Dog implements Animal {
    public void sound() {
        System.out.println("Dogs barking.");
    } 
    public void run() {
        System.out.println("Dogs running.");
    }
}
public class TestInterface {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.sound();
        dog.run();
        System.out.println(Dog.count);
    }
}

Output

Dogs barking.
Dogs running.
10

What is a Marker Interface?

An interface that has no data member or method is known as a marker interface. A marker interface is completely empty. They are used to provide special information/instructions to the JVM. Examples of Marker Interfaces are:

  1. java.io.Serializable
  2. java.lang.Cloneable
  3. java.util.EventListener
  4. java.util.RandomAccess
  5. java.rmi.Remote

Difference between an Abstract class and Interface 

Abstract Class

Interface

An abstract class can have both abstract and non-abstract/concrete methods

An Interface can only have abstract methods

An abstract class can have members with various access levels like private, protected, default etc

An interface must have all the members as public

Abstract class has constructer

Interfaces don’t have a constructer

Abstract class can have instance variable

Interface can’t have an instance variable

Abstract class can have variables with different types of modifier

Interface can have variable only with public static final modifier

An abstract class can have static methods

Interface cannot have static methods

You extend an abstract class

You implement an interface

A class can extend only one abstract class

A class can implement multiple interfaces

A Java abstract class should be extended using keyword extends

Java interface should be implemented using keyword implements

An abstract class can extend another Java class and implement multiple interfaces

An interface can extend only interfaces

An abstract class cannot be instantiated, but can be invoked if a main() exists

Interfaces are 100% abstract and cannot be instantiated


Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.