简体   繁体   English

如何分配内存,什么存储在哪里? :Java继承

[英]How memory gets allocated and what gets stored where? : Java Inheritance

Hi i have a specific question for inheritance in Java. 嗨,我有一个关于Java继承的具体问题。 following is my code 以下是我的代码

class Parent{
    int x = 5;
    public void method(){
        System.out.println("Parent"+ x);
    }
}
public class Child extends Parent{
    int x = 4;
    public void method(){
        System.out.println("Child"+ x);
    }
    public static void main(String[] args){
            Parent p = new Child();
            System.out.println(((Child) p).x);
            System.out.println(p.x);
        }
}

Now my question is what happens actually behind the scene while running this program. 现在我的问题是,在运行该程序时实际在幕后发生了什么。

  • what gets inherited? 继承了什么?
  • where in the memory location? 内存位置在哪里?
  • why first syso gives 4 and second gives 5?(this i am able to understand at some extent but clarification on above two will help understanding it more clearly) 为什么第一个syso给出4,第二个sys给出5?(我在某种程度上可以理解,但是对上述两个进行澄清将有助于更清楚地理解它)

please guide 请指导

In Java there is no variable overriding and only method overriding . 在Java中, 没有变量覆盖 ,只有方法覆盖

    System.out.println(((Child) p).x);

That line telling point p to the Child and get that x variable. 那条线将p告诉Child并获得x变量。

The line, System.out.println(px); System.out.println(px); telling that print Parent 's x 告诉打印Parentx

It is all about Java inheritance and overriding . 它全部与Java inheritanceoverriding method() in the parent class will override by child's method. 父类中的method()将被子方法覆盖。

System.out.println(((Child) p).x);// here you are invoking child 

Then you will get child's attributes. 然后,您将获得孩子的属性。

And next one is all about Java polymorphism. 接下来就是Java多态性。

 ((Child) p).x // invoking object type, x=4 (object is child ) 

  p.x  // invoking reference type, x=5 (reference is parent)

Refer this links and this one. 请参考此链接 链接

what gets inherited? 继承了什么?

All the public and protected methods are inherited in the subclass. 所有公共方法和受保护方法都在子类中继承。 Fields are never inherited. 字段永远不会被继承。

where in the memory location? 内存位置在哪里?

I've written a blog post about Object creation process in Java. 我写了一篇有关Java中对象创建过程的博客文章 I think you will understand from it better. 我认为您会更好地理解它。

why first syso gives 4 and second gives 5? 为什么第一个syso给4,第二个给5?

Field access is always resolved at based on the declared type of the reference, and not the actual object type. 字段访问总是基于引用的声明类型而不是实际对象类型来解决。 So, px will access the field of Parent , as declared type of p is Parent . 因此, px将访问Parent的字段,因为p声明类型为Parent Whereas, ((Child)p).x will access the field of Child , as you have type casted the reference p to Child , and now the declared type considered is Child . 鉴于((Child)p).x将访问Child的字段,因为您将引用p强制转换为Child ,现在考虑的声明类型为Child

In Java, Always sub class object will be instantiated. 在Java中,将实例化Always子类对象。 Super class constructor is called while instantiating child class only to declare super class variables and allocate memory in sub class object. 在实例化子类时,将调用超类构造函数,以便仅声明超类变量并在子类对象中分配内存。 And By default super class public and protected methods are inherited to the sub class. 并且默认情况下,超类的公共和受保护方法会继承到子类。 So concluding this, Memory is allocated only for sub class. 因此,总结来说,内存仅分配给子类。 You can verify it in local, Using JDK Java Virtual VM to see instance list. 您可以在本地使用JDK Java虚拟VM进行验证,以查看实例列表。

For instance variables in inheritance: Assume two circles in memory 例如继承中的变量:假设内存中有两个圆圈

Inner circle is parent with parent instance variables in it and outer circle is child having child instance variables in it. 内圈是其中具有父实例变量的父级,外圈是其中具有子实例变量的子级。

Child ref can access parent instance variables but vice versa is not true because parent instances do not know about child instances 子引用可以访问父实例变量,但反之亦然,因为父实例不了解子实例,所以反之亦然
https://i.stack.imgur.com/sR1bS.jpg
https://www.java-forums.org/new-java/96742-how-memory-allocated-during-inheritance.html https://www.java-forums.org/new-java/96742-how-memory-allocated-during-inheritance.html

class Test1 {       
    public int gear = 10; 
    public int speed =110 ;
} 

// derived class 
class MountainBike extends Test1 
{ 
    //public int gear = 9; 
    public int speed =11 ; 
    // the MountainBike subclass adds one more field 
    //public int seatHeight;
} 

// driver class 
public class Test 
{ 
    public static void main(String args[]) 
    {           
        Test1 mb = new MountainBike(); 
        System.out.println("Hii"+mb.gear); 

        MountainBike m = new MountainBike();

        System.out.println("Hii123 "+m.gear);
    }
} 

The above code will print 10..10 as a output. 上面的代码将输出10..10作为输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM