Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Interfaces

Interfaces are similar to classes.
An interface can contain only abstract methods, not concrete methods.
An interface can also contain variables. But these variables are not instance variables. They are public, final & static implicitly.

Syntax of Interface

accessModifier interface InterfaceName
{
    // variables (public, final & static implicitly)
    // abstract methods (public implicitly)
}

Access modifier decides the scope of the interface.
Java does not allow creating the object of an interface.

Interface implementation

To make the use of an interface, it should be implemented by another class using keyword implements.

accessModifier interface InterfaceName
{
    // variables
    // abstract methods
}

Class ClassName implements InterfaceName
{
 // implementation of abstract methods present in interface
}

Interface just tells “what to do,” not “how to do.” It is the duty of a class that implements the interface.

For better understanding let’s take a small real life example.

Java Programming Language Interface Example

Suppose you are an owner of some company & you want some software for that company.
Then you’ll go to some software company as a customer & will demand them to create software for your company.
You’ll just tell them your requirement that is “what to do.” Now it’s a job of that software company to implement your demand.
Here, your demand acts as an interface & company as a class that will implement your demand.

Example

interface CustomerDemand 
{
    public void makeSoftware();
}

class SoftwareCompany implements CustomerDemand
{
    public void makeSoftware()
    {
        System.out.println("Software developed in Java");
    }
}

class IFImplementation 
{
    public static void main(String args[]) 
    {
        SoftwareCompany s = new SoftwareCompany();
        s.makeSoftware();
    }
}

Output

Software developed in Java

The overridden method in a class that implements an interface must be declared as public, as well as its syntax, must match with the method specified in the interface.
Any interface can be implemented by any number of classes.
Each implementing class has complete independence to implement the interface in its own way.

Example

interface CustomerDemand
{
    public void makeSoftware();
}

class SoftwareCompany1 implements CustomerDemand
{
    public void makeSoftware()
    {
        System.out.println("Software developed in Java");
    }
}

class SoftwareCompany2 implements CustomerDemand
{
    public void makeSoftware()
    {
        System.out.println("Software developed in .Net");
    }
}

class Test 
{
    public static void main(String args[]) 
    {
        SoftwareCompany1 s1 = new SoftwareCompany1();
        SoftwareCompany2 s2 = new SoftwareCompany2();
        s1.makeSoftware();
        s2.makeSoftware();
    }
}

Output

Software developed in Java
Software developed in .Net

A class implementing interface can also contain its own method in addition to implemented methods of the interface.

Example

interface CustomerDemand 
{
    public void makeSoftware();
}

class SoftwareCompany implements CustomerDemand
{
    public void makeSoftware()
    {
        System.out.println("Software developed in Java");
    }
	
    public void service()
    {
        System.out.println("One year free service");
    }
}

class Test2 
{
    public static void main(String args[])
    {
        SoftwareCompany s = new SoftwareCompany();
        s.makeSoftware();
        s.service();
    }
}

Output

Software developed in Java
One year free service

A single class can implement more than one interface.
Implemented interfaces are separated by a comma.

Example

interface Customer1Demand 
{
    public void makeSoftware();
}

interface Customre2Demand 
{
    public void provideService();
}

class SoftwareCompany implements Customer1Demand,Customre2Demand
{
    public void makeSoftware()
    {
        System.out.println("Software developed in Java");
    }
	
    public void provideService()
    {
        System.out.println("Will provide service for 5 years");
    }
}

class Test3
{
    public static void main(String args[]) 
    {
        SoftwareCompany s = new SoftwareCompany();
        s.makeSoftware();
        s.provideService();
    }
}

Output

Software developed in Java
Will provide service for 5 years

Interface reference to refer implementing class objects

Yes, it is possible to use interface type reference to refer implementing class objects.

Example

interface CustomerDemand 
{
    public void makeSoftware();
}

class SoftwareCompany1 implements CustomerDemand
{
    public void makeSoftware() 
    {
        System.out.println("Developed in Java");
    }
}

class SoftwareCompany2 implements CustomerDemand
{
    public void makeSoftware() 
    {
        System.out.println("Developed in .NET");
    }
}

class Test4 
{
    public static void main(String args[])
    {
        CustomerDemand c;
        SoftwareCompany1 s1 = new SoftwareCompany1();
        c = s1;  
        c.makeSoftware(); // calls SoftwareCompany1 method
		
        SoftwareCompany2 s2 = new SoftwareCompany2();
        c = s2;
        c.makeSoftware(); // calls SoftwareCompany2 method
    }
}

Output

Developed in Java
Developed in .NET

Java Programming Language Interface reference to refer implementing class Objects

Partial implementation of interface

If any class implements a specific interface but does not implement all the methods of that interface then it is known as a partial implementation of the interface.
The class that implements an interface partially must be declared as abstract. That is the class must either implement interface completely or must be declared as abstract itself.

Example

interface CustomerDemand 
{
    public void makeSoftware();
    public void provideService();	
}

abstract class SoftwareCompany1 implements CustomerDemand
{
    public void makeSoftware()
    {
        System.out.println("Developed in Java");
    }
}

Here the class SoftwareCompany1 does not implement CustomerDemand completely, therefore, it is declared as abstract.

If any other class extends SoftwareCompany1, it must either implement all unimplemented methods of Customerdemand or declared as abstract itself.

Example

interface CustomerDemand 
{
    public void makeSoftware();
    public void provideService();	
}

abstract class SoftwareCompany1 implements CustomerDemand
{
    public void makeSoftware()
    {
        System.out.println("Developed in Java");
    }
}

class SoftwareCompany2 extends SoftwareCompany1
{
    public void provideService()
    {
        System.out.println("Will provide service for 10 years");		
    }
}

class PartialImplementation 
{
    public static void main(String args[]) 
    {
        SoftwareCompany2 s2 = new SoftwareCompany2();
        s2.makeSoftware();
        s2.provideService();
    }
}

Output

Developed in Java
Will provide service for 10 years

 

Leave a reply