Abstract Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Abstract Classes

Description:

What we would like is the ability to say in the Shape class that all Shapes will ... We have the ability to define a method with no method body. ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 11
Provided by: BillL161
Category:
Tags: abstract | classes

less

Transcript and Presenter's Notes

Title: Abstract Classes


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
getArea getPerim
Ellipse
Rectangle
Triangle
major minor
altitude base
length width
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 dont 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 youre wondering...
If Shape is abstract and ThingsWithStraightSides
doesnt define getArea then it must also be
abstract.
Shape
ThingsWithStraightSides
Triangle
Rectangle
10
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com