Three characteristics of java object-oriented: encapsulation, inheritance and polymorphism

Three characteristics of object-oriented

encapsulation

1. The concept of encapsulation is to make the program * * highly cohesive and low coupling**

  • High cohesion: it means that the internal data operation details of the class are completed by itself, and external interference is not allowed
  • Low coupling: only a few methods are exposed for external use

2. Encapsulation is to prohibit direct access to the actual representation of data in an object, but to access * * (get/set) through the operation interface**

package com.oop.demo03;
//Private for students: private
public class Student {
    //Property private
    private String name;//full name

    private int id;//Student number

    private char sex;//Gender

    private int age;//Age

    //By providing some public get and set methods, the program can be called
    //Get: get this data
    public String getName(){
        return this.name;
    }
    //Set: set the value for this data
    public void setName(String name){
        this.name=name;
    }
    //Use shortcut key: Alt+insert to quickly generate get and set methods

    public int getId() {
        return id;
    }

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

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        //Judgment statement can be added to set method to make the program more rigorous
        switch (sex){
            case 'male': this.sex = sex;
            break;
            case 'female': this.sex = sex;
            break;
            default: this.sex = 'nothing';;
            break;
        }

    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age>0 && age<100){
           this.age = age;
        }else{this.age=0;}

    }
}

//The significance of encapsulation
1.Improve program security and protect data
2.Hidden code implementation details
3.Unified interface
4.Improve the maintainability of the system

public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
        //When using private as modifier, the program cannot be called directly
        s1.setName("Haoxin");
        s1.setId(1001);
        s1.setSex('male');
        s1.setAge(101);

        System.out.println(s1.getName());
        System.out.println(s1.getId());
        System.out.println(s1.getSex());
        System.out.println(s1.getAge());

    }
}


inherit

  1. The essence of inheritance is to abstract a group of classes, so as to realize better modeling of the real world

  2. Extensions means extension. Subclass is extension of parent class

  3. In java, classes only have single inheritance but not multiple inheritance

  4. Inheritance is the relationship between classes. In addition, the relationship between classes includes dependency, composition, aggregation, etc.

  5. There are two classes of inheritance relationship, one is a subclass (derived class) and the other is a parent class (base class). The subclass inherits the parent class, which is represented by the keyword "exrtends"

    //Human -- > parent class / base class
    //All classes in Java directly or indirectly inherit Object classes
    public class Person  {
    
        //1.public: public
        //2.protected: protected
        //3.default: default
        //4.private: 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;
        }
    }
    
//Student is human student class (subclass / derived class) inherits human (parent class / base class)
//If a child inherits the parent, he will have all the methods of the parent
//Use ctrl+h to view the subclass and parent of the class
public class Student extends Person {

}
package com.oop.demo04;
//Teacher is human teacher class (subclass / derived class) inherits human (parent class / base class)
public class Teacher extends Person {

}
import com.oop.demo04.Student;
import com.oop.demo04.Teacher;
//Test class
public class Application {
 public static void main(String[] args) {
     Student student = new Student();
     student.say();Said a word
     System.out.println(student.getMoney());1000000000
     Teacher teacher = new Teacher();
     teacher.say();Said a word
     System.out.println(teacher.getMoney());1000000000


 }
}

super and inheritance

package com.oop.demo04;
//Superclass
public class Person  {

    public Person() {

        System.out.println("person No reference");
    }
    protected String name="Haoxin";

    //Private things cannot be inherited
    public void print(){
        System.out.println("Person");
    }
}
package com.oop.demo04;
//Subclass
public class Student extends Person {

    public Student() {
        //Hidden code: calling the nonparametric construction of the parent class first by default
        super();
        //To call the constructor of the parent class, it must be on the first line of the subclass constructor
       System.out.println("student No reference");

    }

    private String name="Andy";

    public void print(){
        System.out.println("Student");
    }
    public void test2(){
        print();
        this.print();
        //Method in parent class can be called through super
        super.print();
    }

    public void test1(String name){
        System.out.println(name);
        System.out.println(this.name);
        //super allows you to call properties in the parent class
        System.out.println(super.name);
    }
}
package com.oop;

