What is interface in java-vishmy1.blogspot.com

Interface in JAVA

Interface is defined as a set of data members and member functions as of a class, but in most common forms, an interface is a set of related methods with an empty body.

Interface looks like class, but it is not a class.

An interface can have methods and variables just like class, but the method declared inside the interface will act as an abstract method.

We can also say that an interface is a set of abstract methods.

The interface keyword is used to declare an interface.

Interface is also called as blueprint of a class

Syntax:-
interface interface_name
{
    
     abstract  method();
}

Implementing interface in Java:-

When a class implements an interface you can get a class assigning a contract to perform the specific behavior of the interface.

A class uses the implement keyword to implement an interface.

The implement keyword appears in a class declaration following the extended portion of a declaration.



Example of interface in Java:-

interface A
{
     public void show();
    public void dispaly(int a);
}

class B implements A
{
    public void show()
    {
        System.out.println("Abstact method called");
    }
    public void display(int a)
    {
         System.out.println("value of A is:"+a);
    }
}
class InterfaceExample
{
    public static void main(String args[])
    {
         B b1 = new B();
         b1.show();
         b1.display(2);
    }
}

Implementing multiple Interface into a class:-

It is also possible that a class implements multiple interfaces at a time. In such a case, all the methods of all the interfaces must be defined in that class.

Example:-

interface A
{
   public void set();
   public void get();
}
interface B
{
  public void put();
  public void check();
}
class C implements A,B
{
      public void set()
    {
        System.out.println("this is method set from A");
   }
    public void get()
    {
        System.out.println("this is method get from A");
   }
    public void put()
    {
        System.out.println("this is method put from B");
   }
    public void check()
    {
        System.out.println("this is method check from B");
   }
     public void demo()
    {
        System.out.println("this is method set from class C");
   }

}

class MultiInterface
{
     public static void main(String args[])
     {
        C c1 = new C();
        System.out.println("calling from main method");
       c1.set();
      c1.get();
      c1.put();
      c1.check();
     c1.demo();
  }
}

Note:- Here we can see that all the methods of interface A and B are defined is implemented in class C along with its methods and we create objects of class C to call all the defined methods.

difference between class and interface:-

class

  • In a class, we can only define member functions, we can declare them.
  • In a class, the data members are by default variable and non-static.(without applying any keyword).
  • In a class, static member functions are allowed to define.
  • We can create objects of a class.
  • We can define constructors in class.
  • A class cannot extend multiple classes.
Interface
  • In an interface, we can only declare member functions, we cannot define them.
  • In an interface, the data members are by default final and static.(without applying any keyword).
  • Interface does not allow static member functions.
  • We cannot create objects of any interface. We can create its references only.
  • We cannot define any constructor in an interface.
  • An interface can extend multiple interfaces.
Interface extending another interface:-

An interface can also extend another interface, similar to the way that a class extends another class and inherits the properties of base class into the derived class.

Similarly, the keyword extends is used to extend an interface and the properties of one interface gets derived into another interface.

Example:-

interface A
{
   public void set();
   public void get(int a, int b);
}
interface B extends A
{
    public void put();
   public void check();
}


Implementing Interface into a class or achieving multiple Inheritance:-

We know that in Java we can achieve multiple inheritance by using the concept of interface. we have two options to achieve multiple inheritance.


  1. By extending one class along with implementing one (or more) interface.
  2. By implementing multiple inheritance.
We use multiple inheritance to achieve/derive the properties of multiple classes/interfaces into one class to obtain their characteristics.

We have discussed the interface with multiple inheritance in our previous blog.








                                                                                                    










Post a Comment

0 Comments