JAVA object-oriented inheritance using the parent class and the sub-variable members illustrated memory

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_42947972/article/details/102025461

inherit

Code examples:
We are here not to like inside the variable is defined as private for simplicity, and provide indirect access to the

class Person{
    //把下面内容相同抽取出来的部分
    String name; 
    int age;    

}
class Student extends/*继承*/ Person{
    public void study()     //功能(动作)
    {
        System.out.println(name+"....student study...."+age);
    }

}

class Worker extends /*继承*/ Person{
    public void work()     //功能(动作)
    {
        System.out.println(name+"....Worker work...."+age);
    }
}
public class ExtendsDemo {     //类名
    public static void main(String[] args){
        Student st = new Student();
        st.name = "小明";
        st.age = 20;
        st.study();

        Worker wo = new Worker();
        wo.name = "小工";
        wo.age = 28;
        wo.work();

    }
}

The output is:
Here Insert Picture Description
we have a Student Worker class name and a class name, which name, and age are the same, we have to reuse code, they could have extracted a new form of class Person and use. Java keywords extends, so that the new class Person , become Student and Worker 's parent class.
we call this is called inheritance .
is the Student and Worker can use the Person of some variables and methods inside.

Inheritance we can be simply understood as long as you deny me Dangdie, I'll give you something with.

But if we subclass the parent class, there are two identical variable names
such as:

class Fu{
    int num = 3;
}
class Zi extends/*继承*/ Fu{
    int num = 5;
    void show()
    {
       System.out.println(this.num+"........"+super.num);
    }
}

Then we need to use the keyword: superto the parent molecule district
where you can write this omission, but in order to distinguish wrote out

Java,

本类的成员和局部变量同名用this区分.
当子父类中的成员变量同名用super区分父类.

If we in the general privatization of variables, we can use the method of access by superkeyword method name.
For example:

class Fu{
    private int num = 3;
    public int getNum()
    {
    	Systme.out.println(num);
    }
}
class Zi extends/*继承*/ Fu{
    int num = 5;
    void show()
    {
       System.out.println(this.num+"........"+super.getNum());
    }
}

But we will not define the actual development of the same type of variable name in the parent class and subclass, as long as the parent class has, we directly on it.

This is generally in the interview will be asked !!!
.
.
.

this usage is very similar and super

this:代表一个本类对象的引用
super:代表一个父类空间

Why not the same like it?
Depends on the specific way in which memory:
do with this example

class Fu{
    int num = 3;
}
class Zi extends/*继承*/ Fu{
    int num = 5;
    public void show()
    {
       System.out.println(this.num+"........"+super.num);
    }
}
public class Demo{
	public static void main(String[] args)
	{
		Zi z = new Zi();
		z.show();
	}
}

Here Insert Picture Description
Here Insert Picture Description
With compiled class is loaded into the method area, mian function onto the stack, and then to create an object called z is
Here Insert Picture Description
then generated in the heap space, and is initialized to 0 by default, since there is no object's parent class is created, the parent class num in fact, exist in the sub-category inside Here Insert Picture Description
and then separately initialized
Here Insert Picture Description
after initialization is complete, we will address to the z, z then point to heap memory space
Here Insert Picture Description
and then call the method, so the method to call the district's way into the stack
method there is this belongs to, and it will point to heap memory
and then output, then, this will find its own
Here Insert Picture Description
subclass relationship with the parent class is loaded, so with the super reference
super is representative of the parent class space Fu
Fu parent with the establishment of sub-class member variable, and in space among the heap memory
so when we print super.num, we are looking for the father of the num

Guess you like

Origin blog.csdn.net/weixin_42947972/article/details/102025461