akr
akr

Reputation: 133

Why super referes to current object in the below Java Example

class Animal {
    String color = "white";
}

class Dog extends Animal {
    void printColor() {
        System.out.println("This Color: " + color); //prints color of Dog class  
        System.out.println("Parent Color: " + super.color); //prints color of Animal class  
    }
}

class TestSuper1 {
    public static void main(String args[]) {
        Dog d = new Dog();
        d.color = "black";
        d.printColor();
    }
}

In the output "Parent Color" is printing "black" which confusing me. It should be "white" right since I have changed the color of Child object only ? Could anybody please explain me the reason.

Upvotes: 2

Views: 103

Answers (5)

Ralf Kleberhoff
Ralf Kleberhoff

Reputation: 7290

The previous answers already explain what's happening in your code.

So what's the use of super.xxx? It means (more or less): take the current this object, but use the method and field declarations of the superclass.

super is mostly used with methods where your method overrides the parent's inherited method - but you don't want to have a completely new behaviour, just add functionality to your parent's implementation.

class Animal {
    String color = "white";
    void printColor() {
        System.out.println("Animal color: " + color); 
    }
}

class Dog extends Animal {
    void printColor() {
        // Animal class prints the color
        // Dog class remarks that it's a dog
        System.out.println("I am a Dog!");  
        super.printColor(); 
    }
}

class TestSuper1 {
    public static void main(String args[]) {
        // declaring d as Dog or Animal doesn't matter
        Animal d = new Dog(); 
        d.color = "black";
        d.printColor();
    }
}

This will print

I am a Dog!
Animal color: black

Upvotes: 0

Mohit Tyagi
Mohit Tyagi

Reputation: 2876

since the field color is defined only in Parent class Animal, then the object of child class Dog will set its value to "black" and child class has no field of name color that's why its value will print "black" in both the cases. But if we define a field String color = "brown"; in Dog class then the output will be print : black color for Dog class and white color for Animal class.

Upvotes: 0

davidxxx
davidxxx

Reputation: 131346

It should be "white" right since I have changed the color of Child object only

You have not 2 objects but 1.

color is inherited in the child class.
Invoking super.color or color is the same thing as you have a single color field.

Add a color field to the Dog class and it will produce a distinct result :

class Dog extends Animal {
   ...
    String color = "white";
    void printColor() {
        System.out.println("This Color: " + color);// prints color of Dog class
        System.out.println("Parent Color: " + super.color);// prints color of Animal class
     }
}

Now you will get two distinct outputs when you write :

Dog d = new Dog();
d.color = "black"; // modify the color field of Dog not which one of Animal
d.printColor();

as the first output refers to the Dog field and the the second output refers to the Animal field.

Upvotes: 3

T.J. Crowder
T.J. Crowder

Reputation: 1074295

There is only one object, and it has only one color member. The only difference between referring to it via this (as you are implicitly; inside printColor, color and this.color are the same thing) or via super is the type of reference you have to it and some details around how that type information is used. You're still referring to the same object. There is only one color member in that object. And since there is only one, and you can access it via this or super, you get the same result either way.

Upvotes: 1

Will Hartung
Will Hartung

Reputation: 118641

color is only declared once, in the super class Animal. Dog itself does not have a slot named color exclusive to itself.

Since Dog is a subclass, it inherits the slot from Animal.

Upvotes: 1

Related Questions