JAVA inherits basic classes, abstract classes, interfaces, and java Abstraction
Encapsulation: encapsulate some attributes and methods into a class.
Inheritance: a 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 general class (generally referred to as 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 abstract methods (abstract methods are defined and not implemented by inheritance ). 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:
Copy codeThe Code is as follows:
Public class Fruit {
Public void price (){
System. out. println ("fruit price ");
}
Public void weight (){
System. out. println ("fruit weight ");
}
}
Abstract class definition:
Copy codeThe Code is as follows:
Public abstract class Fruit {
Public void price (){
System. out. println ("fruit price ");
}
Public abstract void weight ();
}
Interface Class Definition:
Copy codeThe Code is as follows:
Public interface Fruit {
Public void price ();
Public void weight ();
}
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:
Copy codeThe Code is as follows:
Public class Apple extends Fruit {
}
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:
Copy codeThe Code is as follows:
Public class Apple extends Fruit {
@ Override
Public void weight (){
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:
Copy codeThe Code is as follows:
Public class Aple implements Fruit {
@ Override
Public void price (){
System. out. println ("fruit price ");
}
@ Override
Public void weight (){
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 class Apple extends Fruit implements Fruit1, 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.
The interface can also inherit interfaces such as: public interface Fruit1 extends Fruit {}. This is the multi-inheritance in the interface, and the same is true for abstract classes and basic classes. If we add final modification to the basic class, it defines that the class is not inherited and cannot be used as 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.