0

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();  
5
  • 1
    Difference in which aspect ? Commented Nov 3, 2014 at 7:04
  • The methods will be invoked based on the Object type and the fields will be inferred based on the reference type.. Thats the difference. Commented Nov 3, 2014 at 7:06
  • obj2 is type parent and instance of child. What differences it have from obj3. (any aspects) Commented Nov 3, 2014 at 7:07
  • If you have problems with some homework, maybe you want to tell us what you think first? :) On a side note: classes should have a name starting with a capital letter (Parent obj1= new Parent()) Commented Nov 3, 2014 at 7:08
  • Not a homework just trying to understand. Thanks and for the naming, just edited. Commented Nov 3, 2014 at 7:14

5 Answers 5

1
  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

Sign up to request clarification or add additional context in comments.

1 Comment

The exact answer I wanted. Lots of Thanks!
1

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

Comments

1

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.

Comments

1

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)

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.