This page was exported from TechnicalStack [ http://technicalstack.com ]
Export date: Wed Oct 17 8:40:42 2018 / +0000 GMT

Default Method



Need for default method-

Without effecting implementation classes if we want to add new method to the interface then we can do this with default interface.

Syntax:

package com.technicalstack.basic;

interface InterfaceDemo{

default void f1(){

System.out.println("Default Method");

}

}

public class DefaultMethod implements InterfaceDemo{

public static void main(String[] args) {

DefaultMethod d = new DefaultMethod();

d.f1();

}

}

Output: Default Method

Suppose if we want to provide new implementation then we can do this as well.

This can be done as below:

package com.technicalstack.basic;

interface InterfaceDemo{

default void f1(){

System.out.println("Default Method");

}

}

public class DefaultMethod implements InterfaceDemo{

public void f1(){

System.out.println("Own new Implementation");

}

public static void main(String[] args) {

DefaultMethod d = new DefaultMethod();

d.f1();

}

}

Now if we compile and run the above code below is the output that we will get:

Output: Own new Implementation

 

 


Post date: 2018-08-28 09:49:43
Post date GMT: 2018-08-28 09:49:43
Post modified date: 2018-08-28 09:50:46
Post modified date GMT: 2018-08-28 09:50:46

Powered by [ Universal Post Manager ] plugin. MS Word saving format developed by gVectors Team www.gVectors.com