user1985273
user1985273

Reputation: 1967

Usings abstract class to add a method to an interface

I want to add a method to an interface, but i do not want to rewrite all the implementations (i only need to use it in one or two implementation). I read that i can achive this with the use abstract classes, but i cant quite figure out how its done?

Inteface:

public interface Animal {
    public void doSound();
}

Classes:

public class Cat implements Animal {

    @Override
    public void doSound() {
        System.out.print("meow");
    }

}


public class Dog implements Animal{

    @Override
    public void doSound() {
        System.out.print("bark");
    }

}

What i want is to be able to call

animal.doSomethingElse()

but i dont want to add this method to every implementation. Can this be done?

EDIT: Should have mentioned this before, i am not using java 8.

Upvotes: 0

Views: 106

Answers (5)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726669

Unfortunately, no: you cannot add a method to an interface without having to recompile all implementations of the interface.

You can add a method to the abstract class, and change all references to the interface with references to your abstract class. That, however, defeats the purpose of having the interface in the first place.

Finally, in Java 8 you can address this problem by providing a default implementation of a method in an interface. If Java 8 is an option, I would definitely recommend this route.

If you would like to avoid problems like this in the future, you could follow the interface + adapter pattern that Java designers have been following in the AWT framewrok (among other places). They would provide a pair of an interface and its default, do-nothing implementation. Everyone would be encouraged to program to the interface, and base their implementations of the interface on its default implementation counterpart (the adapter). This way Swing designers were free to add more methods to their interface without breaking existing implementations.

For reference, see MouseListener interface and MouseAdapter class.

Upvotes: 2

Adi
Adi

Reputation: 2394

It is possible in Java 8 with help of default method in interface.

public interface Animal {
    void doSound();

    default void doSomethingElse(){
      // do something
    }
}

In case of default methods, your implemented classes from Animal doesn't have to override them.

prior to java 8, you have to make your Animal class abstract and add method implementation there.

Upvotes: 3

Maciej Szumielewicz
Maciej Szumielewicz

Reputation: 325

It is not possible with Java including versions to 7. You can define interface Animal as abstract class and implement new method within it. Sample code as follows:

public abstract class Animal {

    public abstract void doSound();

    public void doSomethingElse() {}

}

However if you are using Java 8 you have mechanism which is called default methods. Example below

public interface Sample {

    public abstract void doSound();

    public default void doSomethingElse() {};

}

default methods do not have to be implemented by classes. Mechanism is very useful when it comes to interfaces with large number of classes implementing certain interface. You can extend it without changing all classes

Upvotes: 2

markbernard
markbernard

Reputation: 1420

You can extend Animal as a separate interface or an abstract class.

public interface Pet extends Animal {
    public void fetch();
}

or

public abstract class Pet implements Animal {
    public abstract void fetch();
}

Upvotes: 0

NPE
NPE

Reputation: 500475

You could change Animal into an abstract class. This will enable you to selectively implement methods:

public abstract class Animal {

    public abstract void doSound();

    public void doSomethingElse() {
    }
}

Upvotes: 3

Related Questions