An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements.
Property of Interface:
1)Methods in an interface are implicitly public.
2)An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.
3)Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
Example of interface in Java:
Interface
Java Class
interface DemoInterface extends Serializable{int mobileNo();}
Interface is key to write flexible and maintainable code.
class InterfaceImplementation implements DemoInterface {private int mobileNo;@Overridepublic int mobileNo() {return mobileNo;}}
No comments:
Post a Comment