Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

class / subclass.....

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here is a question that appeared in a mock test.
class Y is a subclass of X. Will this compile?
X myX = new X();
Y myY = (Y) myX;
As i am given to understand, a class can be casted(hope i am using right term)to its superclass, but here we have (Y) myX,
where Y is a subclass of X. I think this should not compile.
However the correct answer appears to be is 'will compile' but 'not run'. Can someone shed light on this.

thanks.
 
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah ,u can't downcast superclass to subclass,it compiles well.but if u suppose trying to call a method which is present in subclass but not in super class,then u get Runtime exception not compile time exception.U can upcast the class objects but not downcast.
Anil
 
Rajendra Deshpande
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anil,
My question is 'why should it compile at all' when the logic of
downcasting itself is wrong/notallowed.
rajendra.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Up casting a reference (Notice I said reference and not object) is permitted. It can happen only when the Object is a class or subclass of the object reference.

During compilation, Java only determines if the cast you are duing is in the same tree. It doen't actually try and determine if the Object will result in a violation.
Hope this helps
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suggest u visit http://javaranch.com/campfire/StoryPoly.jsp
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajendra,
My two cents on your question...
If the JAVA compiler didn't allow your syntax, then almost all code that access the Collection Framework would all be broken. The collections store everything as an java.lang.Object. When you get the Object out of the collection, you must cast it to the specific type.
For example,
java.util.List list = new java.util.ArrayList();
list.add(new String("Rejendra"));
list.add(new String("Peter"));
list.add(new String("Anil"));
for (int i = 0; i < list.size(); ++i) {
// Notice the cast, because get() return an Object.
String strObj = (String)list.get(i);
System.out.println(strObj);
}
NOTE, all class subclass java.lang.Object.
-Peter
 
Rajendra Deshpande
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anil, Carl, Mahesh, Peter,
Thank you all for responding to this thread. It has boosted my confidence that much more.
regards,
Rajendra.
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic