Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java super.getClass() prints "Child" not "Parent" - why is that?

Tags:

java

super

In Java classes and objects, we use "this" keyword to reference to the current object within the class. In some sense, I believe "this" actually returns the object of itself.

Example for this:

class Lion
{
    public void Test()
    {
        System.out.println(this);  //prints itself (A Lion object)
    }
}

In the scenario of a superclass and subclass. I thought that "super" keyword would return the object of the superclass. However it seems that I got it wrong this time:

Example:

class Parent
{
    public Parent(){
    }
}

class Child extends Parent
{
    public Child(){
        System.out.println(super.getClass()); //returns Child. Why?
    }
}

My Quesiton: In the above example, I was expecting the compiler to print out class Parent, however it prints out class Child. Why is this so? What super actually returns?

like image 578
user3437460 Avatar asked Jan 17 '15 17:01

user3437460


People also ask

What does getClass () do in Java?

getClass() method returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.

How does the getClass method work?

getClass() is the method of Object class. This method returns the runtime class of this object. The class object which is returned is the object that is locked by static synchronized method of the represented class.

Can we assign child class object to the parent class variable?

We can assign child class object to parent class reference variable but we can't assign parent class object to child class reference variable.

What is it called when a child class object passes into parent class reference?

This is called subtype polymorphism. Your updated question asks why Child. salary is not accessible when the Child object is stored in a Parent reference. The answer is the intersection of "polymorphism" and "static typing".


1 Answers

A method call using super just ignores any overrides in the current class. For example:

class Parent {
    @Override public String toString() {
        return "Parent";
    }
}

class Child extends Parent {
    @Override public String toString() {
        return "Child";
    }

    public void callToString() {
        System.out.println(toString()); // "Child"
        System.out.println(super.toString()); // "Parent"
    }
}

In the case of a call to getClass(), that's a method which returns the class it's called on, and can't be overridden - so while I can see why you'd possibly expect it to return Parent.class, it's still using the same implementation as normal, returning Child. (If you actually want the parent class, you should look at the Class API.)

This is often used as part of an override, in fact. For example:

@Override public void validate() {
    // Allow the parent class to validate first...
    super.validate();
    // ... then perform child-specific validation
    if (someChildField == 0) {
        throw new SomeValidationException("...");
    }
}
like image 181
Jon Skeet Avatar answered Sep 27 '22 17:09

Jon Skeet