Why do we need interfaces? Why do we need interface inheritance? Why do we need polymorphism? For those who read and understood how to do interface inheritance, but did not understand why. Last time, using the example of an ordinary Ivanov family, we analyzed why interfaces are needed. We continue to digitize the troubled family. Each person has some habits that he did not inherit from anyone or inherited from anyone - his personal habits. Our task is to give each family member unique habits. We translate it into the Java plane: it is necessary to implement unique methods in classes that will belong only to these classes. Well, go ahead! It's Petya: It's Dad: It's Mom: 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 to change all the time. The Ivanov family was no exception, they had a little girl named Masha. And she inherited 1. from Mom the habit of flapping her eyelashes, 2. and from Dad sniffing. We need to make changes to our program. Come on, it's not so difficult, the main thing is to think logically. After all, everyone knows why interfaces are needed. Now let's create an interface of Masha's Habits, describe there the method of flappingEyelashes () and sniffing (), implement it to Masha and it's in the hat. So what if methods with the same name are already implemented in other interfaces, once you can. Indeed, who knows what plans the Ivanov family has, if Seryozha is born, who will inherit the habits from Dad, Mom, Great-Grandfather and someone else from the fourth generation, each time create an interface, like: interface Habits of Seryozha, and there declare methods that can already be declared hundreds of times in other interfaces? Why do we need interfaces? - 1After a couple or three generations, we run the risk of getting interfaces with a bunch of identical methods that are already described in other interfaces, and if it is necessary to change the name of some habit (and this is quite realistic - after all, the world is changing), then I have no idea how to understand this spaghetti. All that remains is to sit and dream of a miracle. Now, if only there was an interface for each habit. Let's imagine two: And then, as in Lego, it would be possible, with the help of multiple inheritance from individual habits, to type the interface of habits of an individual family member we need. Something like this: And then just implement the right interface to the right class, for example, Mom: The same could be done with Dad, Peter 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... The drawn man is right, so in fact it is possible - now the simulation of the Ivanov family has been saved! An attentive reader may ask the question: "Why produce interfaces for each family member? We have a set of actions - to immediately implement it to the right class." Let's imagine that in many parallel worlds there are twins of Petit, and all Pete need to implement the interface Habits of Petit And if there were no common interface, it turns out a more voluminous repetitive code Interface inheritance makes the application more flexible to change, in particular, you can solve problems with repetitive methods. Once again, note that the multiple Interfaces are allowed.

public class Petya implements DadsHabit, MomsHabit{
    //These are Petya's personal habits
    public void play(){
        System.out.println("Playing cricket");
    }
    //Dad's inherited method implementation
    @Override
    public void squish(){
        System.out.println("Squish");
    }
    //Mom's inherited method implementation
    @Override
    public void sqeeze(){
        System.out.println("Sqeeze lips");
    }
}

class Dad implements DadsHabit{

    //These are Dad's personal habits
    public void scrachtchingHisBeard(){
        System.out.println("Scratching my beard.");
    }
    //These are the transferred habits
    public void squish(){
        System.out.println("Dad squishing.");
    }
}

class Mom implements MomsHabit{
    //These are mom's personal habits
    public void flapEyeLashes(){
        System.out.println("Clap-Calp");
    }
    //These are transferred Habits
    @Override
    public void sqeeze(){
        System.out.println("Press your lips");
    }
}