user3922145
user3922145

Reputation: 39

Java Inheritance and Objects

In a certain program where the Parent class is the Super of the Child. What is the difference b/n the following.

Parent obj1=new Parent();  
Parent obj2=new Child();  
Child obj3=new Child();  

Upvotes: 0

Views: 164

Answers (5)

GameDroids
GameDroids

Reputation: 5662

The first one is almost obvious: you instantiate an object of the same class. Which means, the object obj1 is constructed/instantiated the same way as any other instance of its class. It has the same member variables and the same methods.

Parent obj1 = new Parent(); 

The next is almost the same as above. Only that as programmers, we know that Child is a child class of Parent and thus has similar properties as its parent. However a child class could overwrite some of this properties and change its functionalities.

Child obj3=new Child(); 

So if you call obj3.parentMethod(), the result might be different to obj1.parentMethod(). But we can't know for sure.

The third one is the actual interesting part:

Parent obj2=new Child(); 

There you have a obj2 that is an instance of class Parent. However you instantiate it like a Child instance, thus it works like a Child instance (e.g. when a method is overwritten) but it does not have all the properties of a child.. well it still has those properties and it will use them....But you will not be able to access any child-methods. So it looks like a Parent but it behaves like a Child.

For any further (and more precise) information you should read this or the links shared in the other answers.

Upvotes: 1

Plaiska
Plaiska

Reputation: 166

obj1(line 1) and obj3(line 3) are really plain old object initializations that you would normally use. obj2(line 2) is what's interesting.It allows you to keep your calling code essentially the same while using the concept of inheritance already explained above to substitute different children and thus exhibit different behavior at run time.(Using the same Parent handle and same method calls using this handle)

Upvotes: 1

insanebaba
insanebaba

Reputation: 36

  1. parent obj1=new parent(); Here the reference variable (obj1) is parent type and so is the object created on heap

    • You can call all and only methods of parent class
  2. parent obj2=new child(); Here the reference variable (obj2) is parent type and the object (new child) created on heap is Child Type

    • When you call a method on obj2, it'd be called from child class, If overloaded in it else it'd be called from parent class.
  3. child obj3=new child(); this is same as point one. Here the reference variable and object are both child type and have nothing to do with parent type

read more at 8.4. Method Declarations -> Inheritance, Overriding, and Hiding and 9.4. Abstract Method Declarations -> Inheritance and Overriding

Upvotes: 1

Techfist
Techfist

Reputation: 4344

Alright, key concept being displayed here is called as Polymorphism. Now coming to your question, what does it mean?

Parent obj2=new Child();

It means you are trying to refer your child through its parent, remember in this case as your child already inherit properties of parent, its is logical that you can access those properties which are know to parent through a reference of type parent(Basically Inheritance and static polymorphism of oops).

To add more to it, Only those properties which are know to parent can be accessed via reference of type parents, as parent does not know extra any thing about functionality child might have exposed, but it does know child is having properties being inheretied by him.

Upvotes: 1

Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

They are three different objects, two of Parent class (super class) and one of Child class (sub class).

Inhertience in Java: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Upvotes: 1

Related Questions