-2
Parent parent = new Child();
parent.someMethod();

In what situations,assinging like above is useful or what is the logical explanation behind it? instead of the following:

Child child = new Child();
child.someMethod();

In both the cases,Child's method is only going to be called,anyway.So,why this kind of representation is allowed? Isn't it an unnecessary feature allowed in the design?

The only reason I could come up with,depending on some fiddling I did is that,it lets you know that "someMethod()" is not available in the Parent if it is not present in the Parent,at the time of writing "parent.someMethod()"!

Can you please shed some light as to how it is useful/necessary and give me some perspective related to object oriented concepts? I know it relates to Polymorphism here.But I don't find any necessity to use such representations.

Practical real-world example is much appreciated. Thanks.

I check the following SO question,but I don't feel like I have the answers I am looking for. Using superclass to initialise a subclass object java

Community
  • 1
  • 1
user104309
  • 690
  • 9
  • 20
  • This is explained in basically every introductory Java book. The answer is indeed "polymorphism". – Oliver Charlesworth Nov 24 '14 at 21:40
  • http://stackoverflow.com/questions/13640833/why-different-types-of-object-reference-is-allowed-in-java Check this question. – Salih Erikci Nov 24 '14 at 21:43
  • Can you please provide a practical example where parent.someMethod(); is logically better than child.someMethod();? – user104309 Nov 24 '14 at 21:46
  • 1
    @user104309 - Certainly - when a parent class has multiple possible child classes, now and in the future -- and you want code to work for all of them. – Andy Thomas Nov 24 '14 at 22:11

1 Answers1

2

It gives you the oppurtinity to behave same to different objects.

For example Object class. Object has a toString() method. And every class extending Object has toString() method. (Every class implicityly extends Object class.)

Now , imagine you have the following objects from different classes. A person(Person class), a car(Car class), a lamp(Lamp class).

You want to write a method that calls each object's toString() method. You have to write 3 different version of this method.

callToString(Person p) {p.toString();}  
// A method that accepts person objects.    

callToString(Car c){c.toString();}   
// A method that accepts car objecs.  

callToString(Lamp l){l.toString();}   
// A method that accepts lamp objects.

We know that each 3 classes extends from Object class and Object class has toString() method. We can write a method that accepts Object class objects.

callToString(Object o){o.toString();}

Now as this method accepts Object class object, we can use it with Car, Person and Lamp because all of them extends from Object class.

Usage of the new method.

Person john = new Person();
Car ferrari = new Car();
Lamp philips = new Lamp();  

callToString(john);
callToString(ferrari);
callToString(philips);
Salih Erikci
  • 5,076
  • 12
  • 39
  • 69
  • I am able to get a perspective when to you took the Parent Class' Reference out to an argument in a method and pass multiple Child Class' Objects as parameters. It reduces the method calls and the same code can be reused across all the other possible Child Classes.But when it comes to `Person per=new Person(); Object obj=new Person(); `I am not able to understand the difference between `per.toString()` and `obj.toString()` – user104309 Nov 25 '14 at 10:17
  • both per.toString() and obj.toString() calls the same method. – Salih Erikci Nov 25 '14 at 10:42
  • I get it.Then I can go for `per.toString()` itself instead `obj.toString()` right? I think I am either trying to think too deeply unnecessarily or I have to work up my intuition.By the way,I meant "reduces the number of method declarations" and not "reduces the method calls",in my previous comment. – user104309 Nov 25 '14 at 10:49
  • 1
    Yes you can. As i said obj.toString is used when obj is from many different subclasses and You want to treat same to all of them. – Salih Erikci Nov 25 '14 at 11:00