Interface Concept Simplified In Java

0
48
Spread the love
  • 24
  • 34
  • 14
  • 12
  •  
  •  
  •  
  • 12
  •  
  •  
    96
    Shares

Interface Concept Simplified In Java
Interface Concept Simplified In Java
Interface Concept Simplified In Java
Interface Concept Simplified In Java

 

Interface Concept Simplified In Java:

An interface is a reference type in java and an interface is like a simple java class  that has static constants and abstract methods.Since java does not support multiple inheritance directly,Java uses interface to implement multiple inheritance.A java class can implement multiple java interfaces.

Interfaces provides a way to create a set of classes which have rather different purpose but few similar methods.All methods in an interface are public and abstract. So an interface is a pure abstract class.

When a child class needs to be created from two or more number of super classes,the phenomenon is called multiple inheritance.Java does not support multiple inheritance directly(meaning java classes can not have more than one super class like Class A extends B extends C {} is illegal in java) but with interface we can do similar approach.In real life we can create several distinct classes to support multiple inheritance.In java interface is a reserved keyword that implies an interface consists of a set of instance method interfaces without any implementation.

A class can implement an interface by providing an implementation for each of the methods specified by the interface.The class which is trying to implement the method must state to java virtual machine that this class is going to implement some interface by the keyword “implements”.Like class circle implements Drawable{….}.

Even though the interfaces are not exact classes,they can be compared to abstract classes in the sense that we can not create objects from interface or abstract class but can be used as a base for making sub classes.

Along with the methods an interface may also contain constants,default methods and static methods. Method body exists for only default methods and static methods.

So Interface allows the creator of the classes to establish the form of the classes,method names,argument lists and return types but no bodies.It is possible to add initialized definitions for primitives but these becomes compile time constant.They are static and final thus no storage to be created for the actual object.

An interface says “this is what all classes that implement this particular interface shall look like.Any code that  uses a particular interface knows what methods may be called for the interface.So interface is used to establishes a protocol between classes. Creating an interface -instead of class,we need to use the word called Interface.It can be public or friendly.”

For primitive types,they become compile time constant which is equivalent to create enum in java.We can create an interface containing all over enumeration identifier providing an unique integer value. This is not ideal.

So if we say that a particular class implements an interface.It is a promise that we have included methods in our class corresponding to each of the methods in the definition of the interface.

Syntax of interface:


interface interfaceName{
//variables and methods
}
//another one
interface interfaceName[extends Name1]{
//variables and methods
}
 

One Example for interface

We need to use interface keyword to define an interface

//FileName: MyInterface.java
import java.lang.*;
//any number of import statements
public interface MyInterface{
//any number of final and static fields
public abstract doSomething();
public abstract readSomething();
//any number of abstract method declaration
}
 

Interface will have the properties

  • An interface is implicitly abstract. We do not need to use the abstract keyword declaring interface.
  • Each method in an interface is also implicitly abstract so the abstract keyword is not needed.
  • Methods in an interface are implicitly public.

What is Implementing an Interface?

While a class implements an interface, it makes a contract with the interface to perform specific behavior of the interface. If the class fail to perform all behaviors of the interface,the class must declare itself abstract.

A class uses the implements keyword to implement an interface. The implement keyword is placed in the class deceleration following the extends portion of the deceleration.

The class has to override all the abstract methods defined in the interface. There are few rules around. They are as follows:

  • Checked Exceptions should not be declared on implementation methods other than the ones declared by the interface method or sub class of those declared by the interface method.
  • The signature of the interface method and same return type or sub type should be maintained when overriding the methods.
  • An implementation class itself can be abstract and if so , the interface methods need not be implemented.
  • A class can implement more than one interface at a time but can extend only one class.
  • An interface can extend to another interface.

Class structure implementing interface:


class MyClass extends SuperClass implements interface1,interface2,interface3......intefaceN{
//body of the class
}
 

The member variables present in an interface should be static and final.
Example:


interface Pet{
public void test();
}
public class Cat implements Pet{
public void test()
{
System.out.println("Interface method implemented in the child class");
}
public static void main(String args[])
{
Pet p=new Cat();
p.test();
}
}
 

output of the code:
$javac Cat.java
$java -Xmx128M -Xms16M Cat
Interface method implemented in the child class

One more example:


interface Area{
    final static float PI=3.14F;
    float compute(float x,float y);
}
class Rectangle implements Area{
    public float compute(float x,float y)
    {
        return x*y;
    }
}
class Circle implements Area{
    public float compute(float x,float y)
    {
        return PI*x*y;
    }
}
public class MYInterfaceTest{
    public static void main(String args[])
    {
        Rectangle rect=new Rectangle();
        Circle cir=new Circle();
        Area area;
        area=rect;
        System.out.println("Area of the rectangle "+area.compute(10,20));
        area=cir;
        System.out.println("Area of the rectangle "+area.compute(10,20));
    }
}
 

The output of the code:
$javac MYInterfaceTest.java
$java -Xmx128M -Xms16M MYInterfaceTest
Area of the rectangle 200.0
Area of the rectangle 628.0

A more real time example:

Assume that there are two classes for an organization

  • One PermanentEmployee
  • One Vendor Employee

Now each class has their unique components called Qualification,MainCompany,Current_Project,Current_Project,Experience(),Next_Project().

class PermanentEmployee class VendorEmployee
Qualification MainCompany
Current Project CurrentProject
Experience() Next_Project()
Class Organization
……
Resume

Now if the Organization wants to have a resume for both type of Employees.So the Organization class with define an interface called Resume.


interface Resume{
....
void EmpResume();
}
class  PermanentEmployee Extends Employee implements Resume{
Qualification;//member variables
Current_Project;//member variables
Experience(){};//member function
public void EmpResume()
{
....
//code to implement the resume
}
}
class VendorEmployee implements Resume{
MainCompany;//member variables
Current_Project;//member variables
Next_Project(){}//member function
public void EmpResume()
{
....
//code to implement the resume
}
}
//so the implementation of the code goes like
PermanentEmployee pe=new PermanentEmployee();
pe.EmpResume();
VendorEmployee ve=new VendorEmployee();
ve.EmpResume();
//Futher more we can implement an array of Resume. This is possible as both implement Resume hence they become same type.
Resume listOfResume=new Resume[10];
listOfResume[0]=new PermanentEmployee;
listOfResume[0]=new VendorEmployee;
....
.....

Interface methods

Interface methods are those methods which are abstract methods appearing in the interface.If a class implements an interface but does not implement one or more of the interface methods,the whole class becomes an abstract class and can not be instantiated.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 24
  • 34
  • 14
  • 12
  •  
  •  
  •  
  • 12
  •  
  •  
    96
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here