简体   繁体   中英

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: :]

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());

This happens because of the way you are adding child values in parent.

You need to pass these values through the child class constructor. I suggest to you create other construct in Parent where you could receive only myName attribute and in Child class you pass myName (parentName) to Parent using super, like you are using in your get methods. Can do as follows:

Add (or change) a new construct in Parent :

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

And in Child :

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

And in onCreate method you just need create a child instance, like this:

Child child = new Child("Logan", "Karrie"); 

In this way, the Parent class doesn't need to know and contain the Child class variable.

As others have said, the use of inheritance here is probably not the right way to implement your desired functionality. The reference to the java tutorials will definitely help you understand inheritance and differentiate it from composition or aggregation. Your example uses both at the same time, making it very difficult to grasp the concepts as a beginner. If you are still interested in a hierarchical relationship (parent/child, not super/sub class), take a look at this interface and its implementations from the JDK. There are methods to get parent and get child objects in the hierarchy. https://docs.oracle.com/javase/8/docs/api/javax/swing/tree/TreeNode.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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