• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding polymorphism

 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

So there are a few things regarding polymorphism that I struggle to understand.

For example say I have these classes and interface.









ok then I have a test class like so:



these are all valid declarations right? i.e I can replace the type with that of a super class or the interface through inheritance?

that much i understand, but here is where i get wonky



will printout what?

b is of type Canine but was initialized as a Dog? so i would think it prints out: in Dog?

likewise p.doSomething will print out: in Dog and x.doSomething will print out: in Canine?

am i right so far?

if I am then to carry on if I went



firstly is that a valid cast to use the same reference?

and secondly if I were now to go b.doSomething();

would it print out: in Canine?

Thanks for your time.
 
Ivan Jozsef Balazs
Rancher
Posts: 981
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This cast makes no sense, since b was declared as Canine in the first place.
My guess is that is syntactically allowed but discarded by the compiled.


What about trying the behavior, checking whether it conforms to your expectations and asking a question if it is not clear?


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First: Isn't class Wolf supposed to extend class Canine? (In your code above it isn't explicitly extending anything).

Casting does not change the object or variable in any way. So a line like

does not do anything. Specifically, it's not going to convert the object that b refers to to a Canine.

The only thing a cast means, is that you tell the compiler "look, I have an object of type A here, and I want you to treat it as if it is type B - don't complain if you think it isn't a B". A check will still be done, but at runtime instead of compile time. If, at runtime, the object you're casting really is not a B, you'll get a ClassCastException.
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no Wolf wasn't supposed to extend anything (well besides Object) the reason would come in a follow up question I was going to ask.

Will put it in netbeans and play around a bit more, was just seeing if someone had some sort of easy explanation to polymorphism and some general rules as no matter how many times I read up on it I get some questions wrong in the Enthuware mock exam regarding implications of polymorphism.
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O I see what you mean with the cast, let me put it this way rather:

Canine newRef = (Canine) b;

?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wesleigh Pieters wrote:Canine newRef = (Canine) b;

The only thing that line does is initialize a new variable newRef that would refer to the exact same object that b refers to.

The cast is unnecessary because b is already a Canine. (The cast does nothing, you could just leave it out).
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should try making every class implement that Pet interface. Dog doesn’t need to say so explicitly, because its superclass already implements that interface. Then you can declare every object as of type Pet.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That cast is redundant, but the Java Language Specification explains why it is specifically permitted.
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wesleigh Pieters wrote:was just seeing if someone had some sort of easy explanation to polymorphism and some general rules

1. The effects of polymorphism are not particularly simple, so don't expect them to be.
2. The rules of polymorphism in Java are extremely simple: ALL methods are polymorphic unless they're declared final or static.

The simplest explanation I can give you is that every assignment in your TestClass can be thought of as:where:
  • VisibleType is the type determined by the compiler, and determines which methods you are allowed to call.
  • ActualType is the type determined by the JVM (ie, at runtime) and determines which version of a method actually gets run.

  • Specifically, given a call:the version run will be the lowest (most specific) implementation of method() at or above ActualType in the class hierarchy.

    HIH

    Winston
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic