Syntax : interface interface_name { }
Example:
interface Moveable
{
int AVERAGE-SPEED=40;
void move();
}
- Methods inside Interface must not be static, final, native or strictfp.
- All variables declared inside interface are implicitly public static final variables(constants).
- All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract keyword.
- Interface can extend one or more other interface.
- Interface cannot implement a class.
- Interface can be nested inside another interface.
Class
|
Interface
|
In class, you can instantiate variable and create an object.
|
In an interface, you can't instantiate variable and create an object.
|
Class can contain concrete(with implementation) methods
|
The interface cannot contain concrete(with implementation) methods
|
The access specifiers used with classes are private, protected and public.
|
In Interface only one specifier is present i.e., public
|
Abstract class
|
Interface
|
Abstract class is a class which contain one or more abstract methods, which has to be implemented by its sub classes.
|
Interface is a Java Object containing method declaration but no implementation. The classes which implement the Interfaces must provide the method definition for all the methods.
|
Abstract class is a Class prefix with an abstract keyword followed by Class definition.
|
Interface is a pure abstract class which starts with interface keyword.
|
Abstract class can also contain concrete methods.
|
Whereas, Interface contains all abstract methods and final variable declarations.
|
Abstract classes are useful in a situation that Some general methods should be implemented and specialization behavior should be implemented by child classes.
|
Interfaces are useful in a situation that all properties should be implemented.
|
- Use an abstract class when a template needs to be defined for a group of subclasses
- Use an interface when a role needs to be defined for other classes, regardless of the inheritance tree of these classes
- A Java class can implement multiple Java Interfaces. It is necessary that the class must implement all the methods declared in the interfaces.
- Class should override all the abstract methods declared in the interface
- The interface allows sending a message to an object without concerning which classes it belongs.
- Class needs to provide functionality for the methods declared in the interface.
- All methods in an interface are implicitly public and abstract
- An interface cannot be instantiated
- An interface reference can point to objects of its implementing classes
- An interface can extend from one or many interfaces. Class can extend only one class but implement any number of interfaces
- An interface cannot implement another Interface. It has to extend another interface if needed.
- An interface which is declared inside another interface is referred as nested interface
- At the time of declaration, interface variable must be initialized. Otherwise, the compiler will throw an error.
- The class cannot implement two interfaces in java that have methods with same name but different return type.
From Java 9 onwards, interfaces can contain following also
- Static methods
- Private methods
- Private Static methods
- The class which implements the interface needs to provide functionality for the methods declared in the interface
- All methods in an interface are implicitly public and abstract
- An interface cannot be instantiated
- An interface reference can point to objects of its implementing classes
- An interface can extend from one or many interfaces. A class can extend only one class but implement any number of interfaces
No comments:
Post a Comment