• 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

Why can't we assign parent class object to child class reference?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the basic concept of OOP's in java. But I want to know the reason behind this as in case of primitives we know that it is the loss of precision, when we assign double to integer. But is this applicable to java classes also ?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On a lighter side- Cat, Dog, Animal- Best example to explain such situations
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Simple Terms, Objects of Parent class can hold objects of child class. The reverse could not be true.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Er. Rohit Sharma wrote:This is the basic concept of OOP's in java. But I want to know the reason behind this as in case of primitives we know that it is the loss of precision, when we assign double to integer. But is this applicable to java classes also ?


No. Primitive are NOT objects, so you can't make any anaolgies between the two. The rules for assignment are governed by a totally different set of rules called "widening" and "narrowing" primitive conversion.

Winston
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reference type kind of holds the methods you can call. Bert and Kathy explain it as being similar to a remote control for a TV. The subclass may have a 'picture-in-picture' method. If you could point it at a plainTV, when you push the p-i-p button, the TV wouldn't know how to do that.

extending Rob's code, say that the Dog class had a bark() method. If you create an Animal, it does not have a bark method implemented, but the reference says you can call the bar() method. This is clearly a problem, so it's not allowed.
 
I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic