JAVA09 -- this keyword_ Inheritance (basic concepts, method overrides, Object classes, super keywords, combinations)

catalogue

this keyword (implicit parameter)

inherit

Basic concepts

Summary

Method override

Object class

super keyword

combination

this keyword (implicit parameter)

  • 1. in a normal method, this always points to the object that calls the method
  • 2. in the constructor, this always points to the object to be initialized
  • 3. call other construction methods through this (must be in the first sentence)
  • this cannot be used with static methods

It is the default parameter in the method in the class, that is, the current object

eg1:

public class Student {
	public void study(){
		this.name = "wolf";
		System.out.println(name+"Learning");
	}
	
}

When the study method of an object is run, the name attribute of the object is Zhang San

eg2:

public class Student {
	String name;
	int id;
	
	public Student(String name,int id){
		this.name = name;
		this.id = id;
	}
}

Distinguished from name and id, if this is not added, it will be regarded as a variable of parameter, which means that no operation is performed

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

Ibid

eg3:

public class Student {
	String name;
	int id;
	
	public Student(String name,int id){
		this();
		this.name = name;
		this.id = id;
	}
	
	public Student(){
		System.out.println("Construct an object");
	}
	
}

inherit

Basic concepts

Class is the abstraction of objects, and inheritance is the abstraction of a certain batch of classes, so as to achieve better modeling of the real world
Improve code reusability
Extensions means "extensions". A subclass is an extension of a parent class

eg:

Original code:

public class Animal {

	String eye;
	
	public void run(){
		System.out.println("run");
	}
	public void eat(){
		System.out.println("eat");
	}

}

class Human{

	String eye;
	
	public void run(){
		System.out.println("run");
	}

	public void eat(){
		System.out.println("eat");
	}
	
	public void taiSheng(){
		System.out.println("Viviparous");
	}

}

class Bird{

	String eye;
	
	public void run(){
		System.out.println("run");
	}

	public void eat(){
		System.out.println("eat");
	}
	
	public void eggSheng(){
		System.out.println("Oviparous");
	}

}

There are common codes or methods among various types, and the extensions keyword is used to inherit:

public class Animal {
	String eye;
	
	public void run(){
		System.out.println("run");
	}
	
	public void eat(){
		System.out.println("eat");
	}
	
}

class Human extends Animal{
	
	public void taiSheng(){
		System.out.println("Viviparous");
	}
	
}

class Bird extends Animal{
	
	public void eggSheng(){
		System.out.println("Oviparous");
	}
	
}

Summary

  • A subclass inherits from the parent class and can get all the properties and methods of the parent class (except the constructor of the parent class)
  • In java, there is only single inheritance, not multiple inheritance like c++. Multiple inheritance can cause confusion, making the inheritance chain too complex and the system difficult to maintain. Multi inheritance is to realize the reusability of code, but it introduces complexity, which makes the relationship between system classes chaotic. (a class can only have one direct parent)
  • Multiple inheritance in java can be implemented through interfaces
  • If extensions is not called when defining a class, its parent class is:
    java.lang.object
  • Different names: superclass, parent class, base class, subclass, derived class

Method override

  • Methods inherited from the base class can be overridden in subclasses as needed.
  • The overriding method must have the same method name, parameter list, and return type as the overridden method
  • An overriding method cannot use more strict access rights than the overridden method. (due to polymorphism)

eg:

Animal.java

public class Animal {
	String eye;
	
	public void run(){
		System.out.println("run");
	}
	
	public void eat(){
		System.out.println("eat");
	}
	
}

class Bird extends Animal{
	
	public void run(){	//Override run method
		System.out.println("fly");
	}
	
	public void eggSheng(){
		System.out.println("Oviparous");
	}
	
}

Test.java

public class Test {

	public static void main(String[] args) {
		Bird b = new Bird();
		b.run();
	}
	
}

The implemented method is rewritten

If the parent class needs to be called after Rewriting: super

class Bird extends Animal{
	
	public void run(){	//Override run method
		super.run();
		System.out.println("fly");
	}
	
	public void eggSheng(){
		System.out.println("Oviparous");
	}
	
}

Object class

  • The Object class is the root base class of all Java classes
  • If the extensions keyword is not used in the class declaration to specify its base class, the default base class is the object class
  • Override: toString method:
    Default return: package name + class name + @ + hash code
    Can override
    Hash code: generated according to the memory location of the object, unique and not duplicate

super keyword

  • Super is a reference to a direct parent object. You can use super to access methods or properties in a parent class that are overridden by subclasses.
  • General method:
    There are no sequence restrictions. It can be called at will.
    Each method has two parameters by default: this and super
  • Constructor:
    In the constructor of any class, if the first line of the constructor does not explicitly call super (··); Then Java will call super() by default; As the initialization function of the parent class. So super(); It doesn't matter whether you add it or not.

combination

  • "is-a" relationship uses inheritance
  • "has-a" relation use combination

eg: examples in inheritance:

public class Animal2 {
	String eye;
	
	public void run(){
		System.out.println("run");
	}
	
	public void eat(){
		System.out.println("eat");
	}
	
	public Animal2(){
		System.out.println("Create an animal");
	}
	
	public static void main(String[] args) {
		Bird2 b = new Bird2();
		b.run();
	}
	
}

class Human2{
	
	Animal2 animal2 = new Animal2();;
	public void taiSheng(){
		System.out.println("Viviparous");
	}
	
}

class Bird2{
	
	Animal2 animal2 = new Animal2();
	
	public void run(){
		animal2.run();
		System.out.println("fly");
	}
	
	public void eggSheng(){
		System.out.println("Oviparous");
	}
	
	public Bird2(){
		System.out.println("Create a bird object");
	}
	
}

Operation results:

Create an animal
Create a bird object
run
fly

Same function as inheritance

Tags: Java

Posted by BK87 on Mon, 30 May 2022 09:02:40 +0530