DSN94
DSN94

Reputation: 304

What is the difference between "ParentClass objectName1= new SubClass() " and "SubClass objectName2 = new SubClass()"?

I was wondering if there is any functional difference between those two ways of creating an object in a class that extends from another.

I'm just working with basic Inheritance, nothing complex beyond Cat extends Mammal and that kind of things.

Upvotes: 2

Views: 1144

Answers (5)

Ramesh Kotha
Ramesh Kotha

Reputation: 8322

ParentClass objectName = new SubClass() - parent class reference is holding child class object

class ParentClass{
 void m1(){
   System.out.println("parent m1");
  }
 void m2(){
   System.out.println("parent m2");
  }
}

class SubClass extends ParentClass{
//subclass overriding parent class m1 method
 @Override
 void m1(){
   System.out.println("child m1");
  }
 void m3(){
   System.out.println("child m3");
  }
}

class Test{
public static void main(String[] args){
  ParentClass p = new SubClass();
  p.m1();// out puts child m1 as child class is overriding m1 method
  p.m2();//out puts parent m2 as child class is not overriding m2 method
  p.m3();//gives compile time error as parent class reference doesn't know about m3 method

}
}

Upvotes: 3

Pankaj Arora
Pankaj Arora

Reputation: 10274

case1:

   ParentClass objectName = new SubClass() //  use only when you need some features of parent class.

1). objectName (refrence of your parent class) is having the object of Subclass.

2). in this case you can access the public methods of your parent class,but cannot access the methods of subclass.

case2:

SubClass objectName = new SubClass() // use only when you need features of subclass.

1). object created in both cases are same.

2). you can access both (methods of superclass and subclass).

Upvotes: 1

Andre Holzner
Andre Holzner

Reputation: 18675

It depends on what you want to do:

  • ParentClass objectName = new SubClass() is typically used when you only need the features of ParentClass and don't care about the actual implementation SubClass. A prominent example of this is containers such as List where one often does e.g. List<String> objectName = new ArrayList<String>(). What one needs is a list and in the remainder of the code does typically not care about how the list is implemented. In your case, if all you need is the features of the Mammal you can use this and your code will also work the day you want to use it with Dog instead of Cat. In the spirit of generalization, you could always try to use the most generic type (oldest ancestor so to say) possible. Some integrated development environments actually have a function to try to replace the types of variables by the most generic type which does not break the code.

  • SubClass objectName = new SubClass() on the other hand lets you use all the details of SubClass in the rest of the code without having to do any type cast (which may in principle fail for the previous case if some part of the code assigns a different object which is not SubClass or a descendant thereof).

Upvotes: 3

Mureinik
Mureinik

Reputation: 311393

The instance returned bynew SubClass() is not effected by the type of reference you assign it to - the same constructor is run, and you get back an object with the same functionality.

Assigning this newly created object to a reference higher-up in the hierarchy is usually used to signify that you won't use any SubClass specific methods, and your code is not tied down to assuming what subclass you're using.

Upvotes: 2

saurabh kumar
saurabh kumar

Reputation: 164

First statement reference ObjectName1 is referring to child class, where as in the second ObjectName2 is referring to its own object, if you call the methods of parent object it will call the methods of child class object if its present

Upvotes: 0

Related Questions