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

Questions on Polymorphism

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference among A a = new B(), B b = new B() and B c = new A()?
btw B is the subclass of A.
Any clue site will appreciate.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we have this code:


What do you get when you try to compile that code? Do you know why?

Once you can answer that, it should help you eliminate B c = new A() from consideration.

As for the other two:
B b = new B()
A a = new B()
If B IS-An A, then we should be able to use B wherever we us A. So unless there is a specific reason to keep referring to b as a B, why not just refer to it as an A? We don't lose anything.

We do gain things though. Let's actually use the objects we created. If I change my Main class to do some work, it might look like this:


Here, the method work() requires an object of type B. All it is doing is calling b.doSomething(). But I could have written the code like this:


What happens here? How are the results different?

Once you try it you should see the results are exactly the same, so you should see we haven't lost anything by referring to B as if it were an A. But what can we gain by it?

Well, perhaps later I want to add another class that extends A, we call this one D:


Back to my main class, if I wanted to use D to do the work() method, if I tried to use the first method I wouldn't be able to because D is not a B.


But if I tried to use the second approach, where I pass an A as the parameter into the work() method, then I can use the same work() method for Bs and Ds, and any other class which extends A:


Polymorphism will let me use the same code to handle different types of objects, so I don't need separate work(A), work(B), work(D) methods. I get to reuse the same method and get more bang for my line of code. It makes it easier to debug, too, because I find I have an error in work() I can fix it once and it will be fixed for all my objects that extend A, I don't have to go back and fix it for each type.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"xx yy," please check your private messages by clicking on My Private Messages. Thanks!
 
Peter Summer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for detailed explanation, steve. it helps a lot.
reply
    Bookmark Topic Watch Topic
  • New Topic