my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Polymorphism example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphism example" Watch "Polymorphism example" New topic
Author

Polymorphism example

Jen Francis
Greenhorn

Joined: Jan 12, 2011
Posts: 2
I am trying to find an example that includes both overridden methods and subclass-specific methods and I can't seem to find any. It seems like in the past I've used a base class to take advantage of polymorphism, and have then wanted to use the derived instances. Is this something that never happens or should never happen? Here is an example:

List<Shape> shapes = new ArrayList<Shape>();
list.add(new Circle());
list.add(new Rectangle());
list.add(new Triangle());

for (Shape shape : shapes)
{
shape.printArea();
}

Ok, now I want to call specific (not overridden) methods in each of these derived classes (such as getRadius() in the Circle class). But in order to get the derived instances from the list, I would have to downcast and call instanceof (ugly, right?). So instead, I could do this:

Circle circle = new Circle();
Rectangle rectangle = new Rectangle();
Triangle triangle = new Triangle();

Now I can call the subclass-specific methods. But in this case, it would be fewer lines of code to just call printArea() for each instance instead of using the base class Shape.

So what is the best way to write this? Or should you never expect to have access to a subclass instance once you declare it using its superclass?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Hello Jen! Welcome to ranch!

Look at this code please:



Override and polymorphism are used in this code.Please ask any question if you have any doubt.Best wish!>
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

Jen, Welcome to JavaRanch!!

You can always have Circle specific data- radius or Rectangle specific data- length, breadth in the respective classes and use them in the overridden printArea() method. But accessing the methods introduced in the Circle/Rectangle class with a reference of type Super class(though the instance its pointing to is of Subclass type) is not possible.

You could have some thing like this:


And similarly for other Shape classes.

And please UseCodeTags while posting the source code.

Mohamed Sanaulla | My Blog
Stefaan Dutry
Ranch Hand

Joined: Sep 17, 2010
Posts: 32

Jen Francis wrote:I am trying to find an example that includes both overridden methods and subclass-specific methods and I can't seem to find any. It seems like in the past I've used a base class to take advantage of polymorphism, and have then wanted to use the derived instances. Is this something that never happens or should never happen?

The reason for using polymorphysm is to be able to call the same method with different implementations, without at that moment having to know what the exact implementation is. In this case, printing an area.
When you want to use subclass specific methods you need to ask yourself if you realy want to call the method on every item, and if it makes sense to do so.
If it does make sense to call the method on the superclass or interface, you should add it there and add the wanted implementations to the subclasses.
If it does not, then using a polymorphic aproach is probably not the best way.

You could do the following at the declaration:
Jen Francis
Greenhorn

Joined: Jan 12, 2011
Posts: 2
Stefaan Dutry wrote:

You could do the following at the declaration:


Or I could write:


In this case I am not taking advantage of polymorphism at all, although the printArea() method is part of the Shape class. This reduces the amount of code I would need to write by a few lines. I guess in this case it's a matter of whether you wanted to print all areas as a group (as in your example) or not. I guess I don't see what is so useful about polymorphism if you need to access subclass-specific methods as well as superclass methods (which is often going to be the case).
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11160
    
  16

I would say that polymorphism is a tool, like everything else. It is great for what it is designed for, but terrible for what it's not. Just like a drill is a horrible tool for hammering a nail.

Your examples illustrate where it can(and should) be used. If you need to do the same thing to every object (like getArea()), it's awesome. Let's say you are part of a huge team of developers. Some other team is going to populate your collection, and you are going to process the objects. Writing your code this way would cause problems:

What do you do when the other team wants to start sending you trapezoids? or pentagons? or some other shape? You have to go in and write brand new code, which means you're going to have to test everything all over again. You have to cancel your vacation to fix the problem.

Whereas if you write code to use a Shape collection, you don't care what the specific shape is. You never have to touch your code. it just works.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

Nice explanation Fred Thanks a lot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism example
 
Similar Threads
Generics: Shape example does not work as SUN says
Can't find my error
Questions about polymorphism and interfaces
polymorphism
Storing objects polymorphically in a Figure array