In this article, we will discuss the difference between Abstract Class and Interface in Java with examples.I have covered the abstract class and interface in separate tutorials of OOPs Concepts so I would recommend you to read them first, before going though the differences. In one of my interviews, I have been asked to explain the difference between an Interface and an Abstract class. Here's my response: Methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behaviour.
Class and Interface both are used to create new reference types. A class is a collection of fields and methods that operate on fields. An interface has fully abstract methods i.e. methods with nobody. An interface is syntactically similar to the class but there is a major difference between class and interface that is a class can be instantiated, but an interface can never be instantiated. So let us learn some more difference between a class and interface with the help of a comparison chart shown below.
Content: Class Vs Interface
Comparison Chart
Basis for Comparison | Class | Interface |
---|---|---|
Basic | A class is instantiated to create objects. | An interface can never be instantiated as the methods are unable to perform any action on invoking. |
Keyword | class | interface |
Access specifier | The members of a class can be private, public or protected. | The members of an interface are always public. |
Methods | The methods of a class are defined to perform a specific action. | The methods in an interface are purely abstract. |
Implement/Extend | A class can implement any number of interface and can extend only one class. | An interface can extend multiple interfaces but can not implement any interface. |
Constructor | A class can have constructors to initialize the variables. | An interface can never have a constructor as there is hardly any variable to initialize. |
Definition of Class
A class is a most important aspect of Java programming without classes you can’t implement a java program. A class creates reference types and these reference types are used to create objects. A class has a signature and a body. Signature of a class has class’s name and information that tells whether the class has inherited another class. The body of a class has fields and methods that operate on those fields. A Class is created using a keyword class. Lets us see how to declare a class.
When a class is instantiated each object created contains a copy of fields and methods with them. The fields and members declared inside a class can be static or nonstatic. Static members value is constant for each object whereas, the non-static members are initialized by each object differently according to its requirement.
Members of a class have access specifiers that decide the visibility and accessibility of the members to the user or to the subclasses. The access specifiers are public, private and protected. A class can be inherited by an another class using the access specifier which will decide the visibility of members of a superclass (inherited class) in a subclass (inheriting class). Thus class fulfills the concept of data hiding and encapsulation in Object Oriented programming.
Definition of Interface
An interface is also among the reference types defined in Java. The importance of an interface is that in Java, a class can only inherit a single class. To avoid this restriction, the designers of Java introduced a concept of interface. An interface is syntactically similar to a class, but it lacks in field declaration and the methods inside an interface do not have any implementation. An interface is declared using a keyword interface. Let us view the declaration of an interface.
An interface does not define any method declared inside it because it lacks in fields to operate on. Just because any method inside an interface does not perform any action, an interface can never be instantiated. If an interface has any field member, it must be initialized at the time of their declaration. An interface never contains any constructors as it lacks in field members to get initialize. So, an interface only defines what a class must do instead of how it must do.
An interface once created can be implemented by any number of classes using a keyword implements. But the classes implementing an interface must define all the methods inside an interface. An interface can also inherit another interface using extend keyword. If a class implements an interface that extends an another interface. Then a class must define the methods of both the interfaces appearing in a chain. The methods inside an interface must always be public as they have to be accessed by the classes implementing them.
Key Differences Between Class and Interface in Java
- A class can be instantiated by creating its objects. An interface is never instantiated as the methods declared inside an interface are abstract and does not perform any action, so there is no use of instantiating any interface.
- A class is declared using a keyword class. In the same way, an interface is created using a keyword interface.
- The members of a class can have the access specifier like public, private, protected. But the members of an interface are always public as they have to be accessed by the classes implementing them.
- The methods inside a class are defined to perform an action on the fields declared in the class. As interface lacks in the declaration of fields, the methods inside an interface are purely abstract.
- A class can implement any number of interfaces but can extend only one super class. An interface can extend any number of interfaces but cannot implement any interface.
- A class has constructors defined inside it to get the variable initialized. But, an interface does not have any constructors as there are no fields to be initialized. The fields of an interface are initialized at the time of their declaration only.
Conclusion:
Both classes and interfaces have their own importance. A class is used when we need to define how the task would be done. An interface is used when we need to know what task has to be done.
Related Differences:
Prerequisite – Interface, Abstract Class
Abstraction: Hiding the internal implementation of the feature and only showing the functionality to the users. i.e. what it works (showing), how it works (hiding). Both abstract class and interface are used for abstraction.
Abstract class vs Interface
- Type of methods: Interface can have only abstract methods. Abstract class can have abstract and non-abstract methods. From Java 8, it can have default and static methods also.
- Final Variables: Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.
- Type of variables: Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
- Implementation: Abstract class can provide the implementation of interface. Interface can’t provide the implementation of abstract class.
- Inheritance vs Abstraction: A Java interface can be implemented using keyword “implements” and abstract class can be extended using keyword “extends”.
- Multiple implementation: An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
- Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.
// concept of abstract class import java.io.*; // abstract class { String objectName = ' ' ; Shape(String name) this .objectName = name; // it has default implementation { System.out.println( this .objectName + ' ' + 'has been moved to' } // abstract methods which will be abstract public double area(); } class Rectangle extends Shape Rectangle( int length, int width, String name) this .length = length; } @Override { } @Override { } { double pi = 3.14 ; Circle( int radius, String name) this .radius = radius; public void draw() } @Override { } { { // creating the Object of Rectangle class Shape rect = new Rectangle( 2 , 3 , 'Rectangle' ); System.out.println( 'Area of rectangle: ' + rect.area()); Shape circle = new Circle( 2 , 'Cicle' ); System.out.println( 'Area of circle: ' + circle.area()); } |
Output:
In you don’t have any common code between rectangle and circle then go with interface.
See this…..
// concept of interface { void draw(); } class Rectangle implements Shape int length, width; // constructor { this .width = width; public void draw() System.out.println( 'Rectangle has been drawn ' ); public double area() return ( double )(length*width); } class Circle implements Shape int radius; //constructor { this .radius = radius; public void draw() System.out.println( 'Circle has been drawn ' ); public double area() } } class GFG public static void main (String[] args) // and using shape interface reference. System.out.println( 'Area of rectangle: ' + rect.area()); // creating the Objects of circle class System.out.println( 'Area of circle: ' + circle.area()); } |
output
When to use what?
Consider using abstract classes if any of these statements apply to your situation:
- In java application, there are some related classes that need to share some lines of code then you can put these lines of code within abstract class and this abstract class should be extended by all these related classes.
- You can define non-static or non-final field(s) in abstract class, so that via a method you can access and modify the state of Object to which they belong.
- You can expect that the classes that extend an abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
Consider using interfaces if any of these statements apply to your situation:
- It is total abstraction, All methods declared within an interface must be implemented by the class(es) that implements this interface.
- A class can implement more than one interface. It is called multiple inheritance.
- You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
Quiz
This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.