Polymorphism

Download Report

Transcript Polymorphism

Polymorphism in Java
Introduction
What is Polymorphism?
Example code
Why should we use it?
How important is it?
Resources
[email protected]
Polymorphism in Java
What is Polymorphism?
Poly means many – as in polyphonic, polyester,
polygon...
Morph means shape or form – as in
anthropomorphic, the plasticine animation character
named Morph, metamorphosis...
So a polymorph has many shapes or forms.
In a Java object, a method name may exist as
several different versions.
[email protected]
Shape.java, the superclass...
// Base class with
// a single method.
public class Shape
This class sets up a template for a couple
more classes we will define next.
Note that the one method defined here
is pretty useless.
It will be overridden by the two subclasses
on the following slides.
{
public double getArea()
{ // generic Shape has no useful "area" to report
return 0.0;
}
} // end of Shape
Circle.java, a subclass of Shape
// Subclass of Shape that implements a getArea method
public class Circle extends Shape
{
double dRadius;
public Circle(double r)
This class replaces everything
that was in Shape – so why
inherit from Shape?
{ dRadius = r;
}
public double getArea()
{ double dArea = dRadius*dRadius*Math.PI;
return dArea;
}
} // end of Circle
// pi*r-squared
Square.java has two constructors
public class Square extends Shape
{ double dSide;
public Square(double s)
{ dSide = s;
}
This class replaces everything
that was in Shape – so why
inherit from Shape?
public Square()
{ dSide = 1.0; // default value for the size of the square.
}
public double getArea()
{ double dArea = dSide*dSide;// side-squared
return dArea;
}
} // end of Square
Code from the Polymorfik demo
public static void main(String[] args)
{ // We can assign a subclass to a variable with the "wrong" type only
// if that "wrong" type is a superclass type
Shape shape1 = new Shape(); // variable and instance: same type
Shape shape2 = new Circle(1.0);
// instance is a subclass
Shape shape3 = new Square(2.0); // instance is a different subclass
System.out.println("Shape One has area: " + shape1.getArea());
System.out.println("Shape Two has area: " + shape2.getArea());
System.out.println("Shape Three has area: " + shape3.getArea());
}
Polymorphism in Java
How does the code work?
When we print the area of each Shape, will we run
the getArea() method defined in Shape?
Or will we use the newer versions in Circle and
Square?
Which way makes more sense and is more useful?
Which is simplest for Java to do?
What about the multiple constructors in Square?
[email protected]
Polymorphism in Java
Why should we use it?
Overloaded methods (polymorphic methods) give us
different styles of using functionality.
Our Square example allows us to have a useful
“default” constructor, for example.
Runtime polymorphism (where subclassed objects
share a method name but have specialised
functionality) provides coders with a simpler yet
consistent logical model of slightly different
customised objects.
[email protected]
Polymorphism in Java
How important is it?
Polymorphism is essential to our use of inherited
code.
Without polymorphic capabilities, it would be very
hard to use high-level abstractions (like getArea() for
example).
Polymorphism is the basis of GUI toolkits, Java
Beans and just about any other set of reusable,
structured tools.
And best of all, it's not complicated for the coder!
[email protected]
Polymorphism in Java
Online Tutorials
The Essence of OOP Using Java, Polymorphism
Based on Overloaded Methods By Richard G.
Baldwin
http://www.developer.com/java/article.php/966001
The Essence of OOP using Java, Runtime
Polymorphism through Inheritance By Richard G.
Baldwin
http://www.developer.com/java/article.php/983081
[email protected]