Reputation: 93
Experts I need a bit of help here to understand what I am not able to understand. I am creating a super class and a sub class, I am trying to access the super class instance variable through subclass, but I always get null. Unless I explicitly assign value to the 'name' variable in super class.
package example1;
public class InterfaceExample {
public static void main(String[] args) {
Person person = new Person();
person.printinfo("WOLFSKIN", "Test Address");
Boy boy = new Boy();
boy.info();
}
}
class Person {
String name;
String address;
public void printinfo(String name, String address) {
this.name = name;
this.address = address;
System.out.println("Name: " + name + '\n' + "Address: " + address);
}
}
class Boy extends Person {
public void info() {
System.out.println("Subclass Name " + this.name);
}
}
Upvotes: 0
Views: 849
Reputation: 1243
From your example, you appear to think that this
returns the instance of your super class, when it actually returns the instance of the class it is in. In your example, this
refers to your Boy
class and its fields, when you seem to want it to refer to Person
. Boy
does not have the same name
as Person
unless you add:
Boy boy = new Boy();
boy.name = person.name;
boy.info();
I am not an expert on this, but I believe you could also do, in your info()
method:
this.name = super.name;
System.out.println("Subclass Name " + this.name);
You may need to make name
a static variable in Person
if you use the second approach, but I am not sure (if anyone knows please edit/comment). It is probably safer to use the first one, because then you can have multiple instances of Boy
each with a different name. Anyone reading this who knows more or knows if I am incorrect in any of this, please edit/comment any corrections.
Upvotes: 0
Reputation: 1075219
You never assign any value to name
in your Boy
instance, I've added some explanatory comments:
// Creates a Person (not a Boy)
Person person = new Person();
// Sets the name on that Person
person.printinfo("WOLFSKIN", "Test Address");
// Creates an *entirely separate object*, an instance of Boy
Boy boy = new Boy();
// Accesses `name`, which has never been set
boy.info();
To set name
on your Boy
instance, call printinfo
on your Boy
instance:
boy.printinfo("WOLFSKIN", "Test Address");
Let's throw some ASCII-art at it:
Person person = new Person();
gives us:
+-----------+ +-----------------+ | person |---->| Person instance | +-----------+ +-----------------+ | name: null | +-----------+ | address: null | | boy: null | +-----------------+ +-----------+
Then after:
person.printinfo("WOLFSKIN", "Test Address");
We have:
+--------+ +-----------------+ +-----------------+ | person |------>| Person instance | | String instance | +--------+ +-----------------+ +-----------------+ | name |--->| "WOLFSKIN" | | address |-+ +-----------------+ +-----------------+ | +-----------+ | +-----------------+ | boy: null | | | String instance | +-----------+ | +-----------------+ +->| "Test Address" | +-----------------+
Now we do:
Boy boy = new Boy();
And get this:
+--------+ +-----------------+ +-----------------+ | person |------>| Person instance | | String instance | +--------+ +-----------------+ +-----------------+ | name |--->| "WOLFSKIN" | | address |-+ +-----------------+ +-----------------+ | +-----------+ | +-----------------+ | boy |-+ | | String instance | +-----------+ | | +-----------------+ | +->| "Test Address" | | +-----------------+ | +-----------------+ +->| Boy instance | +-----------------+ | name: null | | address: null | +-----------------+
Note that the Boy instance is made up of the fields of its superclasses (Person
, in this case) as well as its own fields (but it doesn't have any).
Since you've never filled in a value for boy
's name
, it still has the default null
.
Upvotes: 3
Reputation: 48434
You are referencing name
from an instance of Boy
, but it's been assigned in an instance of Person
, hence it's null
.
You can obviate that by adding:
boy.printinfo("WOLFSKIN", "Test Address");
Upvotes: 0