What is the reason for using the interface instead of the abstract class?

Asked 2 years ago, Updated 2 years ago, 93 views

In the past, when taking classes, the professor uses interfaces rather than abstract classes. And they didn't give me any additional explanations We moved on to the next page, but now that we're programming with Java, we have something to inherit The story suddenly came to mind. Does anyone know why?

interface java

2022-09-22 14:11

1 Answers

If you look at the Effective Java, Chapter 18 says let's use the interface rather than the abstract class.

For one thing, the striking difference between an interface and an abstract class is that an abstract class can contain implemented methods, and an interface does not. Another difference is that classes that implement types defined as abstract classes must be subclasses of abstract classes. In contrast, for classes that implement interfaces, you can implement all the methods defined in the interface and follow the interface implementation agreement. This has nothing to do with class inheritance. In Java, inheritance between classes allows only a single inheritance, so defining a type as an abstract class is severely constrained.

Existing classes can be easily changed to implement new interfaces. If all methods declared in the interface are not implemented in the class, you can add them and add the implementations clause to the class declaration. For example, when the Comparable interface was added to the Java platform, existing classes were changed to implement this interface. However, in general, it is not possible to modify an existing class to inherit a new classes.

If two classes want to inherit from the same abstract class, the abstract class should be raised to one level in the type class (class inheritance structure) to become the ancestor class of the two classes. Unfortunately, this causes great damage to the type class. This is because all descendant classes (including all subclasses of two classes) must inherit from the new abstract class, whether it is appropriate to do so.

The interface is ideal for defining a mixin. Mix-in is a type that the class can implement in addition to its "original type" and provides selectable functions, which are declared by the class to be provided. For example, Comparable is a mix-in interface that can be declared in the class that instances of the class are sorted by comparison with other mutually comparable objects. The reason why such an interface is called a mix-in is that it is possible to "mixed in" a function that can be selected for any type of original function. For the same reason that an existing class cannot be modified to inherit from an abstract class, an abstract class cannot be used to define a mix-in. In other words, the class cannot have more than one super class, so there is no suitable place to insert a mix-in into the class inheritance layer.

For example, in reality, there are cases of singers and composers, and this case can be easily implemented if implemented through an interface.

public interface Singer{
    AudioClip sing(Song s);
}

public interface Songwriter{
    Song compose(boolean hit);
}

public interface SingerSongwriter extends Singer,Songwriter{
    AudioClip strum();
    void actSensitive();
}

This flexibility isn't always necessary, but if you really need it, the interface will be the answer.


2022-09-22 14:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.