Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

In Java, what are the differences between Interface and Abstract class?

user-image
Question added by Salauddin Mohammad , Sr. Manager, Software Development , Aspen Technology Inc
Date Posted: 2014/08/27
Deleted user
by Deleted user

Interface will contain only method declarations. But abstract class may contain method implementation as well as method declaration(abstract methods).

If all the child class having the same implementation of method, we should go with abstract. In case methods are same and different implementation means, we should go with interface

Alabbas Alhaj Ali
by Alabbas Alhaj Ali , Senior Frontend Developer , Acomodeo

An interface differs from an abstract class because an interface is not a class. An interface is essentially a type that can be satisfied by any class that implements the interface.

Any class that implements an interface must satisfy2 conditions:

  • It must have the phrase "implements Interface_Name" at the beginning of the class definiton.
  • It must implement all of the method headings listed in the interface definition.

This is what an interface called "Dog" would look like:

public interface Dog { public boolean Barks(); public boolean isGoldenRetriever(); }

Now, if a class were to implement this interface, this is what it would look like:

public class SomeClass implements Dog { public boolean Barks{ // method definition here } public boolean isGoldenRetriever{ // method definition here } }

Now that we know the basics of interfaces and abstract classes, let’s get to the heart of the question and explore the differences between the two. Here are the three major differences:

abstcract class is pratially implemented class .it coantain all method.

interface is full unimpelementd class.abstract is100% contain abstract method.

S Thirumalai Venkatesan
by S Thirumalai Venkatesan , Technical Lead , Fatpipe Networks India Ltd

Interface is a complete skeleton  whereas abstract classes will have some methods(object behaviour) implemented. 

 

pardeep kumar
by pardeep kumar , Senior R & D Engineer , Mavenir Systems

Just to update you guys. An interface can also provide default implementation in java8 even static method also.

Noha Nabil
by Noha Nabil , Technical Team Lead , DPS

Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.

Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.

Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..

Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.

An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.

A Java class can implement multiple interfaces but it can extend only one abstract class.

Mohd shahnawaz khan
by Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution

Abstract classes can have consts, members, method stubs and defined methods, whereas interfaces can only have consts and methods stubs.

Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public.

When inheriting an abstract class, a concrete child class must define the abstract methods, whereas an an abstract class can extend another abstract class and abstract methods from the parent class don't have to be defined.

Similarly, an interface extending another interface is not responsible for implementing methods from the parent interface. This is because interfaces cannot define any implementation.

A child class can only extend a single abstract (or any other) class, whereas an interface can extend or a class can implement multiple other interfaces.

A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility.

Shefaa Masadeh
by Shefaa Masadeh , Oracle Developer , Talal Abu Ghazaleh Organization

the abstract class contains one or more abstract method, but the interface all of its methods are abstract. 

More Questions Like This