File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes polymorphism and casting question logic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "polymorphism and casting question logic" Watch "polymorphism and casting question logic" New topic
Author

polymorphism and casting question logic

alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
if we add


to the class bellow will it work or will get a bug?
if it will give a bug then it will be on the compiler problem or running time problem?






Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

donaldth smithts wrote:if we add
to the class bellow will it work or will get a bug?
if it will give a bug then it will be on the compiler problem or running time problem?


Try it and see.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You will have to get a pencil and paper, and write the inheritance of all those classes, and the declared types of all the variables. Then you can see whether they can be assigned to one another.
And please always tell us where the question is from, to avoid problems with copyright or similar.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Pierluigi Di Giacomo wrote:Didn't test the code yet but I think that it will generate a compiler error of "incompatible types"...


Please don't just give the OP the answer like that. The expectation here is that he will ShowSomeEffort.(⇐click) If he does, and give his thoughts on what he expects and why, or tries the code, says, "I got such and such result, but I thought it would be this other thing," then we can guide the discussion toward helping him come to an understanding on his own.

Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I know it may be too late, but as Jeff has said, you do not help people by providing a straight answer like that. Don’t be annoyed with me, but I shall have to pull rank, remind you what it says at the beginning of this forum:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
. . . and delete that last post.
Pierluigi Di Giacomo
Greenhorn

Joined: Jan 18, 2012
Posts: 13
Campbell Ritchie wrote:I know it may be too late, but as Jeff has said, you do not help people by providing a straight answer like that. Don’t be annoyed with me, but I shall have to pull rank, remind you what it says at the beginning of this forum:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
. . . and delete that last post.


Hi Jeff&Campbell,

when I read the author's post I also realized that maybe he didn't show enough effort, and seemed like he was aiming for a straight answer. For this reason I decided to write more than a straight answer and explain the behaviour of his and my code, so he could think over them and hopefully understand the background topic covered by his code, rather than assimilating "e=c -> NO", which I could have written straight away (sparing much of my time by the way), but that wouldn't have helped him at all.
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
regarding

I thought it would cheat the compiler becuase of the casting
and it would fail on the running time.


but the compiler said incompatible types.

why??

also on this line

i thought it would use the b method of B,but it used the b method of G

why??


regarding this line:

why the compiler said that we cannot cast F into G
??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Apologies and explanation accepted

donaldth smithts, did you write down the inheritance hierarchy of classes E and C? Have you worked out which type you are trying to assign to which reference? Let’s have one question at a time. Let’s work out the original question first, whether adding that assignment will cause compiler errors or an Exception at runtime. Let’s leave that cast question till later.
If you had the inheritance hierarchy written down, you would be a long way towards answering your question.
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
this is the tree,how does it helps me seeing why i dont get the proper responce/?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

donaldth smithts wrote:this is the tree,how does it helps me seeing why i dont get the proper responce/?


To draw the tree *was* the proper response -- not a requirement for us to give the proper response. You are supposed to draw the tree, and from that see why the compiler is not allowing the implicit cast.

However, if you need a hint.... Can the compiler assume that the cast is always correct (so that it can implicitly cast)?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Chetan Sarnad
Greenhorn

Joined: Nov 26, 2011
Posts: 20
donaldth smithts wrote:regarding

I thought it would cheat the compiler becuase of the casting
and it would fail on the running time.


but the compiler said incompatible types.

why??



Can you tell me if there is any kind of relationship or inheritance between Class D and Class E? Answering this question will help you analyse the reason for compilation error.




also on this line

i thought it would use the b method of B,but it used the b method of G

why??


you must first try to understand the behavior of static members of a class, the static methods cannot be overridden.


alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
regarding

D and E classes both inherit class C

still cant see why casting wont cheat the compiler
?




regarding:
over riding is when we use some other function instead of the function in class B.
in this line we dont override thestatic fubction in class B we try to use it.
where am i wrong?

and i know that G inherits F which inherits B
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
alex lotel wrote: regarding
D and E classes both inherit class C
still cant see why casting wont cheat the compiler

Give your classes meaningful names and it might make more sense.

Have you ever known a cat that is also a dog ? So does it make sense to cast a dog to a cat ?

If Java allowed you to cast an instance of one object to a different type just because they had a super class in common then you could cast anything to anything because all classes inherit from the Object class.


Joanne
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
ok but previosly
when we did
e=(E)c
we cheated the compiler it allowed us to do it,but we failed on the running time.
so i understand that the ruke is that we cast cast only from classes that there is a direct inheritance relationship.

regarding:

over riding is when we use some other function instead of the function in class B.
in this line we dont override thestatic fubction in class B we try to use it.
where am i wrong?

and i know that G inherits F which inherits B
?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
alex lotel wrote:this is the tree, . . .
That looks more like BlueJ’s class diagram, with the classes set out randomly. Can’t you move the classes round so they are in order? And so you can actually read it?
You get something like thise = c;
Now work out the chances that your C will be an E. Does the compiler believe it can fit c into e?
e = (E)c;
The compiler believes you implicitly if you tell it that is an E. Does the runtime know better, or does it go along with that cast?
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
I understood that we cant a non specific variable to a specific like e=c
but we can cheat thec complier because one extends the other directly?

but what if E extends G
and G extends C

i.e we have one class in the middle
will it still be cheated

my question is in general in what cases we can cheat the compiler

??

and i got problems with:


i thought it would use the b method of B,but it used the b method of G

why??


regarding this line:

why the compiler said that we cannot cast F into G
??
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

alex lotel wrote:
my question is in general in what cases we can cheat the compiler


Never. We must always follow the language rules, or it won't compile. Casting is not cheating the compiler. It is simply using a language rule.

The rules for casting are simple.

At compile time:
We are allowed to cast (X)Y if and only if there is a straight line from X to Y in the type hierarchy. We cannot "jump branches." X must be either a supertype of Y or a subtype of Y. Note that this typing can include interfaces as well as classes, and X and Y can both be classes, both be interfaces, or there can be one of each.

Using Cambell's diagram from before:


A <--> C <--> D : Any combination of any of those, in any direction is allowed.
A <--> C <--> E : Any combination of any of those, in any direction is allowed.
A <--> B <--> F <--> G : Any combination of any of those, in any direction is allowed.

Nothing else is allowed.
(C, D, E) <--> (B, F, G) is not allowed.
D <--> E is not allowed.

Do you understand now?

And at runtime, if we do (X)Y, then the object referred to by Y must be of type X or a subtype of X, or we get a ClassCastException.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

alex lotel wrote:
regarding this line:

why the compiler said that we cannot cast F into G
??


I do not believe it says that, based on the code in your original post. If you think it does, please provide an SSCCE that anybody can just copy, paste, compile, and see the same results.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

alex lotel wrote:I understood that we cant a non specific variable to a specific like e=c
but we can cheat thec complier because one extends the other directly?
but what if E extends G
and G extends C
i.e we have one class in the middle
will it still be cheated

You know what? I have no idea; and what's more I don't really care because you've clearly not read Joanne's post.

Classes are not just some abstract A, B, C and D that you can fool about with any way you like; they represent things. Things with names.

Furthermore, this is Java, not C++, so you have a lot of restrictions on class hierarchies (and not before time).

Come up with a scenario in which some realistically named class needs a particular inheritance property and we (or at least I) might be able to help; but speculating about some specious hierarchy just seems like a waste of time to me.

my question is in general in what cases we can cheat the compiler.

My question: Why would you want to? Any result of an exercise like that is likely to be brittle at best; and might well end up being invalidated by a future release that "plugs the hole".

My two-penn'orth.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: polymorphism and casting question logic