[JAVA refined into immortality] magic chapter -- object oriented ② (inheritance, construction method, rewriting, overloading)

Posted by bsamson on Thu, 10 Mar 2022 21:04:59 +0100

🔎 This is JAVA Chengxian road. Pay attention to my learning JAVA and don't get lost
👍 If it helps you, give the blogger a free praise to show encouragement
Welcome 🔎 give the thumbs-up 👍 Comment collection ⭐ ️

This chapter introduces the construction method, inheritance, rewriting, overloading and creation method in detail. The whole article takes the old driver and human as examples to illustrate the case of interspersed code.

JAVA Chengxian road starts from the foundation, and the follow-up meeting will talk about JAVA advanced, which will be interspersed with test questions and project practice. I hope it can bring you help!

Construction method

The construction method of a class is a special method. The name of this method must be consistent with the class name. The construction cannot have a return value, does not use void, and cannot be called directly. It is called automatically when the class object is instantiated, and can be called when new. The general construction method is used for the initialization of class object instantiation. If a class does not write a constructor, the system will automatically add a parameterless constructor to this class during compilation. If the declaration class has written a construction method, the system will no longer add a parameterless construction method. It is recommended to write a parameterless construction method after writing the construction method.

Nonparametric structure

To put it bluntly, it is a construction method without parameters
I can't understand the concept. For example, it's clear.
Declare a Class A

public class A {
    
}

Create a constructor A() in class A and print out a sentence
The constructor must be the same as the class name

public class A {
    public A(){
        System.out.println("Construction method A()");
    }
}

Create A new test class and nuw A class A object in the class

public class Test {
    public static void main(String[] args) {
        A a = new A();
    }
}

Try the main method

The construction method is equivalent to the method used automatically in the new object

Parametric structure

Parametric construction is a construction method with parameters
Declare a construction method with parameters in class A and pass in two String type parameters, a and b

public class A {
    public A() {
    }

    public A(String a, String b) {
        System.out.println(a + b);
    }
}

Pass in the parameter when you create a new object a in the test class

public class Test {
    public static void main(String[] args) {
        A a = new A("aaa", "bbb");
    }
}

Execute the main method

be careful

When there is no construction method, a parameterless construction is hidden in the class. However, if you create a parameterized construct, the hidden nonparametric construct will disappear. Then the new object can only take parameters in the future. Therefore, when building a parametric construction, you must create a nonparametric construction method and put it there.

Class inheritance

In the java language, class is a single inheritance and implements multiple interfaces. Interface interface is multi inheritance.

Why inherit? Because the subclass wants to add new functions based on the parent class.
A subclass inherits from a parent class and can inherit methods and properties from the parent class
The following is an example:
The parent class is people and the child class is old drivers.
Human characteristics are: two hands, two eyes, two feet, can eat and drink
Old drivers inherit human characteristics and have the function of being able to drive.

The code is as follows:

This is a human with the following attributes and functions.

public class Ren {
    public final String shou = "Two hands";
    public final String jiao = "Two feet";
    public final String yan = "Two eyes";

    public void chi() {
        System.out.println("Can eat");
    }

    public void he() {
        System.out.println("Can drink");
    }

}

This is an old driver who inherits human beings. It also has a function of being able to drive

public class Siji extends Ren {
    public void kai() {
        System.out.println("Can drive");
    }
}

Test: create the output attribute of the old driver object and call the method.

public class Test {
    public static void main(String[] args) {
        Siji b = new Siji();
        System.out.println(b.jiao);
        System.out.println(b.shou);
        System.out.println(b.yan);
        b.chi();
        b.he();
        b.kai();
    }
}

Method rewriting and overloading

Override: the method name, return type and formal parameters are the same. In this case, it must be inheritance.
Overloading: methods with the same method name, return type, number of formal parameters and different types do not have to be inherited. They can be used in the same class, such as construction method overloading

rewrite

Because the subclass is not satisfied with the method of the parent class, I want to change it. This is the method of the subclass overriding the parent class.
For example, people can eat and drink, but the old driver doesn't like it. He wants to change it to eat shit.

The code is as follows:

Old driver rewrites human chi() method
The method name must be the same

public class Siji extends Ren {
    public void kai() {
        System.out.println("Can drive");
    }

    public void chi() {
        System.out.println("Can eat shit");
    }
}

Now override the run test class

heavy load

Parametric construction and nonparametric construction are method overloading. There are two methods with the same name in a class, but their return value type and parameter type and the number of parameters are different.
The return value type, parameter type and number of parameters of the two methods are different. But if they have the same method name, the two methods will be overloaded.

Topics: Java JavaEE