Introduction to Interface In JAVA

Introduction

In this article we will discuss about interface in JAVA. As multiple inheritance in JAVA only achieve through Inerface. So we discuss about Interface in JAVA.

What is Interface

  • In computing, interface is act like a communicator between peripheral devices(monitor, keyboard) and computer system.
  • In field of computer science, an interface refers as a interactor between components.
  • In computer any type of communication of computer system with user we say it is an interface.

Interface in Java

An interface is a blueprint of class. It is a java core part and a way to achieve data abstraction in Java along with abstract class. Since multiple inheritance is not allowed in Java, interface is only way to implement multiple inheritance. At in basic level interface in java is a keyword but that time it is an object oriented term to define contracts and abstraction, This contract is followed by any implementation of Interface in Java.

Key Point of Interface in JAVA

  • You can fully abstract a class using keyword interface.
  • While declaring interface in a class there is no need of keyword abstract .
  • All methods in an interface are implicitly public.
  • Interface is not a class defining an interface is similar to defining a class, but they are two different concepts. A class define the attributes and behaviors of an object. An interface contains behaviors that a class implements.
  • We have to define all the abstract methods of the interface in the class.

Declaring Interfaces

Using Interface keyword to declare interface.

Example

Define a simple interface

interface Ex1
  {
    public static final int min=10;
    public abstract void print();
  }

Define another interface

interface Ex2
  {
    public static final int min=5;
    public abstract void show();
  }

Implementing Interfaces

A class uses the implements keyword to implement an interface. If a class implements more than one interface, they are separated with a comma. The methods that implement an interface must be declared public (to be access by other member). Also, the type signature of implementing method must match exactly the type signature specified in the interface definition.

Sample Program

class InheritanceEx implements Ex1
  {
    public void print()
      {
        System.out.println("Hello");
      }
    public static void main(String args[])
      {
        InheritanceEx obj=new InheritanceEx();
        obj.print();
      }
  }

Output

inheritance.jpg

Multiple Inheritance

Multiple inheritance is the ability of a single class to inherit from multiple classes. Multiple inheritance is a feature of some object-oriented computer programming languages in which a class can inherit characteristics and features from more than one superclass. It is distinct to single inheritance, where a class may only inherit from one particular superclass.

Java does not support multiple inheritance. For achieving multiple inheritance JAVA use interfaces.

In below figure we show Multiple Inheritance. This figure contain multiple animal class, this animal class are inherited according to there nature (like Rabbit in Herbivore, Lion and Hyena in Carnivore), and after that all these class iherited in a single class called Animal Class.

m1.gif

Implementing Multiple Interfaces (i.e Multiple Inheritance)

Sample program

class MulInheritanceEx implements Ex1,Ex2
  {
    public void print()
      {
        System.out.println("Hello");
      }
    public void show()
      {
        System.out.println("Welcome");
      }
    public static void main(String args[])
      {
        MulInheritanceEx obj=new MulInheritanceEx();
        obj.print();
        obj.show();
      }
  }

Output

Multipleinheritance.jpg

Note 1-For overiding methods there are certain rules to be followed

  1. The argument parameter should be exactly the same as that of the overridden method.
  2. A method declared static cannot be overridden but can be re-declared.
  3. The return type should be the same or a subtype of the return type declared in the original overridden method in the super class.
  4. A method declared final cannot be overridden.
  5. Constructors cannot be overridden.
  6. If a method cannot be inherited then it cannot be overridden.
  7. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now