This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interfaces

 
Roll
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
: When i try to compile this
: class Super {}
: public class Sub extends Super {
: public static void main(String[] args){
: Super s = new Super();
: Sub s1 = (Sub)s;
: //s = s1;
: }
: }
: Iam agetting runtime error as ClassCastException
: But the same ClassCastException is not occurring when i try to use the below code in the below program
: Object obj = aBase; //code
: Runnable rn = (Runnable)obj; //code
: Any explanations !!!

: PROGRAM
: : Suppose we have two classes defined as follows:
: : class ApBase extends Object implements Runnable
: : class ApDerived extends ApBase implements Observer
: : Given two variables created as follows:
: : ApBase aBase = new ApBase();
: : ApDerived aDer = new ApDerived();
: : Which of the following Java code fragments will compile and execute without error?
: : a)Object obj = aBase;
: : Runnabel rn = obj;
: : b)Object obj = aBase;
: : Runnabel rn = (Runnable)obj;
: : c)Object obj = aBase;
: : Observer ob (Observer)aBase;
: : d)Object obj = aDer;
: : Observer ob2 = obj;
: : The given answer was only b) why not a).
: : ANY EXPLANATIONs
: : ANY HELP WILL BE APPRECIATED

 
paul wheaton
Trailboss
Pie
Posts: 21736
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't cast a class to something it isn't. A "Super" is not a "Sub" even though a "Sub" is a "Super".
 
Sam Cooper
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could some one please go over this casting example once again.
Any thoughts are greatly appreciated
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sam Cooper:
Could some one please go over this casting example once again.
Any thoughts are greatly appreciated

Okay - here goes. Corey's synopsis 'o casting...
When dealing with two objects that are in an inheritance relationship, it is possible to use a child object wherever you expect a parent object. Take the following example:

A key to remember is that you can always use a child class wherever you can use a parent class. This is because the child class has an "is a" relationship with the parent. In this case, a Dog "is a" Animal. Therefore, you can use a Dog wherever an Animal is expected, like I did in line 1.
When you use an instance of a child class when the compiler is expecting an instance of a parent class, an implicit "widening" conversion is done. However, just like with Java primitives, this conversion doesn't work both ways. Let's continue the example from above...I'll just continue the code in main below:

Line 2 is illegal because I'm trying to use an instance of a parent class where an instance of a child is expected. An Animal is not a Dog - it might be a Cat, or a Bat, or a Rat (I'm such a poet). Therefore, the compiler forces us to say "Yes, the object a1 references is really a Dog and I'll take responsibility for anything that happens." That's what we're doing in line 3. The cast tells the compiler that we know that a1 references a Dog object. In this case, we're right a1 really does reference a Dog object, so this code will compile and run just fine. However, is a1 really referenced an Animal, this code would compile, but it would throw a CastClassException (which "is a" RuntimeException) when you tried to execute it.
So, those are the rules about casting parents and siblings. Now, let's look at how you cast and assign siblings. Look at the following code:

In this code, I'm casting a Dog to a Cat and vice versa. THIS IS ILLEGAL! This code will not compile. A Dog is not a Cat and a Cat is not a Dog, even though they are both animals. In Java, siblings are not really related in any way.
So, that's what I know about casting. I hope that helps - if you have more questions, please ask.
Corey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic