Understanding inheritance and polymorphism in java

The concept of inheritance

Inheritance is a cornerstone of Java object-oriented programming techniques because it allows the creation of hierarchical classes.

Inheritance means that the subclass inherits the characteristics and behavior of the parent class, so that the subclass object (instance) has the instance domain and methods of the parent class, or the subclass inherits the methods from the parent class, so that the subclass has the same behavior as the parent class.

class inheritance format

In Java, the extends keyword can be used to declare that a class is inherited from another class. The general form is as follows:

class inheritance format

class parent class { } class child class extends parent class { }
First, let's discuss why we need inheritance? ? ? ?
First of all, to inherit the popular vernacular is to extract the common features.  
Inherited Features
1. The subclass has non-private properties and methods of the parent class
2. Subclasses can have their own properties and methods, that is, subclasses can extend the parent class
3. Subclasses can implement the methods of the superclass in their own way
4. A subclass can only have one parent class, and a parent class can have multiple inheritances

Inherit keyword

Inheritance can use the keywords extends and implements to achieve inheritance and all classes are mostly inherited from java. lang.Object When a class does not inherit two keywords, it inherits object by default (this class is in the java.lang  package, so there is no need to  import )

final keyword

Don't let its class have subclasses

polymorphism

It is a common method, a common class, and a method with the same name and the same parameter and the same form can constitute polymorphism.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325291121&siteId=291194637
Recommended