Principle and Characteristics of Java Object Oriented Inheritance
- 2021-12-04 10:06:32
- OfStack
1. Preface
I also talked about the related encapsulation before, and now we first understand the concept and use of inheritance.
2. Succession
What is inheritance?
Inheritance is not uncommon in display life, such as inheriting property and so on, and it is also used in our java study.
The inheritor is called a subclass or a derived class, the inheritor is called a parent class, a base class or a superclass, and the objec class is the parent class of all classes
(Later introduction)
Advantages and disadvantages of inheritance
Benefits: It is to improve the maintainability of the code (multiple codes need to be modified, only need to repair the 1 can).
Improved code reusability (multiple identical members can be used in the same class)
Disadvantages: The disadvantage of inheritance is that it makes the coupling between codes high, and modifying the subclasses of the parent class will also change accordingly
Inherited usage scenarios?
Two are subordinate, such as cats and animals, students and people.
The inherited keyword is extends.
Inherited format:
public class Subclass Name extends Parent Class Name {}
Examples:
public class Cat extends Animal{}// Cats inherit animals
Characteristics of inheritance:
Subclasses can have non-private methods and member variables of the parent class, or override non-private (private-modified) methods of the parent class. Methods of all subclasses access the parameterless construction of the parent class by default
The concept of rewriting:
Override means that a subclass restates a non-private method in the parent class. The general characteristics of override are the same method name, the same format, the same return type and different method body
Such as:
In the fu class:
public class fu{
public void play(){
System.out.println("fu Like playing badminton ");
}
}
In the zi class:
public class zi extends fu{
public void play(){// Method override of parent class
System.out.println("zi Like playing basketball ");
}
}
Alternatively, access requires that the zi class be greater than or equal to the fu class
Decorations in the fu class are the default:
public class fu{
void eat(){
System.out.println("fu Medium eat Method ");
}
}
In the zi class:
public class Zi extends Fu {
@Override// Correctness of rewriting of detection method
public void eat() {
System.out.println("zi In eat Method ");
}
}
In the zi class, override:
public class Zi extends Fu {
// @Override It doesn't matter whether you write or not, this only plays an auxiliary role
void eat() {
System.out.println("zi In eat Method ");
}
}
zi can also:
public class Zi extends Fu {
@Override
protected void eat() {
System.out.println("zi In eat Method ");
}
}
Modified access relationship: public > protected > Default (do not write) > private
When the permission modifier of the parent class is default (that is, do not write the modifier), the modifier method overridden by the subclass can be default and
Default before and so on. Note: When the permission modifier of the parent class is private, the subclass is not private
Before. Subclasses cannot override such methods when the parent class is decorated with private.
super Keyword
When the member variable in the child class has the same name as the member variable in the parent class and we want to use the member variable in the parent class,
Or when we override the method in the parent class and we still want to call the method in the parent class. At this point, we will use super
Keyword to call members in the parent class.
Comparison of super and this
super and this use the same principle, this is to solve the local variable and member variable with the same name, local variable pair
Member override super is to solve the override of subclass to parent class.
When local variables, member variables and variables in the parent class have the same name, local variables are accessed. It can be changed with this and super.
public class fu{
public int age=40;
public fu(){}// Parametric structure
public fu(int age){// Parametric structure
this.age=age;}
public void eat(){
System.out.println("fu Medium eat Method ");
}
}
public class Zi extends Fu {
public int age=18;
public Zi(){}
public Zi(int age){
this.age=age;}
public void eat() {
super.eat();
System.out.println("zi In eat Method ");
}
public void show(){
int age=1;
System.out.println(age);//1 Values of local variables
System.out.println(this.age);//18 Values in subclass members
System.out.println(super.age);//40 Values in the parent class
}
public class Demo{
public static void main(String[] args){
zi z=new zi();
z.eat();
z.show();
}
}
Let's continue in the next article! !