Interface :
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
The interface in java is a blueprint of a class. It has static constants and abstract methods only.
The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface.
The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members.
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
The interface in java is a blueprint of a class. It has static constants and abstract methods only.
The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface.
The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members.
An interface is different from a class in several ways, including:
- You cannot instantiate an interface.
- An interface does not contain any constructors.
- All of the methods in an interface are abstract.
- An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
- An interface is not extended by a class; it is implemented by a class.
- An interface can extend multiple interfaces.
Class and Interface:
- Class can Extend another Class
- Class can Implement Interface
- Interface can Extend another Interface
Multiple inheritance in Java by interface:
If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.
A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.
The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.
For example, if the Cricket interface extended both Sports and Event, it would be declared as:
The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.
For example, if the Cricket interface extended both Sports and Event, it would be declared as:
public interface Hockey extends Sports, Event
- Class1 implements Interface 1 and Interface 2 or
- Interface1 extends Interface2 and Interface3
Marker or tagged interface:
An interface that have no member is known as marker or tagged interface. For example: Serializable, Cloneable, Remote etc. They are used to provide some essential information to the JVM so that JVM may perform some useful operation.
No comments:
Post a Comment