• 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

A question on object !!!

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose there is a class called Animal. Dog extends the Animal class. Can anyone explains what is the use of this kind of object ?

Animal a=new Dog2();

Cheers,
-Biswa
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think :
first "new Dog2();" means creat a new object of the subclass-Dog2 with its constructers:
second "Animal a" a is the new object's reference ,its cast is Animal
third "=" is evaluation and upcast
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By creating a Dog2() object using

Animal a = new Dog2();

We are saying that the Dog2() object behaves like an Animal. The advantage would may be later you could change the object Dog2() to some object Dog3 () and refer it as

Animal a = new Dog3 ();

still the code using "Animal a" need not be changed. We are ensuring here that both Dog2() and Dog3() behave like animal.

Thanks
Chandu
 
Honk if you love justice! And honk twice for tiny ads!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic