File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Question about inheritance (assigning references). Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question about inheritance (assigning references)." Watch "Question about inheritance (assigning references)." New topic
Author

Question about inheritance (assigning references).

Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hello,



This is allowed while



is not. Could anyone please give the reason as to why?

Thanks,
Raj.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18123
    
  39

You really should name your classes something other than Parent and Child, if you want to understand inheritance in this regard... but... the reason the first is allowed is because a Child IS-A Parent, but a Parent may not be a Child.

This would make better sense, if you named parent and child to something else... Maybe name the parent class the Dog class. And name the child classes the Beagle class, the Akita class, the Terrier class, etc... The first is allowed because the Akita is a Dog, but a Dog may not be an Akita.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Thanks,

I do understand the base class - child class relation and the idea of inheritance. I am interested in knowing what goes on behind the scenes. The real reason, I mean. Why is it that we can assign the derived class instance to that of a parent class while the other way round is not permitted?


Thanks,
Raj.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18123
    
  39

Raj Kamal wrote:
Why is it that we can assign the derived class instance to that of a parent class while the other way round is not permitted?


Another way to think about it is that a Child is a specialized version of a Parent. It can behave like the Parent, and be used everywhere a Parent is used, plus it has extra stuff / features. A Parent cannot be used in place of a Child because it doesn't have the stuff that is specific to a Child.

Henry

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about inheritance (assigning references).
 
Similar Threads
difference in instances
Overriding/overloading
Question on Super
query on method over riding
Object from Subclass to Superclass