Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.
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
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:
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.
Interface is a complete skeleton whereas abstract classes will have some methods(object behaviour) implemented.
Just to update you guys. An interface can also provide default implementation in java8 even static method also.
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.
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.
the abstract class contains one or more abstract method, but the interface all of its methods are abstract.