• 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

inheritence reference

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider the following program:

class animal
{
}

class dog extends animal
{
}

so if we declare animal a=new dog();

then is it a animal reference or dog reference and is it a animal dog or animal object!!!
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd consider a an Animal variable (classes should be capitalized) that references a Dog object.
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To create an Object you have to always use new <Class name>. Whatever comes to the left is its reference.

Just to add: an Object occupies memory in the heap while references are in stack.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


OK, this method of creating objects is called: POLYMORPHISM, and it means that you are giving different definition to the same thing.

Going back to your question, Animal a = new Dog(), is going to create a Dog object because you are calling the Dog() constructor.

The object reference in this case a is of type Animal BUT the object itself is of type DOG, there is a big difference between objects and their references and as it was stated by Gupta that objects reside in heap while references in the stack.


reply
    Bookmark Topic Watch Topic
  • New Topic