Java is an object-oriented language. Java Object-oriented has three main features: encapsulation, inheritance, and polymorphism.
Encapsulation: Encapsulate some attributes and methods into a class.
Inheritance: For example, subclass inherits some attributes and methods of the parent class.
Polymorphism: For example, a parent class has multiple sub-classes with different characteristics.
Here I will not explain much. Next I will mainly explain an inheritance. Inheritance is a feature of OOP. Java only supports single inheritance (if two parent classes with the same method are inherited, they do not know which parent class to inherit from, so Java only supports single inheritance ). Inheritance is a special feature of Java. All classes we use inherit the objict class, so we need methods of the object class, such as tostring (), getclass (), wait ()...... Therefore, all classes we create have parent classes.
Java generally has three types:
Basic Class: A common class (generally called a basic class) is an object template and a set of attributes and methods. It can inherit other basic classes, abstract classes, and implementation interfaces.
Abstract class: Class with an abstract method (an abstract method must be implemented by inheritance. It is defined only and not implemented ). Abstract classes can have one or more abstract methods. They are excessive basic classes and interface classes.
Interface Class: It is generally called an interface. All methods in this class are abstract methods. The methods of this class are defined and not implemented.
Abstract classes and interfaces are defined by [abstract class + abstract class name] and [interface + Interface Name]. All methods in interfaces are abstract methods, some methods of abstract classes are self-implemented, and some of them are only defined and not implemented.
Definition of basic classes:
Public ClassFruit {
Public VoidPrice (){
System. Out. println ("fruit price ");
}
Public VoidWeight (){
System. Out. println ("fruit weight ");
}
}
Abstract class definition:
Public Abstract ClassFruit {
Public VoidPrice (){
System. Out. println ("fruit price ");
}
Public Abstract VoidWeight ();
}
Interface Class Definition:
Public InterfaceFruit {
Public VoidPrice ();
Public VoidWeight ();
}
From the above we can see that the interface is an upgraded version of the abstract class, because the methods of this class are all abstract methods, so change abstract to interface. The method of the interface must be implemented by subclass.
Inheritance
Inherit basic classes. You can inherit the methods of the parent class, or expand them. The following are the classes that implement basic interfaces:
Public ClassAppleExtendsFruit {
}
This class has two methods in the basic fruit class: price () and weight ();
Inherit abstract classes. abstract methods of abstract classes must be implemented. Methods of parent classes and adding methods can be modified. The following are classes that inherit abstract classes:
Public ClassAppleExtendsFruit {
@ Override
Public VoidWeight (){
System. Out. println ("fruit weight ");
}
}
This class inherits the price () method of the fruit class and implements the weight () method of the fruit abstract class.
Inherit the Interface Class (also implement the interface), must implement all abstract classes of the interface class and Add. The following are the classes that inherit interfaces:
Public ClassAPLEImplementsFruit {
@ Override
Public VoidPrice (){
System. Out. println ("fruit price ");
}
@ Override
Public VoidWeight (){
System. Out. println ("fruit weight ");
}
}
This class is an abstract method that implements the fruit interface.
Java only supports single inheritance (inheriting basic classes and abstract classes), but we can implement it using interfaces (multiple inheritance interfaces)
For example:Public ClassAppleExtendsFruitImplementsFruit1, fruit2 {}
Generally, we use the extends keyword to inherit basic classes and abstract classes, and use the implements keyword to inherit interface classes. In fact, inheritance is very simple. It can be that we did not find out the two keywords. When we find out, it is relatively simple.
Interfaces can also inherit interfaces such:Public InterfaceFruit1ExtendsFruit {} is the multi-inheritance in the interface, and the same is true for abstract classes and basic classes. If we add the basic classFinalThis defines that the class is not inherited, and the class cannot be the parent class. At the same time, the methods of basic classes can be modified using public, private, and proptected, and final can be used to prevent inheritance of this method.
This is just your own understanding. In some places, you may not use the words properly, but you may think that the words are better understood. In some cases, it is not the key point here and I do not make it clear. I hope you can learn it in other ways.