Abstract | Interface | |
---|---|---|
method | abstract and concrete | abstract only, support default and static method |
multiple inheritance | no (extend one class) | yes (extends multiple interfaces) |
variables | final, non-final, static and non-static default: declared as public, static, final |
static, final |
implementation | provides interface implementation | doesn't provide abstract class implementation |
constructor | allowed | not allowed |
keyword | abstract (implicit: public, abstract) | interface |
inheritance | implements multiple interfaces but can extend one class |
extends multiple interfaces |
Showing posts with label interface. Show all posts
Showing posts with label interface. Show all posts
Sunday, April 9, 2017
Abstraction vs Interface
Interface
An interface in Java is blueprint of a class. It can achieve abstraction and provides multiple inheritances.
Interface is similar to abstraction class but interface is purely abstract. So it is blueprint of a class instead of object instantiation. Below illustrate interface characteristics in Java.
Interface
- Interface can have only abstract methods. Since Java 8, it can have default and static methods also
- Interface supports multiple inheritance (An interface can extend multiple interfaces)
- Interface has only static and final variables.
[By default variables are declared as public, static and final]
- Interface can't provide the implementation of abstract class
- Constructor is not allowed in Interface
[There is only static fields in interface that doesn't need to be initialized during object creation in subclass and the method of interface has to provide actual implementation in subclass. So there is no need of constructor in interface]
[During the object creation of subclass, the parent constructor is called. But if there will be more than one interface implemented then a conflict will occur during call of interface constructor as to which interface's constructor will call first] - The interface keyword is used to declare interface
[By default interface is implicitly public and abstract]
Subscribe to:
Posts (Atom)
Abstraction vs Interface
Abstract Interface method abstract and concrete abstract only, support default and static method ...
-
Single inheritance is the most simple form among five types of inheritance. It requires one superclass and one subclass. Examples: Dog I...
-
In this section we will talk about the argument lists in method overloading. Method overloading can be achieved if method signature is diff...