Beruflich Dokumente
Kultur Dokumente
Contents
1. Forms of polymorphism
2. Polymorphic variables – late (run-time binding)
3. Abstract classes – example, Shape
4. Inheritance and polymorphism
Polymorphism
Forms of poymorphism
operator overloading
parametric overloading
polymorphic variables
The Object-oriented Programming Paradigm
Polymorphic Variables
Shape
Shape is an abstract class
area()
with no implementation of
perimeter()
area() and perimeter()
Rectangle
Circle
area()
area()
perimeter()
perimeter()
Circle and Rectangle are concrete classes with their own separate
implementations of the methods area() and Perimeter()
The Object-oriented Programming Paradigm
Abstract Classes
There can berealizations
Concrete NO instances of an
of Shape
public abstract class Shape { Abstract Shape class
abstract class (no Shape objects)
protected String shapeName;
Class MUST be qualified as
public Shape(String name) {shapeName = name;} abstract if one or more
public abstract double area ( ); methods are abstract
public abstract double perimeter ( );
public String toString( ) {return shapeName;};
} public class Circle extends Shape {
public class Rectangle extends Shape{ private double radius;
protected double length, width; public Circle (double rad) {
public Rectangle(double len, double wid) { super (“Circle”);
super(“Rectangle”); radius = rad;
length = len; width = wid; }
} public double area( ) {
public double area ( ) {return length * width;} return Math.PI * radius * radius; }
public double perimeter ( ) { public double perimeter( ) {
return 2.0 * (length + width); } return 2.0 * Math.PI * radius; }
} }
The Object-oriented Programming Paradigm
Additional inheritance Square extends Rectangle
public class Square extends Rectangle {
Shape public Square (double side) {
super (“Square”);
Attributes inherited
from Rectangle length = width = side;
}
}
BUT! There is a problem!
Circle Rectangle super(“Square”); refers to
the base class of Square –
which is Rectangle.
We need to add a constructor:
Rectangle(String name) {
Add a class Square Square super(name);
to the collection of }
shapes. to class Rectangle -- to “pass the
string Square” up to the base class.
The Object-oriented Programming Paradigm
A vector of Shapes
Animal
private String name;
Attributes
private String says;
public Animal(String str, String s2){ Constructor
name = new String(str);
says = new String(s2);
}
Derived classes
extend the base class
Dog Bird
public Dog(String) public Bird(String)
public void move( ) public void move( )
The Object-oriented Programming Paradigm
Inheritance
Consider the following application (class AnimalHouse)
public class AnimalHouse { public static void main(String [ ] args) {
private Dog Lassie; AnimalHouse zoo = new AnimalHouse( );
private Bird TweetyBird; zoo.animalAct( );
private Animal Felix; }
public AnimalHouse ( ) { }//end class AnimalHouse
Lassie = new Dog(“ -- bow-wow”);
TweetyBird = new Bird(“ -- tweet-tweet”);
Felix = new Animal(“Cat”, “ – meoow”); The class contains
Output
Theapplication
constructor three
allocates
The class
} attributes
memory for (“data”
the members)
three private
(AnimalHouse) contains a main
that are objects
Dog -- bow-wow
objects of the
and initializes base
public void animalAct( ) { function that creates antheir
Lassie.speak( ); class Animal, or one of the
attributes.
instance of the class and tells
Bird --derived
tweet-tweet
TweetyBird.speak( ); classes from it.
the AnimalHouse object zoo to
Felix.speak( ); Cat -- its
execute meoow
animalAct( )
Lassie.move( ); operation.
-- I run
TweetyBird.move( );
} -- I fly
Felix cannot receive a message move( )
The Object-oriented Programming Paradigm
Composition
The method tick( ) is overriden in the derived class. After every tick
of the clock, a test is done to see whether it is time for the alarm to go
off. The methods viewHr( ) and viewMin( ) are inherited from the
base class and are available to any client of an AlarmClock object
(including the methods of this class).
The Object-oriented Programming Paradigm
Polymorphism and Genericity
Genericity
The only container object we have studied so far is the array. Arrays of
any type can be declared, and the array operations such as
•Assignment ex. A[0] = assigned value of the declared type.
•Retrieval ex. Itemtype myVar = A[3]; //retrieve object at index 3
•Length A.length
are syntactically independent of the type being stored in the array.
functions with different parameter lists may have the same identifier
Ex: public static int sqr(int x) {…}
public static double sqr(double x) {..}
The compiler determines which function to use by the type of the argument in
the function call.
An operation in a base class may be overridden in a derived class.
Ex: function tick( ) is redefined in AlarmClock
2. Overloading of operators
The operator + can be used to add pairs of any of the primitive types
and to concatenate Strings. However, Java does not permit the
programmer to define additional operations for any operator.
The Object-oriented Programming Paradigm
Polymorphism
3. Polymorphic variables
Polymorphism critter.speak(
critter
critter
tweety
Animal
fido ===tweety;
=
new
fido; ); Bird(“
critter;
newDog(“ ruff-ruff”);
tweet-tweet”);
critter.speak( ); tweet-tweet
Dog Bird
ruff-ruff tweet-tweet