Search code examples
javaobjectsubclasssuperclass

Why is my value null when calling superclass method from subclass?


I'm working with Java and Android Studio IDE. I am trying to call a method in a parent class from it's sub class. I have written a small test routine but I am only getting a null result.

In my main project I have Parent and Child Objects/Classes and I would like to get a value from the the Parent Object/Class called from the SubClass/Object. (House Object consists of Wall Objects and my Wall Object is trying to get the "Wall Type" specified within the House Class. But below is a small example of what I am working with.

I have tried searching for this issue, but reading other people's code a lot of times just makes this issue more confusing for me. I must be misunderstanding how this is supposed to work.

I have tried rewriting how I fetch the data, either by calling then super.getMethod() or Overriding the Parent's method. I have also played with my variables/methods being public/private and protected but have achieved nothing.

My Parent Class:

public class Parent {
    protected String myName;
    protected Child parentsChild;

    public Parent() {}

    public Parent (String myName, Child parentsChild) {
        this.myName = myName;
        this.parentsChild = parentsChild;
    }

    public String getMyName() {
        return this.myName;
    }

    public void setMyName(String myName) {
        this.myName = myName;
    }

    public Child getParentsChild() {
        return this.parentsChild;
    }

    public void setParentsChild(Child parentsChild) {
        this.parentsChild = parentsChild;
    }
}

My Child Class:

public class Child extends Parent {
    String childName;

    public Child() {

    }

    public Child(String childName) {
        this.childName = childName;
    }

    public String getChildName() {
        return childName;
    }

    public void setChildName(String childName) {
        this.childName = childName;
    }

    public String getParentName() {
        return super.getMyName();
    }

    @Override
    public String getMyName() {
        return super.getMyName();
    }
}

My MainActivity:


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        Child child = new Child("Logan");

        Parent parent = new Parent("Karrie", child);

        System.out.println("Parent: My name is " + parent.getMyName());
        System.out.println("Child: My parent's name is " + child.getParentName());
        System.out.println("@Override-> Child: My Parent's name is " + child.getMyName());
    }
}

I am hoping to retrieve the Parent's name as I passed it when constructing the class Parent with "Karrie". Instead I only get "null". What a poor child! :-(

Here is my log:

2019-10-17 20:25:36.594 27040-27040/com.example.android.superclassinheritancetesting I/System.out: Parent: My name is Karrie
2019-10-17 20:25:36.594 27040-27040/com.example.android.superclassinheritancetesting I/System.out: Child: My parent's name is null
2019-10-17 20:25:36.594 27040-27040/com.example.android.superclassinheritancetesting I/System.out: @Override-> Child: My Parent's name is null

Thank you kindly for any help! :]


Solution

  • As pointed out in the comments, inheritance might not be the right approach in this case.

    You can define a single Node class to model the relationship.

    class Node {
    
      String name;
      Node parent;
    
      public Node(String name) {
        this.name = name;
      }
    
      public Node(String name, Node parent) {
        this.name = name;
        this.parent = parent;
      }
    
      public String getParentName() {
        return this.parent != null ? this.parent.name : null;
      }
    
    }
    

    Then in your client code:

    Node parent = new Node("ParentName");
    Node child = new Node("ChildName", parent)
    System.out.println(child.getParentName());