• 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

Sub Class Error

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following piece of code creates a run time error at line 2. As we are casting from base to sub (going down the tree) we need a cast. Line 2 has a cast. However if I change line 1 to Base1 base = new Sub() it works. I think I am missing a fundamental understanding of casting.

class Base1 {}
class Sub extends Base1 {}
public class Orange {
public static void main(String args[]) {
Base1 base = new Base1(); // 1
Sub sub = (Sub)base; // 2
}
}
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi david,

Consider changing the two lines from :

Base1 base = new Base1();
Sub sub = (Sub)base;

To :

Base1 base = new Sub();
Sub sub = (Sub)base;

In this case the code compiles fine and runs without throw any exceptions.

Your original code is incorrect from conceptual point of view.

You simply cannot downcast base variable to Sub class at runtime just because your object crated on heap doesn't contain the members needed by Sub class. So this reference cannot fit into Sub class.

On the other hand, if you replace the code for the above provided, I can downcast just because the memory object contains all members needed by Sub class.

Hope that helps.

Please let me know if you still cannot figure out the explanation.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Althrought both classes don't have any mambers, they have different definitions, so the above epxlanation is still valid for this scenario.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this line:

Sub sub = (Sub)base; // 2

sub if of type Sub, so sub can refer to an object of Sub or subclasses of Sub. In line 2 you are trying to assign it to its superclass and thus it fails (therefore your other line of code works). Compilation works because it checks to see if both classes are in the same inheritence tree or not.
 
Let nothing stop you! Not even this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic