0

This is from docs

If you consider the above program, you can instantiate the class as given below. But using the superclass reference variable ( cal in this case) you cannot call the method multiplication(), which belongs to the subclass My_Calculation.

Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);

My doubt is :

What is use for assigning subclass object to a superclass variable , If I cant even able to access members of the object

  • Because in your subclass you can override some methods from superclass and make them do a little different things. Then you can pass that class to some function which can't be modified by you but it will use your overrided method – Morph21 Sep 10 '20 at 09:31
  • @luk2302 mentions the main reason, programming for the most general notion, so the program covers the most, and objects can be implemented differently. This no longer holds when functionality is added by extending a class. That however is a questionable use of inheritance. Java `String` cannot be extended (fortunately), as it would certainly have invoked numerous child instances. – Joop Eggen Sep 10 '20 at 09:43

0 Answers0