Class Notes (1,200,000)
US (500,000)
Cornell (1,000)
CS (100)
CS 2110 (30)
Gries (30)
Lecture 7

CS 2110 Lecture Notes - Lecture 7: Abstract Data Type, Abstract Type


Department
Computer Science
Course Code
CS 2110
Professor
Gries
Lecture
7

This preview shows page 1. to view the full 4 pages of the document.
Lecture 7 - Abstract and Interfaces
A Little Geometry!
Whe usig a GUI, a shape’s positio is gie y the top left orer of the
box it can be inscribed in
Abstract classes/methods
Problem!
We do’t like the reatio of Shape
objects
Why? Because they are too abstract
How do we avoid this
public abstract class Shape(...) {...}
The abstract prevents the user from using the Shape
constructor
If you tried to call the constructor you get a Static Error
A concrete class is required
If it was just public class Shape(...)
{...}
We can call the constructor and run into problems
So this forces the user to construct objects of the right type
Suppose we have in the abstract class Shape a sumAreas() method
This would involve taking a list of
Shapes and adding the area of each of them
If we called a helper function for
each shape, we get a problem:
Because the sumAreas method is defined in Shape but the
area method is NOT, it will not use the bottom up rule
We make an area method in Shape
public abstract double area();
Now it will go down the chain and
use the bottom up rule for the correct shape
Abstract summary
To make it impossible to create an
instance of a class C, make C abstract:
public abstract C {...}
In an abstract class, to require each
subclass to override method m(...) make m abstract:
find more resources at oneclass.com
find more resources at oneclass.com
You're Reading a Preview

Unlock to view full version