Inheritance in JAVA

Inheritance: Java only supports single inheritance

Take a simple example

/* Human, with attribute name and age */
class Person {
	String name;
	int age;
}

/* Student class, inherits human (student is also human), has its own unique method, study() method*/
class Student extends Person {
	void study() {
		System.out.println("good study");
	}
}

/* Worker class, inherits human beings (workers are also human beings), has its own unique method, work() method*/
class Worker extends Person {
	void work() {
		System.out.println("good work");
	}
}

public class ExtendsDemo {
	public static void main(String args[]) {
		// create student class object
		Student s1 = new Student();
		s1.name = "Zhang San";
		System.out.println(s1.name);
	}

}

In the above example, the name attribute in the student class inherits the name in the parent class

The following example details inheritance

/*
 * After the child parent class appears, the characteristics of class members
 * Members of the class:
 * 1. Variables
 * If there is a non-private variable of the same name in the subclass,
 * Subclasses use this to access variables in this class
 * Subclasses use super to access variables of the same name in the parent class
 * The use of super is almost the same as the use of this
 * this represents a reference to an object of this class
 * super represents a reference to the parent class object
 * 2. Function
 * When the subclass has the same function as the parent class
 * When the subclass object calls this function, the content of the subclass function will be run
 * Like the function of the parent class will be overridden
 * is one of the features of functions: rewriting
 *
 * When the subclass inherits the parent class, it inherits the functions of the parent class and transfers it to the subclass
 * However, although the subclass has this function, the content of the function is inconsistent with the parent class
 * At this time, it is not necessary to define a new function, but use the override special, retain the function definition of the parent class, and rewrite the function content
 *
 * Override:
 * 1. If the subclass overrides the parent class, it must ensure that the subclass permission is greater than or equal to the parent class permission before it can be overwritten, otherwise the compilation will fail
 * 2. Static can only cover static
 *
 * 3. Constructor
 * When the subclass is initialized, the constructor of the parent class will also run
 * That's because the constructor of the subclass has an implicit statement super() on the first line by default;
 * super(): The constructor with empty parameters in the parent class will be accessed, and the first line of all construction statements in the subclass is super() by default;
 *
 * Why does the subclass have to access the constructor in the parent class
 * Because the data in the parent class can be obtained directly by the subclass, when the subclass object is created, it is necessary to check how the parent class initializes the data.
 * So when the subclass object is initialized, you must first visit the constructor in the parent class.
 * If you want to access the constructor defined in the parent class, you can specify it by manually defining the super statement
 *
 * Note: the super statement must be defined in the first line of the subclass constructor
 * 		
 * Subclass instantiation process
 *
 * Conclusion: All constructors of subclasses will access the constructors with empty parameters in the parent class by default, because the first line in each constructor of subclasses has an implicit super()
 * When there is no constructor with empty parameters in the parent class, the subclass must manually specify the constructor in the parent class through the super or this statement. Of course, the constructor of the subclass
 * The first line can also manually specify this statement to access the constructor in this class
 * At least one constructor in the subclass will access the constructor in the superclass
 *
 * object class is the parent class of all classes
 * */

class Father {
	int num = 4;
	Father(){
		System.out.println("Father run");
	}
	void show() {
		System.out.println("Father"+" "+num);
	}
	void speak() {
		System.out.println("VB");
	}
}

class Son extends Father {
	int num = 5;
	Son(){
		System.out.println("Son run");
	}
	void speak() {
		System.out.println("JAVA");
	}
	void show() {
		System.out.println("Son"+" "+num);
	}
}

public class ExtendsDemo2 {
	public static void main(String args[]) {
		Son son = new Son();
		son.show();
	}
}

The execution result is: Father run

                        Son run

                        Son 5


                                                                                        --------------------By chick

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325593307&siteId=291194637