Search This Blog

Abstract & Interface

Abstract 

An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

        abstract void moveTo(double deltaX, double deltaY);

If any class have any abstract method the class itself should be declared as abstract. Generally the class which extends abstract class provide implementation for all the abstract methods of abstract class, If it fails to provide implementation for all the object it must be declared as abstract as well.

Abstract class can have abstract as well as simple java methods(with implementation).Abstract declarations behave like rules to be followed (developer have to provide implementation for them)  and already implemented methods behave as guidelines which can be followed as it is or can be overridden with your choice of implementation(as long as its not declared as private or final).

Abstract Class can have non-final variables.
Abstract Class can have usual flavor of class members, e.g. private, protected.

Interface

Interfaces are contract.
An interface is a description of the behavior an implementing class will have. The implementing class ensures, that it will have these methods that can be used on it. It is basically a contract or a promise the class has to make.
Interfaces are rules because the implementing classes have to give the implementation without fail to each methods define in interface.Interface face declares what needs to be done but how it will be done is totally dependent upon developer.

Variables declared in interface is by default Final.
Members of interface are by default public.

No comments:

Post a Comment