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

Polymorphism example

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!>
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 33
VI Editor Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation Fred Thanks a lot
 
That is a really big piece of pie for such a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic