JavaRush /Java Blog /Random EN /Why do we need interface inheritance?
Павел
Level 11

Why do we need interface inheritance?

Published in the Random EN group
Why are interfaces needed? Why do we need interface inheritance? Why do we need polymorphism? For those who read and understood how to do inheritance of interfaces, but did not understand why. Last time, using the example of an ordinary Ivanov family, we figured out why interfaces are needed. We continue to digitize the restless family. Each person has some habits that he did not inherit from anyone or did not inherit from anyone - his personal habits. Our mission is to give each member of the family unique habits. We translate into the Java plane: it is necessary to implement unique methods in classes that will belong only to these classes. Well, go ahead! This is Petya:
class Петя implements ПривычкиПапы, ПривычкиМамы {

//это личные Петины привычки
public void ковырятьВНосу () {
System.out.println("Ковырь-ковырь");
    }

//это унаследованные привычки
@Override
public void прихлюпывать() {
     System.out.println("Хлюп");
   }

@Override
public void поджимать () {
System.out.println("Поджать губки");
    }
}
This is Dad:
class Папа implements ПривычкиПапы {

//это личные Папины привычки
public void чесатьБороду () {
System.out.println("Чешу бороду");
    }

//это переданные привычки
   @Override
    public void прихлюпывать() {
     System.out.println("Хлюп");
   }
}
This is Mom:
class Мама implements ПривычкиМамы{

//это личные Мамины привычки
public void хлопатьРесницами () {
System.out.println("Хлоп-хлоп");
    }

//это переданные привычки
@Override
public void поджимать() {
System.out.println("Поджать губки");
    }
}
Perfect! Everything works as it should! In the first article, it was said that the program is a reflection of the real world. The most interesting property of reality is that it changes all the time. The Ivanov family was no exception, they had a sweet daughter named Masha. And she inherited from Mom the habit of batting her eyelashes, and from Dad to squish. We need to make changes to our program. Why do we need interface inheritance?  - 1Come on, it's not that difficult, the main thing is to think logically. After all, everyone knows why interfaces are needed. Now let's create the Masha's Habits interface , describe the method to clapEyelashes() and squish() , implement it to Masha and that's it. So what if methods with this name are already implemented in other interfaces, once you can. Why do we need interface inheritance?  - 2Indeed, who knows what plans the Ivanov family has if Seryozha is born , who will inherit habits from Dad, Mom, Great Grandfather and someone else from the fourth generation, each time create an interface, like: interface Serezha's Habits , and there declare methods that can already be declared hundreds of times in other interfaces? In a couple, three generations, we risk getting interfaces with a bunch of identical methods that are already described in other interfaces, and if we need to change the name of some habit (and this is quite real - because the world is changing), then how to figure out this spaghetti, I I don't represent. It remains only to sit and dream of a miracle. Why do we need interface inheritance?  - 3Now, if every habit had its own interface. Let's imagine:
public interface ПривычкаПрихлюпывать {
    public void прихлюпывать();
}
public interface ПривычкаПоджимать {
    public void поджимать();
}
public interface ПривычкаКовырятьВНосу {
    public void ковырятьВНосу();
}
public interface ПривычкаХлопатьРесницами {
    public void хлопатьРесницами();
}
public interface ПривычкаЧесатьБороду {
    public void чесатьБороду();
}
And then, it would be possible, like in Lego, with the help of multiple inheritance from individual habits, to type the interface of habits of an individual member of the family that we need. Something like this:
public interface ПривычкиМамы extends ПривычкаПоджимать, ПривычкаХлопатьРесницами {
    }
public interface ПривычкиПапы extends ПривычкаЧесатьБороду, ПривычкаХлюпать {
    }
public interface ПривычкиПети extends ПривычкаПоджимать, ПривычкаХлюпать,ПривычкаКовырятьВНосу {
    }
public interface ПривычкиМаши extends ПривычкаХлюпать, ПривычкаХлопатьРесницами {
    }
And then just implement the desired interface to the desired class, for example, Mom:
class Мама implements ПривычкиМамы{
@Override
public void хлопатьРесницами () {
System.out.println("Хлоп-хлоп");
    }

@Override
public void поджимать() {
System.out.println("Поджать губки");
    }
}
The same could be done with the Pope, Petya and Masha . And then, with the expansion of the Ivanov family, there would be no problems with habits, we would simply shuffle them through inheritance at the interface level, like ingredients in a salad, and not produce a bunch of methods with the same name. Oh, dreams, dreams... Why do we need interface inheritance?  - 4The drawn person is right, it's actually possible - now the simulation of the Ivanov family is saved! An attentive reader may ask the question: "Why produce interfaces for each member of the family? We have a set of actions - immediately implement the desired class." Let's imagine that in many parallel worlds there are twins of Petya , and all Petya need to implement interface Petya's Habits
interface ПривычкиПети extends ПривычкаПоджимать, ПривычкаХлюпать,ПривычкаКовырятьВНосу

class ПетяВселеннаяХ implements ПривычкиПети
class ПетяВселеннаяY implements ПривычкиПети
// и т.д.
What if there was no common interface?
class ПетяВселеннаяХ implements ПривычкаПоджимать, ПривычкаХлюпать,ПривычкаКовырятьВНосу
class ПетяВселеннаяY implements ПривычкаПоджимать, ПривычкаХлюпать,ПривычкаКовырятьВНосу
// и т.д.
It turns out more voluminous repeating code. Interface inheritance makes the application more flexible to change, in particular - you can solve problems with repetitive methods. Again, note that multiple inheritance of interfaces is allowed.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION