Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Default interface methods

Till now we have been learning that interfaces can contain only abstract methods, not concrete methods (methods with a body) at all.

Yes, it was absolutely right but before JDK8.

From JDK8 Java has allowed the interface to contain concrete methods. But those concrete methods must be preceded with keyword default in its declaration.

Syntax for default methods

accessModifier default returnType methodName(parameterlist)
{
    //statements
}

Why default method?

You might be puzzled by thinking what the use of default methods is?

Don’t worry; the reasons are very simple.

Suppose our application contains a very important interface that is implemented by many numbers of classes of that application.

After some time if there is a need to expand this important interface by adding an abstract method & we tried to do that then it will be compulsory to implement that added abstract method in all the classes that implement this interface, which is a big headache for the programmer.

So to avoid such type of problems default method is very important because it does not force the classes to implement default method present in the interface.

By default, every class implementing that interface will have access to default method.

Similarly there are more few reasons which forced Java to introduce default method.

Example

interface CustomerDemand 
{
    public void makeSoftware();
	
    public default void provideService(int yr)
    {
        System.out.println("Provide free service for "+yr+" years");
    }
}

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

class SoftwareComp2 implements CustomerDemand
{
    public void makeSoftware()
    {
        System.out.println("Software developed in Dot Net Framework");
        System.out.println("We don't provide service");
    }
}
 
class DefaultMethodDemo 
{
    public static void main(String args[])
    {
        SoftwareComp1 cmp1 = new SoftwareComp1();
        SoftwareComp2 cmp2 = new SoftwareComp2();
		
        cmp1.makeSoftware();
        cmp1.provideService(2);
        System.out.println("--------------------------------");
        cmp2.makeSoftware();		
    }
}

Output

Software developed in Java and its Technologies
Provide free service for 2 years
--------------------------------
Software developed in Dot Net Framework
We don't provide service

In the above program, we have made provideService() method as default. Therefore it is not compulsory to implement this method again.

Java allows overriding the default method in a class that implements the interface.

Example

interface CustomerDemand 
{
    public void makeSoftware();
	
    public default void provideService(int yr)
    {
        System.out.println("Provide free service for "+yr+" years");
    }
}

class SoftwareCompany implements CustomerDemand 
{
    public void makeSoftware() 
    {
        System.out.println("Developed in Java");	
    }
	
    public void provideService(int yr)
    {
        int rate = 10000;
        int total  = rate * yr;
        System.out.println("We'll apply charge of "+total+"$ for "+yr+" years");
    }
}

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

Output

Developed in Java
We'll apply charge of 40000$ for 4 years

Note

Although it is possible to define default method in an interface, the interface still can’t be used to create its object.

 

Leave a reply