aspose file tools*
The moose likes Java in General and the fly likes Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Overriding" Watch "Overriding" New topic
Author

Overriding

Renjith Mohan
Ranch Hand

Joined: Nov 28, 2008
Posts: 65



Even if I downcast to class B, it fails. What exactly is the reason behind this?

Thanks in advance
Renjith
Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138
It is natural that, B can do all the stuff that A can, But the reverse is not true. As 'obj' reference is is pointing to the Object of type A, So if 'obj' is calling the B specific stuff on object of type A, then obviously it will not be able to do it. because it is referring the object of type A. That is why, it fails.

The downcast is fails, because A never does the B specific stuff.

Please check it here, the question is already answered.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Renjith Mohan wrote: What exactly is the reason behind this?


B is-a A , but reverse not[A is not B].
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There is *no* relationship between A and B other than they both declare a method of the same name.

There are no subclasses, no interfaces, is-a relationship. They are completely unrelated classes, hence no type conversion is possible.
Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138
Yes David, you are correct! I have not seen it. Sorry for mislaid.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Rahul Nair wrote:Yes David, you are correct! I have not seen it. Sorry for mislaid.

me too :shock:
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


The original answer "A is NOT B" still stands. This just adds "B is also NOT A" to that conclusion.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Arun C. Giridharan
Ranch Hand

Joined: Jul 11, 2010
Posts: 96
basically ...you can't override .. class A is different and class B is different ....(yea..No inheritance)...



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding