Object-oriented Inheritance
What is inheritance?
Inheritance means that a class gets the member variables and member methods of another class. Java only supports single inheritance. A subclass can only inherit one parent class, and many inheritance are not allowed. Inheritance is used to reduce repeated code during class definition.
Basic syntax features of inheritance:
Class subclass extends parent class
Class Person {// parent class String name; int age; void introduce () {System. out. println ("My name is" + name + ", my age is" + age );}}
Class Student extends Person {// inherit the parent class Person public static void main (String args []) {Student s1 = new Student (); s1.name = "James"; s1.age = 18; s1.introduce ();}}
The subclass Student inherits the parent class Person and has the member variables and member methods of the parent class. At the same time, it can define the member variables and member functions unique to the subclass.
Object-oriented three basic features encapsulation inheritance Polymorphism
Encapsulation, inheritance, and Polymorphism
It is mainly a class.
Encapsulation is the privatization of classes.
C ++
For example
Class time
{
Public:
Private:
Int hour; // This is the encapsulation, which is the private variable of the class.
};
Inheritance is to keep the attributes of the parent class and expand new things.
Class land_vehicle: public time {
In this way, the parent class time is inherited.
Polymorphism
Polymorphism is a technique that allows the Parent object to be set to be equal to one or more of its sub-objects, such as Parent: = Child;
Object-oriented Inheritance
This is not the case, bro. You need to make your own brains and respect the people who help you solve the problem.