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:
private
, and default
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 :
In this program, there are three important points to note:
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.
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.
Constructor call
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).