JavaRush /Java Blog /Random EN /Why is interface inheritance necessary?
Павел
Level 11

Why is interface inheritance necessary?

Published in the Random EN group
Why are interfaces needed? Why is interface inheritance necessary? Why is polymorphism needed? For those who read and understood how to do interface inheritance, but did not understand why. Last time, using the example of the ordinary Ivanov family, we looked at why interfaces are needed. We continue to digitize the troubled family. Every person has some habits that he did not inherit from anyone or did not pass on to anyone - his personal habits. Our task is to provide each family member with unique habits. Let's move to the Java plane: we need 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 the habit of batting her eyelashes from Mom and sniffling from Dad. We need to make changes to our program. Why is interface inheritance necessary?  - 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 an interface Masha’s Habits , describe the clapElashes() and squish() method there , implement it to Masha and we’re done. So what if methods with the same name are already implemented in other interfaces, you can do it once. Why is interface inheritance necessary?  - 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 creating an interface, like: interface Seryozha’s Habits , and there declare methods that can already be declared hundreds of times in other interfaces? In a couple or 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 possible - after all, the world is changing), then how to figure out this spaghetti, I I can't imagine. All that remains is to sit and dream of a miracle. Why is interface inheritance necessary?  - 3Now, if each 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, like in Lego, we could, using multiple inheritance from individual habits, type the interface we need for the habits of an individual member of the family. Something like this:

public interface ПривычкиМамы extends ПривычкаПоджимать, ПривычкаХлопатьРесницами {
    }
public interface ПривычкиПапы extends ПривычкаЧесатьБороду, ПривычкаХлюпать {
    }
public interface ПривычкиПети extends ПривычкаПоджимать, ПривычкаХлюпать,ПривычкаКовырятьВНосу {
    }
public interface ПривычкиМаши extends ПривычкаХлюпать, ПривычкаХлопатьРесницами {
    }
And then simply 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 Papa, 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 would not produce a bunch of methods with the same name. Eh, dreams, dreams... Why is interface inheritance necessary?  - 4The man drawn is right, this is actually possible - now the simulation of the Ivanov family is saved! An attentive reader may ask the question: “Why create interfaces for each family member? We have a set of actions - immediately implement them for the required class.” Let’s imagine that in many parallel worlds there are Petya’s doubles , and all Petyas need to implement the interface Petya’s Habits

interface ПривычкиПети extends ПривычкаПоджимать, ПривычкаХлюпать,ПривычкаКовырятьВНосу 

class ПетяВселеннаяХ implements ПривычкиПети
class ПетяВселеннаяY implements ПривычкиПети
// и т.д.
What if there was no common interface?

class ПетяВселеннаяХ implements ПривычкаПоджимать, ПривычкаХлюпать,ПривычкаКовырятьВНосу
class ПетяВселеннаяY implements ПривычкаПоджимать, ПривычкаХлюпать,ПривычкаКовырятьВНосу
// и т.д.
This results in more repetitive code. Interface inheritance makes the application more flexible to change, in particular, problems with repeating methods can be solved. Please note again that multiple inheritance of interfaces is allowed.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION