Java Foundation Review Day11

Java Foundation Review Day11

1. Inheritance

1.1 Inheritance mainly solves the problem of common extraction and avoiding code reuse.

Parent class: mainly used to define common attributes, methods

Subclasses: Common attributes and methods are used directly from the parent without having to be redefined. They also have their own unique attributes and methods

Relationship between parent and child: A child (dog) is a parent (animal) and is-a. Keywords:extend

1.2 Access rules for member variables of subclasses and member variables of parent classes

In the inheritance relationship of a parent-child class, if the member variable of the child class and the member variable of the parent class have the same name, the access rules are as follows:

  1. Direct: through subclass objects. Member variable (class object name = new subclass ();) At this point, who is on the left side of the equal sign, that is, who accesses the member variables first, and if not, looks up
  2. Indirect: Access member variables through the member method: the member method belongs to whomever member variable is used, otherwise look up

1.3 Question of subclass methods accessing renamed variables when local variables, subclass member variables, and parent member variables are renamed

Local variable: Direct call within method

Subclass member variable: method is called with the keyword this

Parent member variable: method is called with the keyword super

//Parent Class
public class BasicDemo {
    int num =100;
}
//Subclass
public class DerivedDemo extends BasicDemo {
    int num = 200;
    //Subclass methods access duplicate-name variables
    public void showVar(){
        int num = 300;
        System.out.println(num);//300
        System.out.println(this.num);//200
        System.out.println(super.num);//100
    }
}
public static void main(String[] args) {
        DerivedDemo demo = new DerivedDemo();
        System.out.println(demo.num);//To the left of the equal sign is the subclass, so num is a member variable of the subclass, and the output is 200
        demo.showVar();// 300 200 100
        BasicDemo demo2 = new DerivedDemo();
        System.out.println(demo2.num);//To the left of the equal sign is the parent class, so it is a member variable of the parent class, a polymorphic relationship, so the output is: 100
    }

1.4 When member methods of the parent subclass are renamed:

Access Rule: See who is new calls whose method first, and if not, look up.

//Parent Class
public class BasicDemo {
    int num =100;
    void method(){
        System.out.println("The parent rename method executes.");
    }
}
//Subclass
public class DerivedDemo extends BasicDemo {
    void method() {
        System.out.println("The subclass method with the same name executes.");
    }
}
//main method
public static void main(String[] args) {
        DerivedDemo demo = new DerivedDemo();
        demo.method();//The subclass method with the same name executes.
        BasicDemo demo2 = new DerivedDemo();
        demo2.method();//The subclass method with the same name executes. Newis a subclass so call the subclass method
        BasicDemo demo3 = new BasicDemo();
        demo3.method();//The parent rename method executes.
    }

Override of the 1.5 method

  • Concept: In an inheritance relationship, the name of the method is the same, and the list of parameters is the same (override, override)

  • Matters needing attention:

    1. The method names and parameter lists between parent and child classes must be the same

      You can use the @override annotation in front of the method to detect whether it is valid to override correctly, and it is recommended that you write it

    2. The return value of a subclass method must be less than or equal to the return value type of the parent class

      _Object class is the highest common parent of all classes

      //Parent Class
      public class BasicDemo {
          String method(){
              return null;
          }
      }
      //Subclass 
      public class DerivedDemo extends BasicDemo {
          @Override
          Object method() {//If you use Object as the return value, the return value type is greater than String, so it doesn't matter if the error is reversed
              //'method()' in 'com.kou.oop.demo04.DerivedDemo' clashes with 'method()' in 'com.kou.oop.demo04.BasicDemo'; attempting to use incompatible return type
             return null;
          }
      }
      
    3. The permission modifier of a subclass method must be greater than or equal to the permission modifier of the parent class

      _Privilege size relationship: public > protected > (default) > private

      default: Not a keyword, nothing is written in front of the method, leave blank

      //Parent Class
      public class BasicDemo {
          public String method(){
              return null;
          }
      }
      //Subclass
      public class DerivedDemo extends BasicDemo {
          @Override
          String method() {//If nothing is written, leave blank is in (default) format, access is less than public so error occurs
              //'method()' in 'com.kou.oop.demo04.DerivedDemo' clashes with 'method()' in 'com.kou.oop.demo04.BasicDemo'; attempting to assign weaker access privileges ('package-private'); was 'public'
             return null;
          }
      }
      

