Object Oriented Understanding (Inheritance)

The following personal views:

Call properties and methods through objects—

Attributes : Find matching attributes directly by reference .
Method :
(1) The case of not overriding the method:
Following the proximity principle is to call the method inherited from the closest parent class.
(2) Overriding method situation:
Call that overridden method of the actual object. (Even overwriting that doesn't change is different from not writing.)



The following code example:

Procedure: Invoke a property through an instance object

Viewpoint: Find matching properties directly by reference.

Code:

public class Parent {
	int a = 3;
}


public class Son extends Parent {
	int a = 5;

	public static void main(String[] args) {
		Son son = new Son();
		System.out.println( son.a ); //result 5
		System.out.println( ((Parent)son).a ); //result 3
	}
}


Procedure: Invoke a method through an instance object

View:

        (1) The case of not overriding the method: following the principle of proximity, that is, calling the method inherited from the closest parent class.

(2) Overriding method situation: calling the overridden method of the actual object. (Even overwriting that doesn't change is different from not writing.)


Code:

(1) The case of not overriding the method: following the principle of proximity, that is, calling the method inherited from the closest parent class.
public class Parent {
	public void A(){
		System.out.println("Parent---A");
	}
	public void B(){
		System.out.println("Parent---B");
	}
}




public class Son extends Parent {
	public void A(){
		System.out.println("Son---A");
	}
}





public class Grandson extends Son{
	public static void main(String[] args) {
		Grandson grandson = new Grandson();
		grandson.A(); // Result: Son---A, Son covers A() and is closer to Crandson.
		grandson.B(); // result: Parent---B
	}
}


(2) Overriding method situation: calling the overridden method of the actual object.


public class Parent {
	public void A(){
		System.out.println("Parent---A");
	}
}




public class Son extends Parent {
	public void A(){
		System.out.println("Son---A");
	}
	public static void main(String[] args) {
		Son son = new Son();
		son.A (); // Result: Son --- A
	}
}




Note: calling a method from an object and calling a property in that method -       

        It depends on the relationship between the method and the property being called . For example, the subclass calls the method of the superclass, and the attributes in the method are the attributes in the superclass. In this case, it must not be confused with properties overridden in subclasses.



Personal understanding of super and this is:

super refers to an object that is coerced into a reference of the parent class , the actual object is still the original object, and this conversion will not be polymorphic (see the code below);

this refers to the current object of the current reference .


Code:

public class Parent {
	public void A(){
		System.out.println("Parent---A");
	}
}



public class Son extends Parent {
	public void A(){
		System.out.println("Son---A");
	}
	
	public void print(){
		this.A(); //Result: Son---A
		super.A(); // Parent---A, no polymorphism
		((Parent)this).A();// Son---A, polymorphism occurs
		System.out.println(this); //Son@1db9742
		System.out.println(super.toString());//Son@1db9742
	}
	
	public static void main(String[] args) {
		new Son().print();
	}
}


For a deeper understanding of this and super reference: https://blog.csdn.net/weixin_42095279/article/details/80136733


Guess you like

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