Lecture4 Overloading
Lecture4 Overloading
CPE 372
Lecture 4
2
Create Subclasses to Specialize
3
Class Hierarchy
Does an ExchangeStudent have an advisor?
Can I call hasPassedToefl() method on a PhDStudent?
Is GradStudent a subclass of PhDStudent?
What if I want to create a class for high school students?
6
AbstractShape Class
8
OO Design Rule of Thumb: 3
9
Let’s add new behavior
draw
Draw
10
draw fill color
To Use This Method...
11
1 Overloading
This is an example of overloading a method.
“Overloading” means creating multiple versions of a
method that have the same function name, but different
arguments and/or a different return value.
(We can also say the different implementations have different
signatures.)
ring 12
overload
Another Example owr I
Original constructor for Triangle: Orrison mis
b public Triangle(int x1, int y1, int x2, int y2, int x3, int y3);
y
Or you might implement it like this:
his
public Triangle(Point p1, Point p2, Point p3);
gaffe
It’s possible to do both!
triangle
You could also have a default constructor:
public Triangle()
r{ to help
Triangle(100,0,100,100,150,200);
}
13
What are the advantages of overloading?
14
I new discussion
Adding a New Subclass
(xc, yc)
radius
15
What went wrong???
16
Draw Method in AbstractShape
Circle doesn't have
any point
rehnauhidnmonmnqamognidwoonnwioiiduno.eu
owns
in'rudowwonrurtuny 17
The Solution
Implement specialized version of draw() in the Circle class
public void draw(Graphics2D graphics)
{
graphics.setPaint(drawColor);
/* drawOval takes center plus width and height */
graphics.drawOval(anchor.x,anchor.y,2*radius,2*radius);
/* label it near the anchor point */
onion int labelx = anchor.x + 5;
int labely = anchor.y - 5;
oigo graphics.drawString(new String(" " + shapeId),labelx,labely);
no'rniiu's
}
18
Smart find what to call
Java will call the right method!
The program will start at the level of the subclass and search
upwards in the class hierarchy until it finds a matching method
(matching name and signature)
useclassA cObj.foo(200,200);
UseclassC cObj.foo(new Point(230,80));
useclassBbObj.foo(200,200);
useclass A bObj.foo(new Point(230,80));
19
Overriding allows you to escape from strict
inheritance
Get the benefits of subclassing
Provides customization if necessary
iimwni Venu
Overriding can either replace or expand behavior
Circle draw() replaces AbstractShape.draw()
3DSquare draw() might expand on it
20
voodoo
98class voomfiOS 21
A fromMhs
Polymorphism many shape
many shape
Consider the method drawAll() in AbstractShape
/**
* Static method to draw all the shapes
* that have been created so far.
* @param graphics Graphics context for drawing.
*/
public static void drawAll(Graphics2D graphics)
{
for (int i=0; i < allFigures.size(); i++)
{
AbstractShape shape = allFigures.get(i);
shape.draw(graphics);
}
}
23
Another Example
/**
* Static method to print the area of all the shape
* that have been created so far.
* @param graphics Graphics context for drawing.
*/
public static void showAreas()
{
for (int i=0; i < allFigures.size(); i++)
{
AbstractShape shape = allFigures.get(i);
double area = shape.calcArea();
System.out.println(“The area of shape “ + shapeId +
“ is “ + area);
}
}
24
Another Example: PolyDemo
Contents of objectlist
IntegerC227
class
26
All talk about method
Summary of Terms
Overloading
To create multiple methods in one class which have the
same method name but different signatures
(arguments)
27
Summary of Terms (2)
Overriding
To create a method in a subclass that has the same
name and signature as a method in its superclass, but
a different implementation which is specific to that
subclass Inooooow mon ironnon's now
28
Summary of Terms (3)
Polymorphism
A situation in which multiple
subclasses of a superclass have
methods with the same names and
signatures, but different
implementations. noworms
When the polymorphic method is
called on an object identified as the
superclass, the correct subclass
method is automatically invoked.
29