what is the use of assigning subclass reference to super class reference
posted 10 years ago
I am going to assume you mean to ask what the advantage of upcasting (referring to a child with a reference of the parent's type) is.
If that is the case, then imagine a class Person with subclasses for Man, Woman, Boy and Girl. The class Person defines a method called performDailyRoutine(). Obviously, the different types of person will override the method. Now if we were to have a class Family with a List of Persons, we could Iterate through the List, cast the Objects to Persons and call performDailyRoutine() without having to regard what actual class the Person is. The same goes for classes that implement a certain Interface, so you can also use a reference to an Interface that is implemented by all classes within a collection.