Downcasting - Superclass Reference

Explain what is superclass reference to subclass object? Why do we go for downcasting?

Questions by swapnaskmr

Showing Answers 1 - 3 of 3 Answers

We extend a class to provide a more specialized behavior for it in the subclass. For example, consider the class "Vehicle" and imagine a subclass "Bus". All vehicle objects are capable of moving. Bus is a specific type of a vehicle which is quite big and can carry a lot of passengers. A vehicle object cannot assume the functionalities of a bus. For such a case, where we want to perform general functions (like moving) over a very broad collection of classes (like vehicles) even when we have specific objects (like buses), we can use downcasting. It is a way of limiting your capabilities to make sure that all your subclass objects are still capable of doing them.

  Was this answer useful?  Yes

Downcasting means using the super class object into the subclass.And we are cating to our sub class object.
Like let's assume super and sub classes

class BaseClass{
      -------
      -------
      -------

}

class SubClass extends BaseClass{
    ------------
    -----------
  SubClass sc=(SubClass)new BaseClass();
}

Here we are using the super class reference in the type of subclass object.This is called DownCasting.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions