• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

lets see if someone can solve this mistry

 
Udayan Patel
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class AA extends AbstractClass{
}

class AAA extends AA {
}

class MyClass {
AA aa = somemethod();
sominstance.setAAA((AAA)aa);
}

I am getting a ClassCastException. What is wrong here?
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you could cast like that, then you could potentially call a method that's only in the child class but using the parent class handle. In other words, you would be calling a method that does not exist.

 
Raj Nagappan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Udayan Patel:
class AA extends AbstractClass{}

class AAA extends AA {}

class MyClass {
AA aa = somemethod();
sominstance.setAAA((AAA)aa);
}


somemethod() is returning a concrete instance of type AA, not AAA. You need to ensure that somemethod() returns a new AAA object instead

try this:
AA aa = somemethod();
if (aa instanceof AAA)
sominstance.setAAA((AAA)aa);

Raj.
 
Udayan Patel
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I expected a pretty good discussion here. Oh! well......
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And this has absolutely nothing to do with the SCJD exam. So why post here?

You could have posted it in Java In General.

:shrug

Mark
 
Udayan Patel
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Spritzler:
And this has absolutely nothing to do with the SCJD exam. So why post here?

You could have posted it in Java In General.

:shrug

Mark



Well I am sure you all guys do an assignment given by SCJD certification and thats the place where you can use design patterns. So I thought would be nice if someone should solve this using design patterns. Well never mind. Sorry about that......
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Udayan,
Your question is well answered by Raj. You may appreciate him for that.

Your response to it seems like your expectations didn't meet.

I think your response is what made Mark shrug with.

Again I assume that any issue faced during SCJD assignment can be discussed here. We all learn from eachother. A shrug did give you a signal about the attitude expressed. We all are constantly changing.

Your response to Mark shows that you learned something. I just wanted to make sure you get the message Mark is passing.
Jiju
 
James Clinton
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj's answer is correct, but the IMPORTANT thing to remember here is

AAA 'is a' AA (valid inheritance)

Where as

AA 'is a' AAA (no such relationship, invalid code)

HTH
 
Udayan Patel
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jiju ka:
Udayan,
Your question is well answered by Raj. You may appreciate him for that.

Your response to it seems like your expectations didn't meet.

I think your response is what made Mark shrug with.

Again I assume that any issue faced during SCJD assignment can be discussed here. We all learn from eachother. A shrug did give you a signal about the attitude expressed. We all are constantly changing.

Your response to Mark shows that you learned something. I just wanted to make sure you get the message Mark is passing.
Jiju


Jiju,
I see nothing wrong with your concerns about my attitude. But try executing code given by Raj. I come across something that would be helpful to someone as down casting is not allowed in java but there are ways to do it.
 
James Clinton
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
down casting is not allowed in java but there are ways to do it.


That maybe the case, but if you find yourself having to do this then i suspect there is something seriously wrong in your design.
[ October 21, 2004: Message edited by: James Clinton ]
 
Udayan Patel
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Clinton:


That maybe the case, but if you find yourself having to do this then i suspect there is something seriously wrong in your design.

[ October 21, 2004: Message edited by: James Clinton ]


May be, lets consider a scenario, if you have an abstract algorythm that perticularly returns a certain type of object. now this object has many types underneth, how about an Animal class, subclassed by Mammal and Amphibian. now Mammal, some Mammal lay eggs and most don't(Lets say two classes EggLayingMammal and NonEggLayingMammal both extends Mammal). what if some operation gives me Mammal but I need to cast it to more specific sub class of Mammal class? I do think that I am making my point here.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what if some operation gives me Mammal but I need to cast it to more specific sub class of Mammal class? I do think that I am making my point here.





We do that quite a bit at my work. Now we have a better design in that we
don't have to use instanceof, we really avoid instanceof. But this isn't really that difficult, and in Java it happnes quite a bit in the core libraries.

Mark
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



But try executing code given by Raj. I come across something that would be helpful to someone as down casting is not allowed in java but there are ways to do it.


We can only direct you find the cause of your error. We are not having the entire code you have. So we cannot create the scenario you are facing

In your case you are getting a ClasscastException. From the email chain above it is not clear what exactly is the object passed back from somemethod at line 8.

Can you put the following code between line 8 and 9


After executing this you will find the relation between objectReference AA and actual object. Let us know what is printed out. If the above code prints

AAA

then ClassCastException may be happening at step 9 inside setAAA method. If so please paste code for setAAA method.

