Java Object Oriented 04 (Inheritance)

inherit

The essence of integration is to abstract a certain batch of classes, so as to achieve better modeling of the real world

Extends means "extend". The child class is an extension of the parent class.

Classes in JAVA have only single inheritance, no multiple inheritance!

Inheritance is a relationship between class and class. In addition, there are dependencies, combinations, aggregations, etc. in the relationship between classes and classes.
Two classes of inheritance relationship, one is a subclass (9 derived classes), and the other is a parent class (base class). The child class inherits the parent class and is represented by the keyword extends.
There should be an "is a" relationship between the child class and the parent class in a sense

Key points:

object class
super-this
method override

All classes in Java inherit Object class super by default
Insert picture description here

Call the parent class’s construction method or variable
call the parent class’s variable code example
Insert picture description here
output example
Insert picture description here
Call the parent class’s construction method code example
Insert picture description here
output example When the
Insert picture description here
subclass no-parameter construction is called, the parent’s no-parameter construction will be called first

Code sample
Insert picture description hereoutput example
Insert picture description here

When calling the subclass with parameter structure, the parent class without parameter structure will be called first, code example:
Insert picture description here
output example
Insert picture description here

The actual code in public student is

public Student() {
        super();//隐藏代码,调用了父类无参构造器,且必须要在子类构造器的第一行
        System.out.println("子类无参构造器被调用了" );
    }

Super attention points
1. Super calls the parent class's constructor (parameterless constructor or parameter constructor), which must be the first of the constructor
2. Super must only appear in the method or construction method of the subclass
3. super and this cannot call the constructor at the same time

Vs this

The objects represented are different:
this: the object of the caller
super: the reference
premise of the parent class object
this:
supe can also be used without inheritance : the
construction method
this() can only be used under inheritance conditions ; the construction of this class
super() ; The structure of the parent class


Example output of method rewriting static method code
Insert picture description here
example:
Insert picture description here

Non-static method code example:
Insert picture description here
output example
Insert picture description here
summary:
static method is a method of a class, non-static method is a method of object
rewriting: need to have inheritance relationship, subclass rewrite the parent class method
1. The method name must be the same
2. The parameter list must Same
3. Modifier: the scope can be enlarged: public>Protected>Default>private
4. The exception thrown: the scope can be reduced, but cannot be expanded

Why it needs to be rewritten:
1. The function of the parent class, the subclass does not necessarily need it, or does not necessarily satisfy the
shortcut key:
ALT+INSERT: select override

Guess you like

Origin blog.csdn.net/qq_51224492/article/details/113795348