Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Understanding polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Understanding polymorphism" Watch "Understanding polymorphism" New topic
Author

Understanding polymorphism

Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
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

Joined: May 22, 2012
Posts: 866
    
    5


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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
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

Joined: Sep 04, 2012
Posts: 81
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Oct 13, 2005
Posts: 37901
    
  22
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

Joined: Oct 13, 2005
Posts: 37901
    
  22
That cast is redundant, but the Java Language Specification explains why it is specifically permitted.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

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


    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Understanding polymorphism
     
    Similar Threads
    Anyhting like super.super?
    Polymorphism
    polymorphism by using interface and inheritance
    Polymorphism