jeudi 26 mai 2016

Java interface function with extra parameter

I am beginner in Java, and OOP, and i am actually working with interfaces.

Here is my problem explained with code, excuse me if it's stupid question :)

I have one interface, for example animal,that can shout.

public interface Animal{
    public void shout();
}

My class dog implement the interface, and can shout at anybody.

public class Dog implements Animal{
    public void shout(){
        System.out.println("Woof woof");
    }
}

Ok, now i want a specific dog, Class SmarterDog, that can shout to someone specific.

So my function would be void shout(String somebody), so we have just one extra parameter, but still approximatively the same code. It's still an Animal, so it should implement Animal.

How can i organize my code to simulate this function? The example i show is easy, but for more complex functions what should i do? Create an extra function in the class that implement my interface?

Thanks

Aucun commentaire:

Enregistrer un commentaire