Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Classes. Recall We have the ability to create hierarchies of classes. Good design practice suggests that we move common methods as high as possible.

Similar presentations


Presentation on theme: "Abstract Classes. Recall We have the ability to create hierarchies of classes. Good design practice suggests that we move common methods as high as possible."— Presentation transcript:

1 Abstract Classes

2 Recall We have the ability to create hierarchies of classes. Good design practice suggests that we move common methods as high as possible in the “tree” Why? Minimize code duplication Minimize maintenance efforts Good abstraction Example?

3 But What if we know that every subclass will have a certain method but the methods will all be different??? What then? Example: We are designing a calculator that will have as a feature the ability to make calculations involving geometric shapes We want it to handle rectangle, squares, triangles, equilateral triangles, ellipses and circles Each shape should know how to calculate its area and perimeter

4 Shapes Shape EllipseRectangleTriangle getArea getPerim major minor Circle radius altitude base Equilateral Triangle side length width Square side

5 Need What we would like is the ability to say in the Shape class that all Shapes will have getArea and getPerimeter methods. We obviously don’t know what they will be when all we know is that something is a shape but... The basic idea is for the compiler to provide as a service the ability to “force” all subclasses of shape to have a getArea and getPerimeter method The underlying philosophy is simple: Catch errors when compiling the program not when running the program

6 Abstract Methods We have the ability to define a method with no method body. We are required to label this method as abstract Once a class contains an abstract method it too must be labelled abstract Every subclass will too be abstract until a subclass is written in which every abstract method has been defined Abstract classes cannot be used to make Objects!!! But, they can be used to make references!?!?!?

7 Shape // Comments omitted... abstract class Shape { private String name; public Shape(String name) { setname(name); } public void setName(String name) { this.name = name; } public String getName() { return name; } public abstract double getArea(); public abstract double getPerim(); }

8 Rectangle class Rectangle extends Shape { private double ln; private double wd; public Rectangle(String name, double ln, double wd){ super(name); this.ln = ln; this.wd = wd; } public double getArea() { return ln * wd; } public double getPerim() { return 2.0 * (ln + wd); }

9 In case you’re wondering... Shape ThingsWithStraightSides TriangleRectangle If Shape is abstract and ThingsWithStraightSides doesn’t define getArea then it must also be abstract.

10


Download ppt "Abstract Classes. Recall We have the ability to create hierarchies of classes. Good design practice suggests that we move common methods as high as possible."

Similar presentations


Ads by Google