It's not a secret anymore!
The moose likes Beginning Java and the fly likes Questions on Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Questions on Polymorphism" Watch "Questions on Polymorphism" New topic

Questions on Polymorphism

Peter Summer

Joined: Jul 15, 2008
Posts: 2
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.
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

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.

marc weber

Joined: Aug 31, 2004
Posts: 11343

"xx yy," please check your private messages by clicking on My Private Messages. Thanks!

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Peter Summer

Joined: Jul 15, 2008
Posts: 2
thanks for detailed explanation, steve. it helps a lot.
I agree. Here's the link:
subject: Questions on Polymorphism
It's not a secret anymore!