Interface & Its Advantage:-
1. If all the methods in the class is abstract then the class is nothing but the interface.
2. We cannot create an object of interface.
3. To implement the interface by child class keyword “implements” is used.
4. Interface is defined by using keyword interface.
5. If a class implements interface but does not implements methods, then the class is abstract class.
6. We can assign the object of class which implements the interface of type of that interface.
interface Duck { Void DuckType(); Void DuckProperty(); } class BlackDuck implements Duck { Public void DuckType() { System.out.println("Black Duck"); } Public void DuckProperty() { System.out.println("Swim, Quack and Fly"); } } class TestDuck { Public static void main(String args[]) { BlackDuck obj=new BlackDuck(); Obj.DuckType(); Obj.DuckProperty(); } }
Advantages of Interface:-
- Runtime Polymorphism
- Contract can be used throughout the application
To know more about inheritance and abstract classes, refer this article.
Leave a Reply