JavaRush /Java Blog /Random EN /Why are interfaces needed?
Павел
Level 11

Why are interfaces needed?

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 make interfaces, but did not understand why.

Disclaimer: In this article and subsequent ones, the naming of classes and methods will be given in Russian.

When it comes to the principles of object-oriented programming (OOP): polymorphism, inheritance, and encapsulation, it's helpful to use real-world analogies. The big advantage of OOP is that we can simulate part of the real universe in a program. Let's model the Ivanov family: Dad , Mom and boy Petya . From Dad, Petya inherited the habit of slurping when drinking tea, and from Mom, he inherited the habit of pursing his lips while reading. If we try to implement this situation into a program, we will end up with three classes:
class Папа
class Мама
class Петя
Dad and Mom have habits that need to be passed on to Pete . Habits are actions of some kind - so it's best to implement them in the software world as methods: Dad First :
class Папа {
public void прихлюпывать() {
System.out.println("Хлюп");
    }
}
Now Mom :
class Мама {
public void поджимать() {
System.out.println("Поджать губки");
   }
}
If we are talking about inheritance, then it is logical to write the code like this:
class Петя extends Папа, Мама {
@Override
public void прихлюпывать() {
     System.out.println("Хлюп");
   }

@Override
public void поджимать() {
System.out.println("Поджать губки");
    }
}
That is, to inherit Petya from Dad and Mom at the same time. If you write it like this, the compiler will complain because Java cannot implement multiple class inheritance. By the way, it’s possible in C++, but not in Java, because big problems can arise with multiple inheritance: they write in detail on the Internet . Why are interfaces needed?  - 1To get around this “impossible”, Java has interfaces. And for habits we will come up with our own interface. Even two: They will look like this:
public interface ПривычкиПапы {
    public void прихлюпывать();
}
public interface ПривычкиМамы {
    public void поджимать();
}
In the interface, we only described the habits, but did not describe what they specifically do, because we will write the specific implementation in classes. First, let's give Dad and Mom their legal habits.
class Папа implements ПривычкиПапы {

   @Override
    public void прихлюпывать() {
     System.out.println("Хлюп");
   }
}

class Мама implements ПривычкиМамы{

@Override
public void поджимать() {
System.out.println("Поджать губки");
    }
}
And now, it is absolutely legal to transfer habits from Dad and Mom to Pete at the same time
class Петя implements ПривычкиПапы, ПривычкиМамы {

@Override
public void прихлюпывать() {
     System.out.println("Хлюп");
   }

@Override
public void поджимать() {
System.out.println("Поджать губки");
    }
}
That is, multiple implementation (more often called implementation) in Java is quite possible. Why are interfaces needed?  - 2The meaning of interfaces should now be clear - in Java, you can implement multiple inheritance using interfaces. If we develop the situation further, for example: after all, Dad and Mom probably have habits that they did not pass on to Petya , and Petya may also have his own personal habits. You will learn how to transfer this vital Santa Barbara to the Java plane in the following episodes. This is not the only example for understanding interfaces. If you haven’t read the following articles, then be sure to read: Interfaces in Java (if not open, you can exit your profile or read in incognito mode) Why interfaces are needed in Java - implement all the examples from the article here and change the methods both in interfaces and in classes: names of methods, signatures (what the method takes as input), output types of methods. Understand on your own: - the difference when implementing an interface with a class and an abstract class; - default methods.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION