java object-oriented-inheritance

Java Object Oriented

Author q: 2835916127

1. Inheritance

1. Implementation of Inheritance

  • Concept of inheritance:

    • Inheritance is one of the three major characteristics of object-oriented. Inheritance can make the subclass have the attributes and methods of the parent class, redefine it in the subclass, and add new methods of the subclass
  • Implement inherited format

格式 : class 子类 extends 父类{
    
     }
  • For example:
class Dog extends Animal {
    
     }

I will not give a detailed example here

  • Benefits of inheritance:

    • Improved code reusability (the attributes of multiple classes only need to be declared in the base class)
    • Improve the maintainability of the code (when you need to modify, you only need to modify one point)
  • Disadvantages of inheritance

    • Inheritance creates a relationship between the subclass and the parent class, and the coupling of the class is enhanced. When the parent class needs to be changed, the subclass has to make changes, which weakens the independence of the subclass
  • Use of inheritance

    • To use inheritance, you need to consider whether there is an is...a relationship between the class and the class, and you cannot use inheritance blindly
      • The relationship of is...a: who is a kind of who, for example: teacher and student are a kind of human, that person is the parent, and student and teacher are the sub-categories

2. Inherited access characteristics

Variable access characteristics

  • To access a variable in a subclass, the principle of proximity is adopted
  1. Find in the local scope of the subclass
  2. Find in the scope of the members of the subclass
  3. Find in the member method of the parent class
  4. Report an error if none of the three are found (the father’s father is not considered)

Access characteristics of the object

  • create

this and super

  • this: a reference to the current class object

  • super: reference to the parent object

  • Constructor:

    • this (parameter 1, parameter 2...)
    • super(parameter 1, parameter 2...)

Note: The super() constructor of the parent class is called by default when the subclass is created

  1. When creating a subclass object, if you don’t explicitly call the parent class’s constructor, the super() method of the parent class will be called by default
  2. If you need to call the constructor of the parent class, you must put super (parameter 1, parameter 2...) in the first sentence of the subclass constructor
  3. It is not possible to call the parent class structure multiple times for one subclass.
    Recommended writing: Bring the default structure when creating the class

Method rewriting:

  • Overriding concept: the same method declaration as the parent class is written in the subclass, that is, the method name and parameter type are the same
  • Application scenario: When the subclass needs the functions of the parent class, and a method of the subclass has its own unique method, you can override the parent class method at this time
  • You can use the annotation @Override to verify whether the method declared in the subclass comes from the parent class, and play a [verification] function
    note:
    1. Private methods of the parent class cannot be overridden (private members and methods of the parent class cannot be inherited by subclasses)
    2. The access permissions of the subclass cannot be lower than the parent class (public> protected> default (no write permission)> private)

public class Fu {
    
    
    private void show() {
    
    
        System.out.println("Fu中show()方法被调用");
    }

    void method() {
    
    
        System.out.println("Fu中method()方法被调用");
    }
}

public class Zi extends Fu {
    
    

    /* 编译【出错】,子类不能重写父类私有的方法*/
    @Override
    private void show() {
    
    
        System.out.println("Zi中show()方法被调用");
    }
   
    /* 编译【出错】,子类重写父类方法的时候,访问权限需要大于等于父类 */
    @Override
    private void method() {
    
    
        System.out.println("Zi中method()方法被调用");
    }

    /* 编译【通过】,子类重写父类方法的时候,访问权限需要大于等于父类 */
    @Override
    public void method() {
    
    
        System.out.println("Zi中method()方法被调用");
    }
}

Precautions:

  • Java only supports single inheritance (a father with multiple sons)

  • Classes in java support multi-layer inheritance

  • Pay attention to the difference between rewriting and overloading

    • Rewrite: Subclass overrides the method of the parent class
    • Overloading: In the same class, the method name is the same, but the number or types of parameters are different

final

  • Final represents the final meaning, which can be member methods, member variables, and classes

    • final modified class
    • Final modified class: this class cannot be inherited (cannot have subclasses, but can have parent classes)
    • Final modified member method: this method cannot be overridden
    • Final modified member variable: indicates that the variable is a constant and cannot be reassigned
  • final modification of basic data types

    • Final modified basic data type, its value cannot be changed
    • Final modified reference type: the address value of the reference type cannot be changed, but the content in the address can be changed
  • For example

public static void main(String[] args){
    
    
    final Student s = new Student(23);
  	s = new Student(24);  // 错误
 	s.setAge(24);  // 正确
}

static

  • The concept: static is static meaning, can be modified member methods and member variables

  • Features:

    • Shared by all objects of the class, this is also a condition for judging the use of static
    • Can be called by the class name ( recommended )
      static access characteristics
  • Non-static member method
    • Can access static member variables
    • Can access non-static member variables
    • Can access static member methods
    • Can access non-static member methods
  • Static member method
    • Can access static member variables
    • Can access static member methods
  • Summarized in one sentence:
    • Static member methods can only access static members

Guess you like

Origin blog.csdn.net/weixin_42143994/article/details/109864102