Please use UBB code specified under http://www.javaranch.com
 
James Clinton
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

May be, lets consider a scenario, if you have an abstract algorythm that perticularly returns a certain type of object. now this object has many types underneth, how about an Animal class, subclassed by Mammal and Amphibian. now Mammal, some Mammal lay eggs and most don't(Lets say two classes EggLayingMammal and NonEggLayingMammal both extends Mammal). what if some operation gives me Mammal but I need to cast it to more specific sub class of Mammal class? I do think that I am making my point here.








This is along the lines of how I would do it. Notice not abstract method needed in Parent because getSomething() is specific to Child.

[ October 22, 2004: Message edited by: James Clinton ]

[ October 22, 2004: Message edited by: James Clinton ]

[ October 22, 2004: Message edited by: James Clinton ]
[ October 22, 2004: Message edited by: James Clinton ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Spritzler:



We do that quite a bit at my work. Now we have a better design in that we
don't have to use instanceof, we really avoid instanceof. But this isn't really that difficult, and in Java it happnes quite a bit in the core libraries.


This code is bad *if* you regularly have to adjust it to new subtypes of Mammal - then it's violating the Open Closed Principle and the Single Choice Principle. In that case you should think about making better use of polymorphism - if only by using the Visitor pattern.

And as an aside, not all Java developers are gurus - there is quite some amount of rather questionable design in the JDK, in my not so humble opinion...
 
Udayan Patel
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:



We do that quite a bit at my work. Now we have a better design in that we
don't have to use instanceof, we really avoid instanceof. But this isn't really that difficult, and in Java it happnes quite a bit in the core libraries.
<hr></blockquote>

This code is bad *if* you regularly have to adjust it to new subtypes of Mammal - then it's violating the Open Closed Principle and the Single Choice Principle. In that case you should think about making better use of polymorphism - if only by using the Visitor pattern.

And as an aside, not all Java developers are gurus - there is quite some amount of rather questionable design in the JDK, in my not so humble opinion...[/QB]


All I wanted to see is this. This made me wonder how many people use design patterns while they are submitting their programming assignment? not that without design patterns you can't build a good software but it really lays a strong foundation for three software development principles scalability, extansibility and usability. therefore I am really interested in sun's code evaluation guidelines for programming assignment.
 
Raj Nagappan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

class MyClass {
AA aa = somemethod();
sominstance.setAAA((AAA)aa);
}


Further to the discussion in the same vein of polymorphism, etc, why would you want to getAA() and setAAA() in the same context? Surely it would be more sensible to setAA(source.getAA()) or setAAA(source.getAAA())?

IMHO you will experience fewer ClassCastExceptions if you practice Interface driven programming. eg. always program your client/server against the DB interface given to you by Sun, rather than the concrete implementation that you must develop in the assignment.

Raj.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:



We do that quite a bit at my work. Now we have a better design in that we
don't have to use instanceof, we really avoid instanceof. But this isn't really that difficult, and in Java it happnes quite a bit in the core libraries.
<hr></blockquote>

This code is bad *if* you regularly have to adjust it to new subtypes of Mammal - then it's violating the Open Closed Principle and the Single Choice Principle. In that case you should think about making better use of polymorphism - if only by using the Visitor pattern.

And as an aside, not all Java developers are gurus - there is quite some amount of rather questionable design in the JDK, in my not so humble opinion...[/QB]


That is why I posted this comment.





We do that quite a bit at my work. Now we have a better design in that we
don't have to use instanceof, we really avoid instanceof.


Mark
 
Udayan Patel
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raj Nagappan:


Further to the discussion in the same vein of polymorphism, etc, why would you want to getAA() and setAAA() in the same context? Surely it would be more sensible to setAA(source.getAA()) or setAAA(source.getAAA())?

IMHO you will experience fewer ClassCastExceptions if you practice Interface driven programming. eg. always program your client/server against the DB interface given to you by Sun, rather than the concrete implementation that you must develop in the assignment.

Raj.


Sure Interface driven programming is a good prectice. But, As I said, I might not have a control over what I get back. Like I will be working with legacy code may be lets Say RMI based system is returning me this object. "WHERE MY INTERFACE AND THE INTERFACE OF OBJECT BEING RETURNED DOESN'T MATCH" (quotes and caps here is key). And I am sure by being more specific so two more if conditions to my code so is less cyclomatic complexity.
BTW, If you look at code carefully, get and set is not in the same context. Some operation is returning me instance of base class. where my process is preparing a collection of child classes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic