• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

polymorphism and casting question logic

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?






 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79828
388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79828
388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79828
388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the tree,how does it helps me seeing why i dont get the proper responce/?

 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3742
16
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79828
388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
expectation is the root of all heartache - shakespeare. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic