File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding

 
Renjith Mohan
Ranch Hand
Posts: 65
Android Chrome Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

Thanks in advance
Renjith
 
Rahul Nair
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes David, you are correct! I have not seen it. Sorry for mislaid.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Nair wrote:Yes David, you are correct! I have not seen it. Sorry for mislaid.

me too :shock:
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Henry
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically ...you can't override .. class A is different and class B is different ....(yea..No inheritance)...



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic