Recent Posts

header ads

Interface in java with examples - interface programs । KpTechSolution

➧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.
Interfaces in Java in hindi-interface in java with examples - interface क्या है हिंन्दी में जानें ।
Interfaces in Java Language
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 :
  1. interface Interface_name
  2. {
    Return_type Method_name ( ) { }
    }

In the interfaces, variables can be initialized, while reading these variables, the reader reads Compiler by-default "public static final" with keywords.

For example :

//InterDemo.java
  1. interface InterDemo
    {
    int x = 100;
    void show ( )
    }
//InterDemo.java
  1. //InterDemo.class
    interface InterDemo
    {
    public static final int x = 100;
    public abstract void show ( )
    }

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 :

  1. Interfaces are like a class.
  2. No object can be created from the interface.
  3. No constructor can be created in the interface.
  4. In the Interface and Class, the Interface is implemented only through the "implements" Keyword.
  5. Interface and Interface can be Inherite by the "extends" keyword.
  6. Only reference variables can be created for the interface.
  7. It is mandatory to define the BODY of the methods implemented in a class.
  8. In Compile Compiling_time, the "Public static final" keyword is added to the initialized variables in the interface.
  9. Body of any Methods Declared in the Interface can not be left without Define.
  10. Interface By-default is "public".
  11. Methods are "abstract" in the interface.
  12. 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 :
  1. public interface Red{
    public void showr();
    }
    public interface  Black{
    public void showb();
    }
    public interface Yello extends Black, Red{
    public void showy();
    }
    public class Color
    {
    public void showr( ){
    System.out.println( “ i am Red interface “ );
    }
    public void showb( ){
    System.out.println( “ i am Black interface “ );
    }
    public void showy( ){
    System.out.println( “ i am Yellow interface “ );
    }
    public static void main (String …k )
    {
    Color c = new Color ( ) ;
    c.showr( ) ;
    c.showb( );
    c.showy( ) ;
    }
    }
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 :
  1. public interface Red{
    public void showr();
    }
    public interface  Black{
    public void showb();
    }
    public class Color implements Black, Red
    {
    public void showr( ){
    System.out.println( “ i am Red interface “ );
    }
    public void showb( ){
    System.out.println( “ i am Black interface “ );
    }
    public static void main (String …k )
    {
    Color c = new Color ( ) ;
    c.showr( ) ;
    c.showb( );
    }
    }
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 :
  1. interface Red{
    void show();
    }
    class Color implements Red
    {
    public void show( ){
    System.out.println( “ i am Red interface “ );
    }
    public static void main (String …k )
    {
    Red r = new Color ( ) ;
    r.show( ) ;
    }
    }
OUTPUT :  i am Red interface

Also Read :
  • abstract class in java with examples and program.
  *   Use of “extends” & “implements” Keywords :
  1. The "extends" keyword is used to inherit the interface in the interface.
  2. "Implements" Keyword is used to implement Interface in class.

Source Code :
  1. interface Red{
    void showr();
    }
    interface Black extends Red{
    void showb();
    }
    class Color implements Black
    {
    public void showr( ){
    System.out.println( “ i am Red interface “ );
    }
    public void showb( ){
    System.out.println( “ i am Black interface “ );
    }
    public static void main (String …k )
    {
    Color r = new Color ( ) ;
    r.showr( ) ;
    r.showb( );
    }
    }
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 

Post a Comment

0 Comments