• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

reference variable for class

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the use of assigning subclass reference to super class reference
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Does this make sense?
reply
    Bookmark Topic Watch Topic
  • New Topic