0

Please help me to understand how inheritance works.

If I have two classes, parent and child. When I create an instance of child is parent class instance constructed as well or not?

My code of Parent class.

public class Parent {
    private char s;

    @Override
    public int hashCode() {
        return s;
    }
}

And Child

public class Child extends Parent {
    private int i;

    public Child(int i) {
        super();
        this.i = i;
    }

    @Override
    public int hashCode() {
        return i;
    }
}

And finally the test

public class Main {
    public static void main(String[] args) {
        Child child = new Child(100);
        System.out.println(child.hashCode());
        System.out.println(child.getClass().getSuperclass().hashCode());
    }
}

In output I get

100
2000502626

So the hashes of objects are different. It means that when I create instance of Child it is also created instance of Parent. Am I right?

barbara
  • 3,111
  • 6
  • 30
  • 58
  • 2
    A child _is a_ parent. It **is the same instance**. This is how inheritance works - they're the same object. – Boris the Spider Aug 22 '14 at 11:32
  • 2
    The second `hashCode()` call does not call the method of `Parent` but the method of the `Class` object that represents the class of a `Parent` object. – stevecross Aug 22 '14 at 11:32
  • @Boris the Spider In this case how is it possible to get access to parent methods? – barbara Aug 22 '14 at 12:33

5 Answers5

10

Your question has nothing to do with inheritance.

the 100 you get from child instance's hashcode() method, as you expected.

The 2000502626 was from Parent.class, not Parent object.

Parent.class has type java.lang.Class

parent object has type Parent

Kent
  • 189,393
  • 32
  • 233
  • 301
3

When you create a Child object a Parent constructor is invoked as well, because a Child is a Parent.

But when you do this:

System.out.println(child.getClass().getSuperclass().hashCode());

you're not invoking Parents instance hashode. You are invoking hashCode() of the instance of the Class object. See what child.getClass().getSuperclass() returns. It returns an instance of type Class not of type Parent/Child. You cannot invoke Parents instance methods using child.getClass().getSuperClass() - that doesn't return the instance of a type, but an object representing this type.

Try doing this in child method:

@Override
public int hashCode() {
    System.out.println("In child hashCode: " + i);
    System.out.println("Parents hashCode: " + super.hashCode());
    return i;
}

This will return 100 and 0, as Parents s hasn't been initialized.

Michał Schielmann
  • 1,372
  • 8
  • 17
  • child.getClass().getSuperclass().hashCode() represents the hashcode of which class? – subham soni Aug 22 '14 at 11:46
  • java.lang.Class.class I believe. – Michał Schielmann Aug 22 '14 at 11:49
  • @MichałSchielmann I think it will get the hashcode of `Parent.class` (which class is `java.lang.Class`) – WilQu Aug 22 '14 at 11:56
  • @WilQu - see what child.getClass().getSuperclass() returns. It returns an instance of `Class.class`. http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html This class inherits the `hashCode()` method from `Object.class`, and in `Object.class` this method is (in openjdk) `native`: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/lang/Object.java. For native methods see: http://stackoverflow.com/questions/6557358/native-methods-in-java – Michał Schielmann Aug 22 '14 at 12:04
  • @MichałSchielmann Where do you see that `getSuperclass()` returns `Class.class` ? It returns the `Class` object representing the superclass of `Child`, which is `Parent.class`. – WilQu Aug 22 '14 at 12:11
  • @WilQu Please refer to the docs: http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html Method signature: `public Class super T> getSuperclass()`. And here `Class` is `java.lang.Class.class` So in the question `getSuperclass()` will return `Class` and that is still an instance of type `Class` and not of type `Parent`. – Michał Schielmann Aug 22 '14 at 12:24
  • PS - see @Kent answer. It states the same. – Michał Schielmann Aug 22 '14 at 12:24
  • @MichałSchielmann `Parent.class` is not an instance of type `Parent`, but of type `Class`. And `java.lang.Class.class` is not an instance of type `Class` but an instance of type `Class`. – WilQu Aug 22 '14 at 12:37
  • Where did i write that `Parent.class` or `Class.class` is an instance? I'll write this one last time: `getSuperclass()` will return object of type `Class extends Parent>` and that is an instance of type `Class`, not `Parent`. – Michał Schielmann Aug 22 '14 at 12:46
1

Actually, there will be just one child object created. Since every child is a parent, the parent constructor will be invoked. if you print this in both child as well as parent instance methods, it will print the same (child object) check - this question

Community
  • 1
  • 1
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • 1
    Are you sure that there are two objects being created and not one? – Michał Schielmann Aug 22 '14 at 11:51
  • @MichałSchielmann - well, actually what I meant was - *Both parent as well as child object* are created. yes, there will be encapsulated in the child object itself.So, I meantioned - *So, there will be one big (sub-class) object (this) representing/containing both the objects.*. Maybe I should have been more precise. – TheLostMind Aug 22 '14 at 13:51
  • Still - both means two. Are there two objects or one? – Michał Schielmann Aug 22 '14 at 13:53
0

If I have two classes, parent and child, When I create an instance of child is parent class instance constructed as well or not?

Yes. It works through Constructor. When you call constructor of your child class to create object, it first calls its parent class contructor and hence creates object of parent class

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
Adi
  • 2,364
  • 1
  • 17
  • 23
  • It calls its parent class constructor to initialize itself but only one object is created. – WilQu Aug 22 '14 at 11:53
  • But if I call in some of my child class methods super.hashCode() I get different hash than my object has. – barbara Aug 22 '14 at 12:53
-1

Yes. Both parent and child object are created. The child class constructor calls the parent(super) class constructor first, then only other functions of the child class are performed. As you can see from your own code two different values getting printed.

Aakash Sigdel
  • 9,060
  • 5
  • 33
  • 38
  • Are you sure there are two objects being created? What does it mean and how is it relevant: "then only other functions of the child class are performed."? The question was why are they different, and your reply does not answer that correctly. – Michał Schielmann Aug 22 '14 at 11:59