import com.oop.demo04.Student;
public class Application {
    public static void main(String[] args) {
        Student student = new Student();//
        //person no parameter
        //student no parameters
        student.test1("hao");
        //hao
        //Andy
        //Haoxin
        student.test2();
        //Student
        //Student
        //Person
    }
}
/*
super attention
 1.super calls the constructor of the parent class to be the first
 2.super must only appear in the method or construction method of subclass
 3.super and this cannot call constructor at the same time

vs this: 
Different objects represented
   super: represents the application of the parent object
   this: represents the object called by itself
 Different preconditions
   this: can be used without inheritance
   super: can only be used under inherited conditions
 Different construction methods
   this(); construction of this class
   super(); construction of parent class


 */

The relationship between method rewriting and inheritance

package com.oop.demo04;
//Superclass
//Overrides are all method overrides, independent of properties
public class Person  {
    public  void test(){
        
        System.out.println("Person=>test()");
    }

}
package com.oop.demo04;
//Subclass
public class Student extends Person {
    //Override: the meaning of rewrite

    @Override//This is a functional annotation
    public void test() {
        System.out.println("Student=>test()");

    }
}
package com.oop;
//Test class

import com.oop.demo04.Person;
import com.oop.demo04.Student;
public class Application {
    public static void main(String[] args) {

        //The static method is quite different from the non static method
        //Static methods: method calls are only related to the data types defined on the left
        //Non static method: can implement rewriting

Student student = new Student();
        student.test();//Student=>test()

       //A reference to a parent class points to a child class
       Person person=new Student();
       person.test();//Student=>test()

}
 }
/*
rewrite
 1. Overriding requires inheritance, and only subclasses can override methods of the parent class
 2. Method name must be the same
 3. Parameter list must be the same
 4. Modifiers can be expanded but not reduced public > protected > Default > private
 5. Exception thrown: the scope can be narrowed but not expanded: classnotfountexception -- > exception (large)
6. Method body must be different
 7. Shortcut key for rewriting: Alt + insert -- > override;

 */

polymorphic

  1. That is to say, the same method can adopt different behaviors according to different sending objects
  2. The actual type of an object can be determined, but there are many reference types that can be pointed to
  3. The existence condition of polymorphism
    • Have an inheritance relationship
    • Method of subclass overriding parent class
    • A parent class references an object that points to a child class

Note: polymorphism is the polymorphism of methods, and there is no polymorphism in attributes

package com.oop.demo05;
//Superclass

public class Person {
    public void run(){
        System.out.println("run");
    }
}
package com.oop.demo05;
//Subclass
public class Student extends Person {
    @Override
    public void run() {
        System.out.println("son");
    }

    public void eat(){
        System.out.println("eat");
    }
}
package com.oop;
//Test class

import com.oop.demo05.Person;
import com.oop.demo05.Student;

public class Application {
    public static void main(String[] args) {
        //The actual type of an object can be determined
        //new Student();
        //new Person();

        //The reference types that can be pointed to are uncertain. A reference to a parent class points to a child class

        //The methods that students can call are their own or inherited from the parent class
        Student s1 = new Student();
        //The Person parent type can point to a subclass, but cannot call a method unique to the subclass
        Person s2 = new Student();
        Object s3 = new Student();
        //What methods an object can perform depends on the reference type on the left side of the object, which has little to do with the right side
        s1.run();
        s2.run();//Subclass overrides the method of the parent class and executes the method of the subclass
        //A parent class cannot call a unique method directly
        s1.eat();

    }
}

 */
/*
Considerations for polymorphism
 1. Polymorphism is the polymorphism of method, and there is no polymorphism in attribute
 2. Polymorphism must have inheritance relationship, parent class and child class, otherwise type conversion exception will be reported: ClassCastException
 3. Conditions of existence
  Inheritance relationship
  Method needs to be overridden
  A reference to a parent class points to a child class

Note: which methods cannot be overridden
 1. Static method: belongs to class, it does not belong to instance
 2. The modifier is final (constant)
3. The modifier is private
 */

Keywords: Java Attribute

Added by wildmanmatt on Tue, 16 Jun 2020 11:37:44 +0300