directory

  • Learning materials
  • Access modifier
  • Non-access modifier
  • encapsulation
  • Meaning of encapsulation
  • inheritance
  • Methods to rewrite
  • polymorphism
  • instanceof

Learning materials

B station crazy god said: www.bilibili.com/video/BV12J…

Access modifier

Source: www.runoob.com/java/java-m…

In Java, access control characters can be used to protect access to classes, variables, methods, and constructors. Java supports four different access permissions.

  • Default (default, write nothing) : Visible within the same package, without any modifiers. Use objects: classes, interfaces, variables, methods.
  • Private: visible within the same class. Use objects: variables, methods. Note: You cannot decorate classes (external classes)
  • Public: visible to all classes. Use objects: classes, interfaces, variables, methods
  • Protected: Visible to classes and all subclasses within the same package. Use objects: variables, methods. Note: You cannot decorate classes (external classes).
  • Interfaces and their member variables and member methods cannot be declared protected
The modifier The current class Within the same package Descendant class (same package) Descendants (different packages) Other packages
public Y Y Y Y Y
protected Y Y Y N
default Y Y Y N N
private Y N N N N

Non-access modifier

Java also provides a number of non-access modifiers for other functions.

Static modifier, used to modify class (static) methods and class (static) variables.

The final modifier is used to modify classes, methods, and variables. The class modified by final cannot be inherited, the modified method cannot be redefined by the inherited class, and the modified variable is constant and cannot be modified.

Abstract modifier, used to create abstract classes and abstract methods.

Synchronized and volatile modifiers, used primarily for threading programming.

encapsulation

The pursuit of high clustering, low coupling

Property is private and provides get/set methods

package com.zy7y.oop;

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.oop
 * @Author: zy7y
 * @Date: 2020/8/15 2:23 PM *@Description: encapsulation * /
public class Packaging {
    // A private property that can only be used in the current class
    private String name;

    // Provide the external set method setting value
    public void setName(String name) {
        this.name = name;
    }

    // Provide the external get method to get the value
    public String getName(a) {
        returnname; }}Copy the code

Startup file

package com.zy7y.oop;

import sun.net.www.http.HttpClient;

import java.io.File;

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.oop
 * @Author: zy7y
 * @Date: 2020/8/14 11:58 PM *@Description: Startup method */
public class Application {
    public static void main(String[] args) {
        Packaging packaging = new Packaging();
        packaging.setName("Ha ha"); System.out.println(packaging.getName()); }}Copy the code

Meaning of encapsulation

Provider security to protect data

Hide the implementation of the code interface

Unified interface

Increase maintainability

inheritance

The keyword extends

In Java, there is only single inheritance, which can be directly inherited from one person and indirectly inherited from multiple people

package com.zy7y.oop;
Person, all classes inherit from Object by default

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.oop
 * @Author: zy7y
 * @Date: 2020/8/15 12:04 am *@DescriptionConstructor: public class name (), no return value */
public class Person {
    private int age;
    String name;
    // The class has a no-argument constructor by default

    // Define a parameterized constructor; Constructor function, initializes properties, if a parameterized constructor is defined, it also needs a parameterless constructor, it needs to display the definition of a parameterless constructor
    public Person(String name){
        this.name = name;
    }
    // Alt + insert generates the constructor quickly


    // Displays the definition of a no-argument constructor
    public Person(a) {
        this.name = "zy7y";
    }
    private void privateMethod(String msg){
        System.out.println("Private method, cannot inherit!" + msg);
    }

    public int getAge(a) {
        return age;
    }

    public void setAge(int age) {
        this.age = age; }}Copy the code
package com.zy7y.oop;
// Subclasses inherit the Person class
/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.oop
 * @Author: zy7y
 * @Date: 2020/8/15 2:31 PM *@Description: * /
Teacher is a subclass of Person
public class Teacher extends Person {
    // Subclasses can inherit non-private attributes and methods from their parent class

    private String name = "Child";

