2

I want to instantiate a child class in Java from within a parent class. However, when I do this, and attempt to call a parent's method from the constructor of the child (with super), the returned field is null. If I changed this to instantiating the parent from the main() method, the field returns how it is expected (a String). I'm not sure whats happening here, can anyone explain?

Main class:

public class MainFunc {

  public static void main(String[] args) {
    javaClass jv = new javaClass("Bobby");
    jv.makeJ2();
  }
}

Parent Class:

public class javaClass {

  String name;

  public javaClass(){
  }

  public javaClass(String s) {
      setName(s);
  }

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }

  public void makeJ2 (){
    javaClass2 jv2 = new javaClass2();
  }
}

Child Class:

public class javaClass2 extends javaClass {

  String name;


  public javaClass2() {
    super();
    String superTitle = super.getName();

    System.out.println("HEY " + superTitle);
  }
}

This returns:

HEY null

Instead of:

HEY Bobby
  • The class of an object that is used to instantiate another object is *not* its super class. – Robby Cornelissen Jun 18 '18 at 04:33
  • Every java program runs through main method. It must have main method. – Gaurav Srivastav Jun 18 '18 at 04:35
  • If you want out as 'HEY Bobby' you have to make name field as static 'static String name;' because nun static variables are related to the object so same value not have in different objects – janith1024 Jun 18 '18 at 04:46
  • 1
    @janith1024: nooooooo! Please don't tell beginners to make things static. Christophe: my advice is use a debugger to examine what fields are part of each object you instantiate. Then maybe run through the Java tutorial again. – Nathan Hughes Jun 18 '18 at 04:55

6 Answers6

1

Design wise, a parent should never instantiate a child class. It is not like human reproduction system. In OOPS world, child classes need to declare their own parents, and only these child classes know about their parents and not vice-versa.

Even though intention in the posted question is to make use of Inheritance, it is not happening by the virtue of the convoluted code. This is how the code is running:

  1. Test creates a javaClass object named jv. At this point jv has an attribute name, value of which is set to Bobby
  2. jv's makeJ2 method is called, this creates a very new object of the class javaClass2, named jv2. The parent class of this very new object does NOT have any field set, and nothing has been passed to the parent class's constructor. Hence there is NO relation between the parent of this new object jv2 and the previously created jv object and that is why:
  3. String superTitle = super.getName(); returns null as expected

The exact problem is that the child object is not passing along any information for the parent's attributes to be set. That can happen through overloaded supers or by setting super properties but not just by calling super(). See a good explanation of how inheritance works in java.

Please do not use static just to make it work

Lastly, I suggest reading about composition too, as that is slightly more preferable over inheritance, for some good reasons.

gargkshitiz
  • 2,130
  • 17
  • 19
1

You cannot access child class from parent class,child class has inherited the parent class, not the other way. But you can make your String static for it to work the way you want.

public class javaClass {

  static String name;
Madhu
  • 75
  • 7
0

In your child class you did not overload the constructor for name field. From the overloaded constructor you should invoke super(name);

Gaurav Srivastav
  • 2,381
  • 1
  • 15
  • 18
0

The output that is generated is because of two reasons.

  1. Because you have called super() in the javaClass2 constructor and not super(String str)
  2. And because the parent java class that the child class is instantiating is not the same as the one you are calling the method makeJ2(jv.makeJ2()) from.

Also the blow link can help you understand the instance variable overriding in java. Java inheritance overriding instance variable [duplicate]

user7780
  • 31
  • 5
0

Base on your progress:

  1. You initiate the parent class:

    javaClass jv = new javaClass("Bobby");

  2. javaClass name attribute will be "Bobby"

  3. Now the time you call:

    jv.makeJ2();

  4. It will initiate the new javaClass2:

    javaClass2 jv2 = new javaClass2();

  5. It call the super(); mean: javaClass() in javaClass not javaClass(String s)

  6. So now your new child javaClass2 is extended from new javaClass wiht its name is new (null).

If you want javaClass2 print "Buddy", you should:

public javaClass2(String s) {
    super(s);
    String superTitle = super.getName();

    System.out.println("HEY " + superTitle);
  }
0
jv and jv2 are totally two different objects in the memory.
After all that is the fundamental meaning of "new" operator in Java.
you have used "new" operator twice in your code. 

So it means you have two completely different objects. jv's name is set as "Bobby" but nobody has set a name for the second object jv2 !

Imagine this:

class Manager extends Employee
{
....
public void setPMPCertified(boolean b)
{
...
}
}

//Generally Software engineers
class Employee
{
....
public void setName(String n)
{
.....
}
}

Manager m1 = new Manager();
Employee e1 = new Employee();
m1.setName("Robert");
m1.setPMPCertified(true);
e1.setName("Raja");

Robert is a manager. Raja is a software engineer. 

They are completely two different data (object) in the memory. Just because manager extends employee Robert and Raja cannot become single object. Look at the fact we have used the new operator twice to create two objects. Please note manager does NOT have the setName method. It comes from the parent (Employee). setPMPCertified is only applicable to managers. we don't care if a software engineer is PMP certified or not!! :)

Jinnah
  • 148
  • 10