➧What is Interfaces - Introduction :
"Interface" is also like a class. All the methods created by Interfaces are "abstract" by-default. This means that in the interface only the Declare body of the method has to be define it can not be defined.
Inside the Interface, we can define the body of the method created in any class. Interface is implemented only by "implements" keyword in a class. We can not "extends" to the interface with a class. The object of the interfaces can not be created but it can be used to make the reference variable.
"Interface" does not do any work itself, the Task that the Interface has to do is perform that task through the class. To understand the interface, we take the example of a company. Suppose the company's Boss is an Interface in a company and the Empoloyee Class is working in that company. Boss does not do the work of the company himself, for this, he keeps the Empoloyee in the company and makes the company work through the same Empoloyee. Similarly, in the case of Interface, this is the same.
Syntax of interface :
For example :
//InterDemo.java
//InterDemo.java
Multiple inheritance can also be done in the case of the interface. An Interface can "extend" many interfaces and one class can "implement" many different interfaces. For this, we have to use comma (,).
Property Of Interface :
- Interfaces are like a class.
- No object can be created from the interface.
- No constructor can be created in the interface.
- In the Interface and Class, the Interface is implemented only through the "implements" Keyword.
- Interface and Interface can be Inherite by the "extends" keyword.
- Only reference variables can be created for the interface.
- It is mandatory to define the BODY of the methods implemented in a class.
- In Compile Compiling_time, the "Public static final" keyword is added to the initialized variables in the interface.
- Body of any Methods Declared in the Interface can not be left without Define.
- Interface By-default is "public".
- Methods are "abstract" in the interface.
- The variables created in the interface are constants.
Also Read :
- What is class in java with examples and programs.
* Multiple inheritance in interface-to-interface :
Multiple inheritance can be done with the interface of the interface. For this, we have to use the "extends" keyword and we have to arrange the names of interfaces using the work (,).
Source Code :
OUTPUT : i am Red interface
i am Black interface
i am Yello interface
* Multiple inheritance in interface-to-class :
Multiple inheritance can be done with the class of Interface. For this, we have to use the "implements" keyword and we have to arrange the names of the interfaces using the work (,).
Source Code :
OUTPUT : i am Red interface
i am Black interface
* Creating Reference Variable of Interface:
Interface is also similar to an abstract class. An object can not be created by any object, but its reference can be made as variable.
Source Code :
OUTPUT : i am Red interface
Also Read :
- abstract class in java with examples and program.
* Use of “extends” & “implements” Keywords :
- The "extends" keyword is used to inherit the interface in the interface.
- "Implements" Keyword is used to implement Interface in class.
Source Code :
OUTPUT : i am Red interface
i am Black interface
Read Also :
- What is class in java with examples easy explain.
- abstract class in java with example and examples.
Like this post, If you find Topic useful in this post, so share it with your friends and colleagues on Facebook, Twitter, whatsapp and Google Plus.
Thank You !
Note – For More Core & Advance Java Concepts , Tutorials And Programs Are Available in www.kptechsolution.com
0 Comments