+6

a superclass reference variable can refer to a subclass object , but what is it's practical use?

Checked many websites ,stackoverflow ,books but I can't really understand this concept. ●1.we can simply use a subclass reference for using a subclass object.so why use base class reference? ●2.well every tutorial/book etc says runtime polymorphism is powerful feature but how? why do we even need it? it's "slower" than compile time polymorphism. I haven't found any source that "really" explains why they call it "powerful". One more thing that is often told "flexibility". what does it even mean in this context? ●3.what really makes it unable to happen at compile time ? why methods of subclass called upon base class reference can't be resolved at compile time? All these questions arise because of this : " a superclass reference variable can refer to a subclass object ". Only use I know is that both super and sub class objects can be stored in an array of type super class. That's all. Where have you used elsewhere? ~ swim ~ , John Wells , David Carroll , Shadow 🙏

12/7/2019 5:17:02 PM

🇮🇳Omkar🕉

4 Answers

New Answer

+5

1. super class can refer subclass reference. This is called upcasting or object slicing and is required for polymorphism to work. This allows calling base clsss methods without knowing the exact type of derived class object. use case - say you have different types of windows object in your program like tabed window, floating window, docked window etc. Now you process some data and the result needs to be updated in all those windows. now you have "update" method in base class which is overridden in subclasses. Also assume you have window manager class whoes one of the task is to update various windows objects (kept in the list) How to do it? 1. for each object in the list, check its concrete class and call the overridden method. if (object isinstance is docked_window){docked_window_obj.update();} else if (object isinstance is floating_window) { floatw.update(); }. As you can see if there are many windows types then there is code clutter, has maintainence issue, scalability issue, you need to check each object type and call method on it. Enter polymorphism: You have a base class method "update". Derived classes override it. On data change, call update method on each element of list in a loop. The compiler will correctly bind the right class object. This results in code clutter, flexible since if you want to add a new class, all you need is to override the base class method. Runtime object call is always a bit slow, since you need to resolve the correct object type and call method on it. There is extra bopmkeeping to do. Java does not support compile time function call dispatching beside that at compile time how would the compiler know which type of concrete window subclass object will be created.

+2

Taste thanks I'll read that. Avinesh , Super class reference can invoke sub class version of methods that are present in superclass and overridden by sub class. What you said is also valid, we can't invoke superclass methods through super class reference unless we have method with same signature in super class to override them in subclass. As you know this isn't what I asked btw. I wanted know in particular why do we even use super class reference as sub class reference can do work. I appreciate your help :) Thanks again.

+1

i know an article about this from SO. let me search for it https://www.javaworld.com/article/2073649/why-extends-is-evil.html from https://stackoverflow.com/questions/147468/why-should-the-interface-for-a-java-class-be-preferred

+1

Not sure how relevant it is, I'm just sharing my view on this. When we create a reference to the super class and create an instance of the subclass, the object can invoke only the members defined in the super class. If at all some object doesn't need any new behaviour added by the subclass then isn't that useful that we give the reference of superclass.