Reputation: 463
I encountered a problem that confused me, it is the keyword 'super', my test code is like this:
package test;
public class Parent {
private String name;
public Parent(){
this.name = "parent";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void showName(){
System.out.println(this.name);
}
}
public class Child extends Parent{
public Child(){
this.setName("Child");
}
public void showName(){
System.out.println(super.getClass().toString());
System.out.println(super.toString());
super.showName();
System.out.println(super.getName());
}
}
public class Test {
public static void main(String[] args) {
Child d = new Child();
d.showName();
}
}
so the result is like this:
class test.Child
test.Child@2207d8bb
Child
Child
my understanding about 'super' is that it is a reference to the parent instance of current instance, so my expecting output is like 'Parent', from the result , I am wrong, its like the current instance calls the parent method, and 'super' is not parent instance, is my understanding right ? and is there a way that I can get parent instance only initializing the Child class ?
Upvotes: 0
Views: 1113
Reputation: 375
class Animal {
void eat() {
System.out.println("animal : eat");
}
}
class Dog extends Animal {
void eat() {
System.out.println("dog : eat");
}
void anotherEat() {
super.eat();
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Animal();
a.eat();
Dog d = new Dog();
d.eat();
d.anotherEat();
}
}
The output is going to be
animal : eat dog : eat animal : eat
The third line is printing "animal:eat" because we are calling super.eat(). If we called this.eat(), it would have printed as "dog:eat".
Upvotes: -1
Reputation: 737
You have used four print statements invoked using Child object:
System.out.println(super.getClass().toString()); Since the getClass() is final method thus it cant be overridden, so when you invoke super.getClass(), you actually invoke the Object class's getClass() which returns the name of the class of the actual invoking object.
System.out.println(super.toString()); Here again the toString() method of Object class is invoked which returns "getClass().getName() + "@" + Integer.toHexString(hashCode());" However, if you override this method in your parent class you will definitely get that version executed
super.showName(); this method returns "Child".... here's why: When you create an object of Child class the contructor runs as follows-
public Child(){ super(); // this is implicitly called by the compiler even if you did not write this this.setName("Child"); }
So following action take place - 1. Parent class's contructor is called which sets the 'name' to 'parent'. 2. Child class's contructor now overwrites this valus with 'Child'. So the value of instance variable 'name' is 'Child'.
System.out.println(super.getName()); Here as said in point 3 the there is only one object and one instance variable 'name' whose value is 'Child'.
Upvotes: 0
Reputation: 3934
From the javadocs, getClass
returns the runtime class of an object.
The runtime class of your object is "Child".
As you did not override getClass()
(you can't because it's final
), so super.getClass()
acts exactly like getClass()
. getClass
method of the Object class is called.
If you want to print the Parent, call getClass().getSuperclass()
Upvotes: 2
Reputation: 1500725
my understanding about 'super' is that it is a reference to the parent instance of current instance
No - there's no such thing as a "parent instance". When you create an instance, there's only one object created - an instance of Child
, which inherits all the fields and methods from Parent
as well.
super
is used in two ways:
Child.showName
- it's calling Parent.showName
, but on the same instance (because there is only one instance)Upvotes: 8