- /**Demonstrates the basics of
polymorphism
- *
- * @author TK Rogers
- * @version 3-9-11
- */
- public class
PolymorphismDemo {
-
- RedSquare r1 =
new RedSquare ( ) ;
- // RedSquare r2 = new Square (
) ; gives compiling error
- Square r2 =
new RedSquare ( ) ;
- BlueSquare b =
new BlueSquare ( ) ;
- // BlueSquare b2 = new
RedSquare ( ) ; gives compiling error
- Square s = new Square ( ) ;
- public
static void main ( ) {
-
// Creating a local demo object makes it possible
-
// for a static method like main to access an
-
// instance method like outPut.
-
PolymorphismDemo demo = new PolymorphismDemo ( )
;
-
demo.outPut ( ) ;
- }
-
- public void
outPut ( ) {
-
System.out.print ( "RedSquare r1 area (ft^2)
= " + r1.areaCalculator ( ) ) ;
-
System.out.println ( "\t color = " + r1.color ) ;
-
-
System.out.print ( "Square
r2 area (ft^2) = " + r2.areaCalculator ( ) ) ;
-
System.out.println ( "\t color = " + r2.color ) ;
-
-
System.out.print ( "BlueSquare b area (in^2)
= " + b.areaCalculator ( ) ) ;
-
System.out.println ( "\t color = " + b.color ) ;
-
System.out.print ( "Square
s area (in^2) = " + s.areaCalculator ( ) ) ;
-
System.out.println ( "\t color = " + s.color ) ;
- }
- }