• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Understanding polymorphism

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1043
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Marshal
Posts: 76881
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76881
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That cast is redundant, but the Java Language Specification explains why it is specifically permitted.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic