[Java object oriented] java learning journey 13 - inheritance

Posted by jlsports on Mon, 07 Mar 2022 17:24:08 +0100

definition:

The parent class derives a subclass, which inherits the parent class. The subclass can obtain the properties and methods of the parent class

use:

  • extends keyword, which declares that the subclass inherits from the parent class
  • super keyword, used to refer to the parent object (the parent object will be created before the child object is created)
  • super() represents the construction method of the parent class, super Super, method call. Super Property name calls the property of the parent class

Example:

/**
 * Human (parent)
 */
 public class Person { // Declare the class name. There can only be one class modified by public in a java file, and the class name of this class is consistent with the file name.

	// The attribute of the entity class, which is used to describe the characteristics of the object.
	private String name;
	private int age;
	private char sex;
	private String color;
	
	// Instantiate the object and initialize the assignment method.
	public Person() {
	}

	public Person(String name, int age, char sex, String color) {
		// this refers to the current object, the object to be created by the constructor, or the calling object of the method.
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.color = color;
	}

	// getters/setters method is used to operate private properties and check the legitimacy of the incoming value
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

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

	public char getSex() {
		return sex;
	}

	public void setSex(char sex) {
		this.sex = sex;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	// Class to describe the behavior of an object
	public void eat() {
		System.out.println("Eat something");
	}
	
	public void sleep() {
		System.out.println("sleep");
	}
	
	// Rewritten from the Object class, used to format all the attributes of the Object into a string, which is convenient for debugging in the programming process
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", sex=" + sex + ", color=" + color + "]";
	}
	
}
/**
 * Student class (inheriting parent class)
 */
public class Student extends Person {

	public Student() {
		super(); // Parameterless construction of parent class
	}

	public Student(String name, int age, char sex, String color) {
		// super refers to the superclass object
		super(name, age, sex, color); // Parameterized construction of parent class
	}
	
	public void study() {
		System.out.println("Learn to knock code");
	}
	
	@Override
	public void sleep() {
		System.out.println("Study hard and sleep what");
	}
	
	public void sleepFromSuperclass() {
		// Call other methods of the parent class through the super keyword
		super.sleep();
	}
	
	//Override or override the methods of the parent class
	public String toString() {
		return "Student [name=" + getName() + ", age=" + getAge() + ", sex=" + getSex() + ", color=" + getColor() + "]";
	}
	
}

Considerations for java inheritance

  1. java inheritance is single inheritance, and a subclass can only inherit one parent class.
  2. The subclass inherits the parent class and creates a subclass object. The parent class construction method will be executed first, and then the subclass construction method will be executed. Whether the subclass has parameters or no parameters, the parameterless construction of the parent class will be called by default.
  3. Subclasses cannot inherit private properties, private methods, construction methods, default properties and default methods of other package parent classes.
  4. If the subclass overrides the parent method, the subclass object will call the subclass method.
  5. The parent class is an abstract class, and the child class must implement all abstract methods, otherwise the child class is also an abstract class.
  6. Classes modified by final cannot be inherited, and methods modified by final cannot be overridden.
  7. If a class does not display an inherited parent class, it inherits the super parent class Object by default

The following concepts are extended:

rewrite

Override the inherited parent class method

use:

  1. Occurs in parent-child classes
  2. The method name is the same, the parameter list is the same, and the return value type is the same
  3. The permission modifier range cannot be smaller than the parent class

super vs this

  1. This represents the reference of this class object, and super represents the reference of parent class object
  2. this() represents the constructor of this class, and super() represents the constructor of parent class
  3. this and super represent construction methods and can only be used in construction methods. They must be the first line and cannot exist at the same time.

Four access modifiers

Access modifier meaningScope of action
publicOpenWhole project
protectedProtectedThis class, other classes in the same package, cross package subclasses
defaultdefaultThis category and other categories in the same package
privatePrivateThis category

Summary:

A class inherits from a class. It will get all the methods and properties in that class, and can have its own methods and properties

Topics: Java