• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

base class and derived class

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lets say we have these classes.
class Tree {}
class Pine extends Tree{}
class test{
Tree tree = new Pine();
}
now, we all know that tree is now an instance of Pine and Tree but actually derives Pine. My question is Pine anyway extends Tree so an object of Pine also gets the characteristics of Tree. so, we can say
Pine tree = new Pine();
why is the necessity of the statement
Tree tree = new Pine();
In which scenario does this kind of object creation arise.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would use this if you wanted to make sure you were only using methods available on Tree. That way, you could change to a different type of tree later without having to change the code.
 
Padma Prasad
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks much, Jeanne.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might like a reading of the "How my Dog Learned Polymorphism" story in the JavaRanch Campfire Stories.
Also, Tony Sintes' article Abstract classes and interfaces practicum includes a decent introduction to the concept and advantages of "programming to the interface" (or abstract base type as the case may be).
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This Concept is actually called upcasting. This is actually done when u want a limited functionality to ur class. Through the base class, u are accessing ur derived class members.
 
Padma Prasad
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dirk,
Thanks much for the URL's. "How my Dog Learned Polymorphism" is real good and fun. Sure anybody can easily understand polymorphism with this.
regards,
Padma.
 
crispy bacon. crispy tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic