Java language basics-class inheritance

Java language basics-class inheritance

One of the three major characteristics of the Java language


Inherited characteristics

Through inheritance, we can expand or transform on the basis of existing types to obtain new data types.

  • The existing data types are called superclasses or superclasses.
  • The new data types obtained are called subclasses or derived classes.
  • Class inheritance improves the reusability and scalability of the program code , and shortens the software development cycle.

Inherited classification

  • Single inheritance --- subclasses can only have one direct parent class
  • Multiple inheritance ----- subclasses can have multiple direct parent classes

Java does not support multiple inheritance

Inheritance

format:

父类{
	父类的内容
}
子类 extends 父类{
	子类的内容
}

Example:

package NewBolg;

class inherit {
    private String blogger = "董小宇";
    public String getBlogger() {
        return blogger;
    }
    public void setBlogger(String blogger) {
        this.blogger = blogger;
    }
    public static void main(String[] agrs){
        Subclass subclass = new Subclass();
        System.out.println("平台:" + subclass.getBlog() + ",博主是:" + subclass.getBlogger());
    }
}
class Subclass extends inherit{
    private String Blog = "CSDN";
    public String getBlog() {
        return Blog;
    }
    public void setBlog(String blog) {
        Blog = blog;
    }
}

The running result is:

平台:CSDN,博主是:董小宇

note:

  1. The member variables defined in the parent class are inherited by the subclass, but the privatepermissions cannot be directly accessed, and others can be directly accessed. The member methods defined in the parent class are also inherited by the subclass.
  2. The methods of overriding (overriding) the subclass are exactly the same as those inherited from the parent class (same method name, same parameter, same return type).
  3. The access control permission is higher than the access control permission of the method of the parent class, and the exception type thrown by the method of the subclass is lower than that of the parent class.

Rewrite rules: "Three Same, One Small, One Large" rule

  • "Three same": the same method name, the same formal parameter list, and the same return type;
  • "One small" means that the exception class thrown by the subclass method declaration should be smaller or equal to the exception class thrown by the parent class method declaration;
  • "One big" refers to the access rights of the subclass methods should be greater or equal than the parent class methods;
  • Both the overriding method and the overridden method are either class methods or instance methods, and neither can be a class method or an instance method.

The difference between method rewriting and method overloading

  • Coverage is the direct relationship between the subclass and the parent class; overloading is the relationship between multiple methods within the same class.
  • Overwriting is generally between two methods, and overloading depends on you are multiple overloaded methods.
  • The overridden methods have the same method name and formal parameter list; while the overloaded methods can only have the same method name, not the same formal parameter table.
  • When overwriting, the method is distinguished according to the object that calls it; and overloading is to determine which method is called based on the formal parameters.

note:

  • superIs a keyword provided by Java, it is the default reference of the direct parent object.
  • Just as it thiscannot appear in a staticmodified method, superit cannot appear in statica method.
  • If the subclass defines mathematics with the same name as the parent class, it will also happen that the subclass attributes override the parent class attributes. When the methods of the subclass directly access this property, they will access the overriding property, and cannot access the overridden property-by superaccessing the overridden property of the parent class.

Example:
If we access an attribute named a in a method, but the specified caller is not displayed, the system searches for a in the order:

  1. Find if there is a local variable named a in this method.
  2. Find if the current class contains an attribute named a.
  3. Find if the attribute of a is included in the direct parent class of a, and then trace the parent class of a in turn, knowing the java.lang.Objectclass. If the attribute named a cannot be found in the end, the system will generate a compilation error.

Call the constructor of the parent class

The subclass does not inherit the superclass constructor, but sometimes the subclass constructor needs to call the initialization code of the superclass constructor.


The subclass constructor calls the parent class constructor as follows:

  1. The first line of code in the execution body of the subclass construction method uses the superexplicit call to the parent class construction method. The system will supercall the corresponding construction method of the parent class according to the argument list passed in the call.
  2. The first line of code in the execution method of the subclass construction method uses the thisexplicit call to the construction method in this class. The system will thiscall another construction method of this class according to the list of arguments passed in the call. When another constructor in this class is executed, the constructor of the parent class is called.
  3. There is neither a supercall nor a thiscall in the execution body of the subclass constructor. Before the subclass constructor is executed, the system implicitly calls the parent's constructor without parameters.

Note: The
super call is thisvery similar to the supercall , the difference is that the constructor of its parent class is called, and this calls the constructor carried in the same class. Therefore, using super to call the parent class structure must also appear in the first line of the subclass construction execution, so the thiscall and the supercall will not appear at the same time.

Example:

public class AA {
    public static void main(String[] args){
        //输出A
        //分析:AA为父类,则无影响
        AA aa = new AA();
        //输出A,B
        //分析:BB为子类,调用无参BB,super无参父类,输出A,然后输出无参BB输出语句
        BB bb = new BB();
        //输出A,AA,BB
        //分析:BB为子类,调用含参BB,super带参父类,带参父类中含有this(),先执行不带参父类,然后执行带参父类的输出语句,最后执行带参子类的输出语句
        BB bb = new BB(1);
    }
    AA(){
        System.out.println("A");
    }
    AA(int i){
        this();
        System.out.println("AA");
    }
}
class BB extends AA{
    BB(){
        super();
        System.out.println("B");
    }
    BB(int i){
        super(i);
        System.out.println("BB");
    }
}

I am also a beginner in programming, and it is inevitable that there is a mistake in understanding. I hope that after reading, you can comment out the errors, thank you

Published 23 original articles · praised 276 · 20,000+ views

Guess you like

Origin blog.csdn.net/lolly1023/article/details/105539770
Recommended