Data Ownership between super class and sub class

When a subclass inherit a super class , what's the relationship between subclass and the fields with different accessibility defined in super class ,which field is owned by the subclass and which not. And can someone explain the example below:

class Parent
{
  private int partOne = 0;
  public int partTwo = 1;

  public void tellMe()
  {
     System.out.println(partOne);
     System.out.println(partTwo);
  }
}
class Child extends Parent
{
  private int partOne = 2;
  public int partTwo = 3;

  public void main(String[] args)
  {
   Child child = new Child();
   child.tellMe();
  }
}

When I debugged it in Eclipse , I watched the variable child in the stack , I found the Child instance has four fields , two defined by its class and two by its parent class , I was very confused.Did I create a parent instance when I create a child instance , IMP , I think it only enter the parent class's constructor, but I can't explain why the child class don't override the two fields , or at least , I think it should override the partTwo with the public accessibility .Please tell me what is the reason, thanks.

Answers


why the child class don't override the two fields , or at least , I think it should override the partTwo with the public accessibility

In Java, data members are not polymorphic. This means that in your example, the two fields defined in the Child class are completely unrelated to the two fields in the Parent class, even though they happen to share their names.

Although this is allowed by the language, doing things of this nature is just a recipe for confusion (as you've discovered).

If you want polymorphic behaviour, create two member functions, getPartOne() and getPartTwo(), and override them as appropriate.


What happens is that you're hiding the access to the field partTwo of the Parent class by redefining it with the same name on the Child class. You should actually get a warning from the compiler about that.

Do the following experiment to see what's happening:

  • Add a proper constructor to both classes, calling super on the Child's constructor (yes, that's not necessary, but it can help make things clearer).
  • Initialize your fields in the constructor instead of directly doing that in the field declaration.
  • So far the code will do the same.
  • Now remove the partTwo variable from the Child class - it will still compile but this time the value set by the constructor of Child will be applied (and the warning will disappear).

You may have seen the comment I posted about encapsulation - having only private fields prevent such confusion and allows for better maintenance of your classes later on, because you can change the fields of a class without affecting inheriting classes (as long as the methods to access the data remain unchanged).


Need Your Help

Sending php data to java desktop application

java php

I would like to send data from a php script to a java desktop application. The php data is retrieved from a database in a SELECT * FROM xxxxx. I would like to pass these values to java for displayi...