Abstract Class: Shape Square Circle Triangle
Abstract Class: Shape Square Circle Triangle
Chapter 7 - Object-Oriented
Programming 3
Abstract Class
• There are some situations in which it is useful to
define base classes that are never instantiated.
• Such classes are called abstract classes.
• For instance, we could have an abstract superclass
Shape and derive concrete classes (non-abstract
classes) such as Square, Circle, Triangle etc.
• We do not know how to implement the area()
method of the Shape class - leave it empty and let
subclasses to define it (abstract method).
2
Abstract Methods
//other stuff
}
//other stuff
}
class TestShape {
public static void main(String[] args) {
Shape s = new Shape();
}
}
>javac TestShape.java
TestShape.java:10: Shape is abstract; cannot be instantiated
Shape s = new Shape();
^
1 error
5
//other stuff
}
class TestShape {
public static void main(String[] args) {
Shape s = new Shape();
}
}
>javac TestShape2.java
TestShape2.java:1: Shape should be declared abstract; it does not
define area() in Shape
class Shape {
^
1 error
6
Example
• The class inherits from Shape MUST implement (provide a
method body) area().
abstract class Shape {
String name;
abstract public double area();
public String name() {
return name;
} Output
//other stuff The area of square s is 156.25
}
class Square extends Shape {
double length;
public Square(double length) {
this.length = length;
}
public double area() {
return length*length;
}
}
class TestShape3 {
public static void main(String[] args) {
Square s = new Square(12.5);
System.out.println("The area of square s is "
+ s.area());
}
}
7
Introduction to Polymorphism
• Polymorphism
– Polymorphism means "having many forms."
– In Java, it means that a single variable might be used
with several different types of related objects at
different times in a program.
– Recall that a superclass reference can refer to subclass
objects.
– When the variable is used with "dot notation"
variable.method() to invoke a method, exactly which
method is run depends on the object that the variable
currently refers to.
8
Example
• Consider the previous example, suppose we want to add a new
class Circle that inherits from Shape.
// Shape and Square are the same as before
class Circle extends Shape {
double radius; Output
public Circle(double radius) {
this.radius = radius; The area of s is 156.25
} The area of s is 78.539816339744
public double area() {
return Math.PI*radius*radius;
}
} s.area()
calls the
class TestShape4 { correct
public static void main(String[] args) { method
Shape s = new Square(12.5);
Shape s can
System.out.println("The area of s is " + s.area());
refer to a
Square or a s = new Circle(5);
Circle. System.out.println("The area of s is " + s.area());
}
}
9
Polymorphism Examples
• Video game
– Superclass GamePiece
• Contains method drawYourself
– Subclasses Martian, Venutian, LaserBeam, etc.
• Override method drawYourself
– Martian draws itself with antennae
– LaserBeam draws itself as bright red beam
– This is polymorphism
– Easily extensible
• Suppose we add class Mercurian
– Class Mercurian inherits superclass GamePiece
– Overrides method drawYourself
11
Case Study: A Payroll System Using
Polymorphism
• Example
– Abstract superclass Employee
• Method earnings applies to all employees
• Person’s earnings dependent on type of Employee
– Concrete Employee subclasses declared final
• Boss
• CommissionWorker
• HourlyWorker
12
// Constructor
public Employee( String first, String last ) {
firstName = new String ( first );
lastName = new String( last );
}
Output
Boss: John Smith earned $800.0
Commission worker: Sue Jones earned $850.0
Hourly worker: Karen Price earned $550.0
17
Example (Continued)
• The above test program can be implemented by using an
Employee array.
class TestEmployee2 {
public static void main(String[] args) {
Output
>java TestEmployee3
Boss: John Smith earned $800.0
Commission worker: Sue Jones earned $850.0
Hourly worker: Karen Price earned $550.0
Piece worker: Maggie Jackson earned $1100.0
20
Interface
• Java does not support multiple inheritance: a class
cannot have more than one superclass.
Interface Example
Public class TV {
}
//......
}
22
Interface Example