Encapsulation inheritance polymorphism

encapsulation

  • The dew that should be exposed, the hide that should be hidden
    • Our program design should pursue "high cohesion and low coupling". High cohesion means that the internal data operation details of the class are completed by ourselves, and external interference is not allowed; low coupling: only a small number of methods are exposed for external use.
  • Encapsulation (data hiding)
    • Generally, direct access to the actual representation of data in an object should be prohibited, but should be accessed through an interface, which is called information hiding.
  • It's enough to remember this sentence: property private, get/set
package com.oop.demo04;

//Class private: private
public class Student {

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


    //Provide some methods that can operate this property!
    //Some public get and set methods are provided

    //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 + insert

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age>120 || age<0){//wrongful
            this.age = 3;
        }else {
            this.age = age;
        }
    }
}
package com.oop;

import com.oop.demo04.Student;
/*
    1. Improve program security and protect data
    2. Implementation details of hidden code
    3. Unified interface
    4. System maintainability increased
 */
public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();

        s1.setName("think of source");

        System.out.println(s1.getName());

        s1.setAge(999);//wrongful
        System.out.println(s1.getAge());
    }

}

To judge whether the two methods of a class are the same, we mainly refer to two things: method name and parameter list

inherit

  • The essence of inheritance is to abstract a group of classes, so as to realize better modeling of the real world.
  • Extensions means "extension". A subclass is an extension of a parent class.
  • JAVA classes only have single inheritance, not multiple inheritance!
  • Inheritance is a kind of relationship between classes. In addition, the relationship between classes includes dependency, composition, aggregation and so on.
  • Two classes of inheritance relationship, one is the subclass (derived class) and the other is the parent class (base class). The subclass inherits the parent class and is represented by the keyword extends.
  • In a sense, there should be a "is a" relationship between a child class and a parent class.
package com.oop;

import com.oop.demo05.Person;

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


        Person person = new Person();

    }
}
package com.oop.demo05;

//In Java, all classes inherit object directly or indirectly by default
//Person: parent class
public class Person /*extends object*/{

    //public
    //protected
    //default
    //private
    private int money = 10_0000_0000;

    public void say(){
        System.out.println("Said a word");
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}
package com.oop.demo05;

//Teacher is: derived class, subclass
public class Teacher extends Person{
}
package com.oop.demo05;

//Student is person: derived class, subclass
//If a subclass inherits from the parent class, it will inherit all the methods of the parent class!
public class Student extends Person{

    //Ctrl + H
}
  • Object (in Java, all classes inherit object directly or indirectly by default)
  • super

super note:

  1. super calls the constructor of the parent class, which must be in the first instance of the constructor
  2. super must only appear in subclass methods or constructor methods!
  3. super and this cannot call constructor at the same time!

Vs this:

​ The objects represented are different:

​ This: itself calls this object

​ super: represents the application of the parent object

​ premise

​ this: can be used without inheritance

​ super: represents the application of the parent object

​ Construction method

​ this(): the construction of this class

​ super(): Construction of parent class!

package com.oop;

import com.oop.demo05.Student;

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


        Student student = new Student();
        //student.test("Siyuan");
        //student.test1();

    }
}
package com.oop.demo05;

//Student is person: derived class, subclass
//If a subclass inherits from the parent class, it will inherit all the methods of the parent class!
public class Student extends Person{

    public Student() {
        //Hidden code: the parameterless construction of the parent class is called
        super("name");//Calling the father's constructor must be on the first line of the subclass constructor
        System.out.println("Student No parameter execution");
    }

    private String name = "siyuan";

    public void print(){
        System.out.println("Student");
    }
    public void test1(){
        print();//Student
        this.print();//Student
        super.print();//Person
    }
    public void test(String name){
        System.out.println(name);//think of source
        System.out.println(this.name);//siyuan
        System.out.println(super.name);//lin
    }
}
package com.oop.demo05;

//In Java, all classes inherit object directly or indirectly by default
//Person: parent class
public class Person /*extends object*/{

    public Person(String name){
        System.out.println("Person No parameter execution");
    }

    protected String name = "lin";

    //Private things cannot be inherited!
    public void print(){
        System.out.println("Person");
    }
}
  • Method Rewriting: emphasis -- > polymorphism
package com.oop;

import com.oop.demo05.A;
import com.oop.demo05.B;

public class Application {

    //Static methods and non static methods are very different!
        //Static method: the method call is only related to the data type defined on the left

        //Non static: overriding
    public static void main(String[] args) {


        A a = new A();
        a.test();//A

        //A reference to a parent class points to a child class
        B b = new A();//The subclass overrides the method of the parent class
        b.test();//B

    }
}
package com.oop.demo05;

//inherit
public class A extends B{

    //Override override
    @Override//Annotation: functional annotation!
    public void test() {
        System.out.println("A=>test");
    }
}
package com.oop.demo05;

//Rewriting is the rewriting of methods and has nothing to do with properties
public class B {
    public void test(){
        System.out.println("B=>test");
    }
}

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, but not reduced: public > protect > Default > private
  4. Exception thrown: the scope can be narrowed but not expanded: classnotfoundexception -- > exception (large)

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

Why rewrite:

​ The function of the parent class and the subclass are not necessarily required or satisfied!

​ Alt + Insert : override;

polymorphic

  • That is, the same method can adopt many different behavior modes according to different sending objects.
  • The actual type of an object is determined, but there are many reference types that can point to the object (parent class, related class)
  • Conditions for the existence of polymorphism
    • There is an inheritance relationship
    • Subclass overrides parent method
    • A parent class reference points to a child class object
  • Note: polymorphism is the polymorphism of methods, and there is no polymorphism of attributes
  • instanceof (type conversion) reference type
package com.oop;

import com.oop.demo06.Person;
import com.oop.demo06.Student;

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

        //The actual type of an object is determined
        //new Student();
        //new Person();

        //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 Person parent type can point to subclasses, but cannot call methods unique to subclasses
        Person s2 = new Student();
        Object s3 = new Student();

        //The methods that an object can execute mainly depend on the type on the left of the object, which has little to do with the right!
        ((Student)s2).eat();//The subclass overrides the method of the parent class and executes the method of the subclass
        s1.eat();
    }
}
package com.oop.demo06;

public class Person {

    public void run(){
        System.out.println("run");
    }
}
package com.oop.demo06;

public class Student extends Person{

    @Override
    public void run() {
        System.out.println("son");
    }
    public void eat(){
        System.out.println("eat");
    }
}

Precautions:

  1. Polymorphism is the polymorphism of methods, and there is no polymorphism of attributes
  2. Parent and child classes, associated type conversion exception! ClassCastException!
  3. Existence conditions: inheritance relationship, method needs to be overridden, parent class reference points to child class object! Father F1 = new son();

That cannot be overridden (more likely to be polymorphic):

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

Origin of notes - crazy teacher

Posted on Tue, 30 Nov 2021 19:21:44 -0500 by kirannalla