Encapsulation, inheritance, method rewriting, polymorphism

encapsulation

  • The dew that should be exposed, the hide that should be hidden
  • Encapsulation (data hiding)
  • Property private, get/set
public class Student {

    //Property private
    private String name;//name
    private int id;//Student number
    private  char sex;//Gender

    //method
    //Provide some methods that can operate this property!
    //Provide some public get and set methods

    //get gets this data
    public String getName(){
        return this.name;
    }
    //set sets a value for this data
    public void setName(String name){
        this.name = name;
    }
    //alt+inset quick get,set
}

significance:

  1. Improve program security and protect data
  2. Hidden code implementation details
  3. Unified interface
  4. System maintainability

The same method name and the same parameter list are the same method

inherit

Two classes of inheritance relationship: (the subclass inherits and the parent class is represented by the keyword extends)

  • Subclass (derived class)
  • Parent class (base class)

JAVA classes only have single inheritance, not multiple inheritance!

  1. A subclass can inherit all the methods of the parent class
  2. If the parent class is private, the child class cannot inherit
  3. Ctrl+h open tree structure
  4. In JAVA, all classes inherit Object by default

example:

//Person: parent class
public class Person {
    public void say(){
        System.out.println("ha");
    }
    private int money= 10_0000_0000;

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}

//Student: subclass, derived class
public class Student extends Person{

}

//Call subclass
public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.say();
        student.getMoney();
        System.out.println(student.getMoney());
    }
}

Example of logical relationship between super,this and parameters

//Person: parent class
public class Person {
    protected String name = "hu";

}

//Student: subclass, derived class
public class Student extends Person{
    private String name = "xiao";
    public void test(String name){
        System.out.println(name);//Hu
        System.out.println(this.name);//This name is "xiao" in the subclass
        System.out.println(super.name );//The name is "hu" in the parent class
    }

}

//call
public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.test("Hu");
    }
}

Subclass has hidden code: call the parameterless construction of the parent class

Calling the constructor of the parent class must be on the first line of the subclass constructor

super note

  1. When super calls the constructor of the parent class, it must be in the first of the constructor
  2. super must only appear in subclass methods or constructor methods
  3. super and this cannot call constructor
differencesuperthis
The objects represented are differentRepresents the application of the parent objectCall this object by itself
premiseCan only be used in inheritance conditionsCan be used without inheritance
Construction methodConstruction of parent classConstruction of this class

override method

The subclass overrides the method of the parent class

Static method

//Parent class
public class A {
    public static void test(){
        System.out.println("A=>test()");
    }
}

//Subclass
public class B extends A{

    public static void test(){
        System.out.println("B=>test()");
    }
}

//Static method call
public class Application {
    public static void main(String[] args) {
       
        //Method is only related to the data type defined on the left
        B a = new B();
        a.test();//B=>test()
        
        //A reference to a parent class points to a child class
        A b = new B();
        b.test();//A=>test()
        
        //Memory analysis: static is loaded together with classes, and all reference variables can be called. This is why when a defines B, it directly calls the static method of A.
        //Details will be explained in the following sections
    }
}

Overrides can only be used with non static methods

Non static method

//Parent class
public class A {
    public  void test(){
        System.out.println("A=>test()");
    }
}

//Subclass
public class B extends A{

    //rewrite
    //Shortcut key: Alt+Insert:override
    @Override//Annotation, functional annotation!
    public void test() {
        System.out.println("B=>test()");
    }
}

//Non static method call
public class Application {
    public static void main(String[] args) {
        B a = new B();
        a.test();//B=>test()
        
        A b = new B();//The subclass overrides the method of the parent class
        b.test();//B=>test()
        
        /**
        *That is, B is the object from B new, so the method of B is called
		*Because static methods are methods of classes, not static methods of objects
		*When there is static, b calls the method of class A, because b is defined with class A
		*When there is no static, B calls the method of the object, and B uses class B new
		*/
    }
}

Summary

Override: inheritance relationship is required. The subclass overrides the method of the parent class.

  1. Method names must be the same
  2. The parameter list must be the same
  3. Modifier: the scope can be expanded public > protected > Default > private
  4. Exception thrown: the scope can be reduced, but not expanded.

When overridden, the methods of the child class and the parent class must be consistent, and the method bodies are different.

Reason for rewriting:

  1. The functions of the parent class and the child class are not necessarily required or satisfied

polymorphic

//Parent class
public class Persion {
    public void run(){
        System.out.println("run");
    }
}

//Subclass
public class Student extends Persion{
    @Override
    public void run() {
        System.out.println("son");
    }
    public void eat(){
        System.out.println("eat");
    }
}

//call
public class Application {
    public static void main(String[] args) {

        //The type of reference that can be pointed to is uncertain: the reference of the parent class points to the child class

        //The methods that students can call are their own or inherit the parent class
        Student s1 = new Student();
        //The parent type of person can point to subclasses, but cannot call methods unique to subclasses
        Persion s2 = new Student();

        //The methods that can be executed by an object mainly depend on the type on the left side of the object, which has little to do with the right side
        s1.run();
        s1.eat();
        s2.run();
    }
}

Polymorphic considerations

  1. Polymorphism is the polymorphism of methods, and there is no polymorphism of attributes
  2. Parent and child classes are related
  3. Existence conditions: inheritance relationship, method needs to be overridden, and parent class reference points to child class object

What cannot be rewritten are

  1. static method, which belongs to class, does not belong to instance
  2. final constant
  3. Private method, private

Added by keith73 on Mon, 20 Dec 2021 05:08:10 +0200