Alibaba cloud [teacher class] Java object-oriented development 54 ~ 56: inherited definition and use

Posted by Grant Holmes on Sun, 14 Jun 2020 07:00:04 +0200

Alibaba cloud [teacher class] Java object-oriented development 54 ~ 56: inherited definition and use

Derivation of inheritance problem

The three characteristics of object-oriented programming: encapsulation, inheritance and polymorphism. Inheritance is the expansion of function on the basis of existing.
If you want to better understand why inheritance is necessary, you can define two categories with the concepts you have learned before: people and students.
Example: defining human: name age, student: name age school

class Person {
	private String name ;
	private int age ;
	
	public void setName(String name){
		this.name = name ;
	}
	public String getName(){
		return this.name ;
	}
	public void setAge(int age){
		this.age = age ;
	}
	public int getAge(){
		return this.age ;
	}
}

class Student {
	private String name ;
	private int age ;
	private String school ;
	
	public void setName(String name){
		this.name = name ;
	}
	public String getName(){
		return this.name ;
	}
	public void setAge(int age){
		this.age = age ;
	}
	public int getAge(){
		return this.age ;
	}
	public void setSchool(String school){
		this.school = school ;
	}
	public String getSchool(){
		return this.school ;
	}
}

Students have only one more attribute than humans. If you look at the program carefully, there will be repeated code when it is defined according to the previous pattern (single simple Java class), but this repetition is meaningless.

  • Because Conceptually speaking, students must be human beings, the scope of students is smaller than human beings, and the attributes and methods described are more than human beings. So students should be human expansion.

Implementation of inheritance

Inheritance in Java is implemented using the extends keyword. The definition syntax is as follows:

  • Class subclass extends parent class {}
    • A subclass is also called a derived class, and a superclass is also called a superclass

Example: basic implementation of inheritance

class Person {
	private String name ;
	private int age ;
	
	public Person() {} // Note that there should be a nonparametric structure
	
	public void setName(String name) {
		this.name = name ;
	}
	public String getName() {
		return this.name ;
	}
	public void setAge(int age) {
		this.age = age ;
	}
	public int getAge() {
		return this.age ;
	}
}

class Student extends Person { // No definition for now
}

public class TestDemo {
	public static void main(String args[]) {
		Student stu = new Student() ; // Subclass instantiation object
		stu.setName("dexter") ; // No such property or method in subclass
		stu.setAge(20) ; // No such property or method in subclass
		System.out.println("name:" + stu.getName() + ",age:" + stu.getAge()) ;
	}
}


Through the code, we can find that when inheritance occurs, the subclass can directly inherit the parent class, and can realize code reuse. At least, the subclass keeps the same function as the parent class, and wants to expand the properties and methods as long as they operate directly in the subclass.
Example: subclass for function expansion

······  // The Person class doesn't change anything
class Student extends Person {
	private String school ;
	
	public void setSchool(String school){
		this.school = school ;
	}
	public String getSchool(){
		return this.school ;
	}
}

public class TestDemo {
	public static void main(String args[]) {
		Student stu = new Student() ; // Subclass instantiation object
		stu.setName("dexter") ; // This property does not exist in subclass
		stu.setAge(20) ; // This property does not exist in subclass
		stu.setSchool("hfut") ;
		System.out.println("name:" + stu.getName() + ",age:" + stu.getAge() + ",school: " + stu.getSchool()) ;
	}
}

Inherit usage restrictions

Although inheritance can realize that children share the properties and methods of the parent class, there are some restrictions on inheritance:

Call constructor

Before the instantiation of a subclass object, the parent object is automatically instantiated, and the subclass object is instantiated after the constructor of the parent is called.

class Person {
	public Person() {
		System.out.println("*** Person Class object creation ***") ;
	}
}

class Student extends Person {
	public Student() {
		System.out.println("*** Student Class object creation ***") ;
	}
}

public class ExtendsDemo {
	public static void main(String args[]) {
		new Student() ; // Subclass instantiation object
	}
}

It can be seen that although no statement calls the parent class construction, the parent class construction is executed, indicating that the parent class object must be instantiated before instantiation of the child class object.

  • Note: at this time, in the first line of the subclass construction (because the parent class construction method is called, it must be in the first sentence), there is an implicit statement: super();, which is the same as not writing when constructing no parameter;
  • When the parent class has no parameter free construction, the child class must have super (the corresponding parameter of the parent class construction to be called); otherwise, there will be an error prompt: the actual parameter list and the formal parameter list are different in length.
class Person {
	public Person(String name, int age) {
		System.out.println("*** Person Class object creation ***") ;
	}
}

class Student extends Person {
	public Student(String name, int age, String school) {
		super(name,age) ;
		System.out.println("*** Student Class object creation ***") ;
	}
}

public class ExtendsDemo {
	public static void main(String args[]) {
		new Student("dexter",20,"hfut") ; // Subclass instantiation object
	}
}

Single inheritance: a subclass can only inherit one parent

Example: wrong inheritance in Java: multiple inheritance

class A {}
class B {}
class C extends A,B {}

The purpose of multiple inheritance is that class C inherits the operations of class A and class B at the same time, while Java does not allow multiple inheritance. To achieve this goal, you can use multiple inheritance.

class A {}
class B extends A {}
class C extends B {}

But multi level inheritance is not recommended.

Subclass inherits all the structure of the parent

All structures: private property, construction method, common method.
All non private operations belong to explicit inheritance, and all private operations belong to implicit inheritance

  • Display inheritance: can be called directly
  • Implicit inheritance: called by other means (setter, getter)
class Person {
	private String name ;
	
	public Person() {}
	
	public void setName(String name) {
		this.name = name ;
	}
	public String getName() {
		return this.name ;
	}
}

class Student extends Person {
	
}

public class ExtendsDemo {
	public static void main(String args[]) {
		Student stu = new Student() ;
		stu.setName("dexter") ;
		System.out.println(stu.getName()) ;
	}
}
// Output dexter

If you want to call name directly in a subclass, this happens:

class Student extends Person {
	public void method() {
		System.out.println(name) ;
	}
}
// Output error prompt: name is private access control in Person

It is true that the properties in the parent class are inherited by the child class, but it can be found that all the non private operations (public) can be used by the child class, and all the private operations cannot be used directly, so it is called implicit inheritance.

summary

  1. The purpose of inheritance is to extend the function of existing classes and reuse the code;
  2. The process of subclass instantiation: regardless of how to operate, it must first instantiate the parent class, and then instantiate the subclass object.
  3. Inheritance limit: multiple inheritance is not allowed, but multiple inheritance is allowed.

Topics: Java Programming Attribute