    public void test(a){
        System.out.println(name);
        System.out.println(this.name); // Call the local name
        System.out.println(super.name); // Call the name of the parent class}}Copy the code

Super:

Super calls the constructor of the parent class, which must be the first constructor

Super must only appear in methods or constructors of subclasses

Super and this cannot call the constructor at the same time

This, super difference:

This: a reference to the current object

Super: represents a reference to a superclass object

This: Can be used without inheritance

Super: can be used only after the condition is inherited

This () : Construction of this class

Super () : Construction of the parent class

Methods to rewrite

Overrides are method overrides, not property overrides. Overrides can only overwrite non-static methods, only public methods. Methods decorated by fianl cannot be overridden

A subclass overrides the methods of its parent class, requiring an inheritance relationship

The method names must be the same

The parameter lists must be the same

Modifier: The range can be expanded, but not shrunk public > protected > default > private

Exceptions thrown: The range can be narrowed, not expanded

Override: subclass and parent class must have the same method body, different method body!

package com.zy7y.oop;

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.oop
 * @Author: zy7y
 * @Date: 2020/8/15 2:31 PM *@Description: * /
Teacher is a subclass of Person
public class Teacher extends Person {
    // Subclasses can inherit non-private attributes and methods from their parent class

    private String name = "The Teacher subclass";


    @Override
    public void test(a) {
        System.out.println(name);
        System.out.println(this.name); // Call the local name}}Copy the code
package com.zy7y.oop;


/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.oop
 * @Author: zy7y
 * @Date: 2020/8/15 12:04 am *@DescriptionConstructor: public class name (), no return value */
public class Person {
    private int age;
    String name = "The superclass Person";
    // The class has a no-argument constructor by default

    // Define a parameterized constructor; Constructor function, initializes properties, if a parameterized constructor is defined, it also needs a parameterless constructor, it needs to display the definition of a parameterless constructor
    public Person(String name){
        this.name = name;
    }
    // Alt + insert generates the constructor quickly


    // Displays the definition of a no-argument constructor
    public Person(a) {
        this.name = "zy7y";
    }
    private void privateMethod(String msg){
        System.out.println("Private method, cannot inherit!" + msg);
    }

    public int getAge(a) {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void test(a){
        System.out.println("The superclass Person"); // Call the local name}}Copy the code

polymorphism

The same method behaves differently depending on the object being sent

Condition:

  • Have a hereditary relationship
  • A subclass overrides a parent class method
  • A superclass reference points to a subclass object

⚠️ : Polymorphism is a polymorphism of methods, without polymorphism of attributes

Type conversion exception: ClassCastException

// Start the file
package com.zy7y.oop;

/ * * *@ProjectName: JavaSE
* @PackageName: com.zy7y.oop
* @Author: zy7y
* @Date: 2020/8/14 11:58 PM *@Description: Startup method */
public class Application {
   public static void main(String[] args) {
       Teacher teacher = new Teacher();
       teacher.test();
       System.out.println();


       The parent class reference refers to the subclass Person, the subclass Teacher, and the methods that the object can perform depend on the type on the left, not on new
       Person teacher1 = new Teacher();
       teacher1.test();

       // only subclasses have the eat method
       teacher.eat();
       // There is no parent class
       teacher1.eat();

       // (down transition, may lose some methods)((Teacher)teacher1).eat(); }}Copy the code
/ / parent class
package com.zy7y.oop;


/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.oop
 * @Author: zy7y
 * @Date: 2020/8/15 12:04 am *@DescriptionConstructor: public class name (), no return value */
public class Person {
    private int age;
    String name = "The superclass Person";
    // The class has a no-argument constructor by default

    // Define a parameterized constructor; Constructor function, initializes properties, if a parameterized constructor is defined, it also needs a parameterless constructor, it needs to display the definition of a parameterless constructor
    public Person(String name){
        this.name = name;
    }
    // Alt + insert generates the constructor quickly


    // Displays the definition of a no-argument constructor
    public Person(a) {
        this.name = "zy7y";
    }
    private void privateMethod(String msg){
        System.out.println("Private method, cannot inherit!" + msg);
    }

    public int getAge(a) {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void test(a){
        System.out.println("The superclass Person"); // Call the local name}}Copy the code
/ / subclass
package com.zy7y.oop;

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.oop
 * @Author: zy7y
 * @Date: 2020/8/15 2:31 PM *@Description: * /
Teacher is a subclass of Person
public class Teacher extends Person {
    // Subclasses can inherit non-private attributes and methods from their parent class

    private String name = "The Teacher subclass";


    @Override
    public void test(a) {
        System.out.println(name);
        System.out.println(this.name); // Call the local name
    }

    public void  eat(a){
        System.out.println("eat"); }}Copy the code

instanceof

        // object instanceof class to determine if there is a relationship
        System.out.println(person instanceof Object);
        System.out.println(teacher1 instanceof Object);
        System.out.println(teacher instanceof Object);
Copy the code