






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Modifier – Abstract
Represents generic concept Just a placeholder Leave lower-level details to subclass
Methods Classes
abstract class Foo { // abstract class abstract void bar( ) { … } // abstract method
Motivating Example – Shapes
Picture consists of array shapes of type Shape[ ] To draw the picture, invoke drawMe( ) for all shapes Shape[ ] shapes = new Shape[…]; shapes[0] = new Circle( … ); shapes[1] = new Rectangle( … ); … for ( int i = 0; i < shapes.length; i++ ) shapes[i].drawMe( ); Store the shapes to be drawn in an array. Draws all the shapes. Each call invokes drawMe for the specific shape. Heap: shapes [0] [1] [2] … (a Circle object) (a Rectangle object) …
Motivating Example – Shapes
Shape object does not represent a specific shape Since Shape is just a superclass How to implement Shape’s drawMe( ) method? public class Shape { void drawMe( ) { … } // generic drawing method }
Abstract Method
Since must be overridden by descendent class Final would prevent this
Abstract Class
public abstract class Shape { … }
Cannot be created using “new” Shape s = new Shape( … ); // Illegal! But can create concrete shapes (Circle, Rectangle) and assign them to variables of type Shape Shape s = new Circle( … );
Abstract – Summary
Method that contains no body Subclass provides actual implementation
Required if any method in class is abstract Can contain non-abstract methods Can be partial description of class