iT邦幫忙

2022 iThome 鐵人賽

DAY 22
0

Advanced Concept

Continuing the theme of category inheritance from yesterday, the following content will be slightly more complicated.

Before we get started, let's briefly review the concept of inheritance:

  1. Subcategories extend the attributes of the main category and add new content
  2. A parent category can be inherited by multiple subcategories, and a subcategory can only inherit one parent category
  3. A subcategory can basically completely inherit the content of the parent category, except for the constructor, modifier private, and default

Useing Function inheritance to Access Modifier

Here, a more complete code will be shared, which will be used to address the inheritance of constructors and modifiers.

I believe that more comprehensive content can help you understand the concept of “inheritance” faster and more easily!

class example{
	private double answer;
	private double num = 12.6;

	public example(){
		System.out.println("Superclass was called ! ");
	}
	
	public void add_float(double temp){
		answer = num + temp;
		System.out.println("new_float = " + answer );
	}
}

class example01 extends example{
	private int count = 0;

	public example01(){
		System.out.println("Subclass was called ! ");
	}

	public example01(String name){
		System.out.println("Editor : " + name);
	}
	
	public void add_int(int temp){
		count += temp;
		System.out.println("new_int = " + count);
	}
	
	public void show(){
		System.out.println("End of this example ! ");
	}
}

public class Main{
	public static void main(String[] args){
		example01 new_obj = new example01();
		example01 new_obj_one = new example01("Jason");
		new_obj.add_int(6);
		new_obj.add_float(12.06);
		new_obj.show();
	}
}

Outcome :
https://ithelp.ithome.com.tw/upload/images/20221007/20151216Nu94M8lbeV.png


Program analysis

In this program, there are three important points to note:

  1. How private is accessed
    *Functions:
    Because private variables cannot be inherited by subclasses. If we do not use a concept such as encapsulation, we can use the content of the function inherited from the parent class to read and print it.

  2. Constructor overloading
    Looking at the content, we find that there are two constructors with the same name in the subclass, which is the concept of “overloading” we mentioned earlier. Constructors can also be overloaded, and they must also pass in different argument content. When defining them, pay attention to the difference in the names of the objects.

  3. Constructor call

  • As we shared earlier, constructors are not inherited:
    Here we see that both the parent and child classes have constructor calls. When we create an object of the child class in the main function, we will see that the parent class is called first, followed by the child class. The reason for this is that the subclass inherits the properties of the parent class, so the parent class constructor is called first. The reason for this is that...

the main class content is initialized, and then the initialization process of the subclass is executed

(this is also the meaning of the multiple loads of the constructor in this example).


上一篇
Day 21 : Inheritance ( 1 )
下一篇
Day 23 : Inheritance ( 3 )
系列文
30天Java由淺入深30
.

尚未有邦友留言

立即登入留言