Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

  • General CS Concepts: Polymorphism

    Polymorphism

    Polymorphism means to view an object as several different things. Here's a simple example: A Fararri Enzo is a Fararri, which is a car, which is an automobile, which is a vehicle, which is an object. Depending on what you need to do with the object, you can view it in many different ways. In the above example, you don't really need to know all to much about the Fararri Enzo itself to drive it (err, I think. I've never actually driven one).

    In programming, polymorphism arises from inheritance/interface implementation. In the above example, the Fararri Enzo is some descendant of all the other objects. However, it's not possible to go the other way. A vehicle is not necessarily an automobile, it could be an airplane. However, if you really do have an automobile, you can by definition view the vehicle as an automobile.

    So why would we want this in programming?

    The key idea here is abstraction. When you're operating a Fararri Enzo, it's slightly important to know some of the features specific to that object, but in general it's just like driving any other car, so why not view it as just a car? It would make coding a lot easier. Also, the idea of polymorphism helps with code re-use. Code re-use was the original reason Java was developed. Instead of creating code for a robot to drive a Fararri Enzo, and then creating more code for the robot to drive a Porshe 911 etc., you can create code code for the robot to drive a car and be done.

    There are some important notices that come about when using polymorphism:

    Say you had 3 classes: Shape, rectangle, and circle. Rectangle and circle both are children of shape, shape has an area method, and rectangle and circle both implement it.
    public Shape
    {
         public double area()
         {
              return 0;
         }
    }
    public Rectangle extends Shape
    {
         double width, int height;
         public Rectangle(double width, double height)
         {
              this.width = width;
              this.height = height;
         }
         public double area()
         {
              return width * height;
         }
    }
    public class Circle extends Shape
    {
         double radius;
         public Circle(double radius)
         {
              this.radius = radius;
         }
         public double area()
         {
              return radius*radius*Math.PI;
         }
    }

    What should happen when you try to do this?

    public static void main(String[] args)
    {
         Shape shape = new Rectangle(5.0,3.5);
         System.out.println(shape.area());
         shape = new Circle(3.4);
         System.out.println(shape.area());
    }

    Hopefully you can see that it will first print out the area of that rectangle, then print out the area of the circle. It's using the child's methods instead of the class's methods that we are viewing it as! Doesn't this seem silly? We're viewing it as a shape, but it's using the Rectangle's/Circle's method!

    On closer examination, it becomes clear why this is the case: Even though you are viewing the Rectangle/Circle as a Shape, in reality they are still Rectangles/Circles, and thus their area must be computed in a specific way for each object. However, Shape tells us that for any given Shape/Shape sub-class, we can find the area using the .area() method.

    This is the case when driving cars: You turn the wheel to steer, press the pedal to accelerate, and press the brake to stop. However, how much you turn the wheel and how hard you press on the pedals depends on the type of car you're in, and these details are within that specific model's declaration.

    Happy coding
    This article was originally published in forum thread: General CS concepts started by helloworld922 View original post