1.6. Design principles to note when inheriting

  • For classes that are already in use, try not to modify them. Instead, recommend defining a new class that reuses common content and adds new modifications

  • So you'll find that each overridden method has a default super. Parent method (); To reuse the parent class's methods and add new modifications to them

1.7. Access characteristics of construction methods when inheriting

  • There will be a default super() method call in the subclass construction method, and the compiler will give a super() if nothing is written; So it must be the parent parameterless constructor that was called first

    //Parent Class
    public class BasicDemo {
        int num = 300;
        public BasicDemo(){
            System.out.println("Parent parameterless construction execution~~~");
        }
    }
    //Subclass
    public class DerivedDemo extends BasicDemo {
        public DerivedDemo() {
            System.out.println("Subclass parameterless construction execution----");
        }
    }
    //Main method
     public static void main(String[] args) {
            DerivedDemo derivedDemo = new DerivedDemo();
            //Parent parameterless construction execution ~~
            //Subclass parameterless construction execution--
        }
    
  • In the construction method of a subclass, the super keyword can be used to invoke the construction method of a parent class overload. (Only subclass constructors can call parent constructors, not ordinary member methods)

    //Parent Class
    public class BasicDemo {
        int num = 300;
        public BasicDemo(){
            System.out.println("Parent parameterless construction execution~~~");
        }
    
        public BasicDemo(int num) {
            this.num = num;
            System.out.println("Parent class parametric construction execution~~~" + num);
        }
    }
    //Subclass
    public class DerivedDemo extends BasicDemo {
        int num2;
        public DerivedDemo() {
            super(100);
            System.out.println("Subclass parameterless construction execution----");
        }
        public DerivedDemo(int num2){
            this.num2 = num2;
            System.out.println("Subclass has parametric construction execution----" +this.num2);
        }
    }
    //main method
    public static void main(String[] args) {
            DerivedDemo derivedDemo = new DerivedDemo(300);
            //Parent parameterless construction execution ~~
        	//Subclass parametric construction execution - --- 300
            DerivedDemo derivedDemo1 = new DerivedDemo();
        	//Parent parametric construction execution ~~100
        	//Subclass parameterless construction execution--
        }
    
  • Super's parent construction call must be the first statement of the child class construction method. (that is, only one super can be called in the construction method of a subclass, and it must be the first)

Three uses of the 1.8 super keyword

  1. In member methods of subclasses, access member variables of the parent class. super. Parent member variable name
  2. In member methods of subclasses, access member methods of the parent class. super. Parent member method name (parameter)
  3. In the construction method of the subclass, access the construction method of the parent class. Super (parameter)

Usage of the 1.9 this keyword

  • Role: The super keyword is used to access parent methods, while the this keyword is used to access this class of methods
  • Usage:
    1. Accessing member variables of this class in its member methods
    2. In the member methods of this class, access another member method
    3. In the construction method of this class, access another construction method
      • When using this in a construction method, this must also be the first statement. (There can only be one super () or this () at this time, even if this is the first one, the default super will send oh, but it can't be displayed on the first line)
//Parent Class
public class BasicDemo {
    int num = 300;

    public BasicDemo() {
        System.out.println("Parent class parameterless construction execution.");
    }
}
//Subclass
public class DerivedDemo extends BasicDemo {
    int num;

    public DerivedDemo(int num) {
        this.num = num;
        System.out.println("Subclass has parametric construction--" +this.num);
    }

    public DerivedDemo() {
        this(100);
    }

    public void method(int num){
        System.out.println(num);
        System.out.println(this.num);
        System.out.println(super.num);
        this.method1();
    }
    public void method1(){
        System.out.println("Another member method");
    }
}
//main method
public static void main(String[] args) {
        DerivedDemo demo = new DerivedDemo();
        //Parent class parameterless construction execution.
        //Subclasses have parametric constructs--100
    }

1.10 super and this memory diagrams

Pictures from an online course for learning purposes only, please contact for deletion of infringement

1.11 Features of Java encapsulation

  1. The Java language is single inherited. A direct parent of a class can only have one
  2. The Java language is multilevel inherited.
  3. The direct parent of a subclass is unique, but a parent can have multiple subclasses. (That is, this subcategory has many brothers and sisters)
  • Pictures from an online course for learning purposes only, please contact for deletion of infringement

Tags: Java jvm programming language

Posted by cgraz on Wed, 20 Jul 2022 01:14:07 +0530