In implementing the interface, we use interface syntax to separate the interface from the class definition and form a subject. interface provides interface specifications for classes.
In inheritance, we introduce the inheritance mechanism in order to improve the reusability of the program. Inheritance at that time was based on classes. The interface interface can also be inherited to extend the original interface.
Interface inheritance
Interface inheritance (inheritance) is very similar to class inheritance, which is to add new interface method prototypes based on the inherited interface. For example, we use Cup as the original interface:
Copy the code code as follows:
interface Cup {
void addWater(int w);
void drinkWater(int w);
}
On the basis of inheriting Cup, we define a new interface for cups with scales, MetricCup
The interface is as follows:
Copy the code code as follows:
interface MetricCup extends Cup
{
int WaterContent();
}
We have added a new method prototype WaterContent(), which returns an integer (amount of water).
Multiple inheritance of interface
In Java class inheritance, a derived class can only have one base class. In other words, a class cannot inherit from more than one class at the same time. In Java, an interface can inherit more than one interface at the same time, which is called multiple inheritance.
For example, we have the following Player interface:
Copy the code code as follows:
interface Player
{
void play();
}
We add a new MusicCup interface. It has both Cup interface and Player interface, and adds a display() method prototype.
Copy the code code as follows:
interface MusicCup extends Cup, Player
{
void display();
}
(How to use interface, see Implementing Interface)
abstract class
In life, we have some very abstract concepts. These abstract concepts are often a collection of many classes, such as:
1. Grain (can be corn, wheat, rice)
2. Graphics (can be triangles, circles, squares)
For another example, we gave an example before:
1. Human beings (can be men or women)
When organizing such relationships, we can use inheritance, such as:
According to our common sense:
1. The term "object of Food class" is abstract. Such an object should belong to one of the Corn, Rice, and Wheat subclasses.
2. The Food class has the eat() method (food can be eaten). However, such an action is abstract. The specific ways of eating food are different. For example, corn needs to be peeled and eaten, and wheat needs to be ground into flour. We need to override the eat() method of Food class in every class.
abstract and concrete
Java provides the syntax of abstract class to illustrate the abstraction of classes and their methods. for example:
Copy the code code as follows:
abstract class Food {
public abstract void eat();
public void happyFood();
{
System.out.println("Good! Eat Me!");
}
}
Methods in a class can be declared abstract, such as eat() above. At this time, we do not need to specifically define the method, we only need to provide the prototype of the method. This is similar to interfaces. When we inherit this class in, for example, the Corn class, we need to provide a specific definition of the eat() method.
Another method in the class happyFood() is not
When an abstract method appears in a class, the abstract keyword must be added to the declaration of this class, otherwise Java will report an error. An abstract class cannot be used to create objects.
Inheritance of abstract classes
We can inherit an abstract class just like a class. We must use complete method definitions to cover the abstract methods in the abstract class, otherwise, the derived class is still an abstract class.
Abstract classes can have data members in their definition. Inheritance of data members is the same as that of a normal class.
Summarize
interface inheritance, multiple inheritance
abstract method, abstract class