user4489210
user4489210

Reputation:

Android Studio - How to implement an interface methods in all the implementing classes?

I have an interface which is being implemented in some 20-30 classes. I have added a new new method to this interface. Is there any shortcut in Android Studio to override this method in all the sub classes? Or do I need to manually go to all the classes and implement manually?

Upvotes: 2

Views: 8729

Answers (2)

ZaptechDev Kumar
ZaptechDev Kumar

Reputation: 164

Ya, There is a way to achive such things by making your method DEFAULT inside your Interface.

For Example :

public interface oldInterface {

public void existingMethod();

    default public void newDefaultMethod() {

    System.out.println("New default method"

          " is added in interface");

}

}

The following class will compile successfully in Java JDK 8,

public class oldInterfaceImpl implements oldInterface {

public void existingMethod() {

    // existing implementation is here…

}

}

If you create an instance of oldInterfaceImpl:?

    oldInterfaceImpl obj = new oldInterfaceImpl ();

// print “New default method add in interface”

obj.newDefaultMethod();

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

If you use Java 8, then there's new interface feature called "default method" you may try using:

https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

Upvotes: 2

Related Questions