Understanding Object-Oriented Programming: Shapes, Abstract Classes, and Polymorphism, Papers of Computer Science

An introduction to object-oriented programming concepts using the example of a shape class and its subclasses triangle, circle, and square. The concept of an object contract, abstract classes, abstract methods, and polymorphism. It also discusses the difference between abstract and concrete classes and the use of interfaces.

Typology: Papers

Pre 2010

Uploaded on 08/16/2009

koofers-user-x30
koofers-user-x30 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
The object contractThe object contract
Abstract classes
Abstract Vs Concrete
Abstract
Vs
.
Concrete
Reference type Vs. Object type
Multiple Inheritance
Interfaces
The object contractThe object contract
zThe superclass defines how the objects are to be used (the contract).
Functionality can be overridden, but the method name/purpose
defines the contract.
zExample: Say we wish to keep track of a bunch of 2D shapes to bounce
around a Jpanel
zAll shapes need: constructor, draw(), move() Shape
Shape (x, y, dx, dy, color)
97
Wright State University, College of Engineering
Dr. T. Doom, Computer Science & Engineering CS 241
Computer Programming II
zconstructor and move() is the same all shapes
zdraw() is different code for each shape
zPolymorphism allows us to treat all objects similarly
object.move()
object.draw()
zThis is the “contract” – how to use the object
The contract is the same for all subclasses
move()
draw()
Triangle
draw () Circle
draw () Square
draw ()
Polymorphism as a contract:Polymorphism as a contract:
Using the Shape ClassUsing the Shape Class
ArrayList<Shape> shapeList;
public void makeShapes () {
shapeList = new ArrayList<Shapes>();
shapeList.add(new Circle(0,0,5,3,Color.Red));
shapeList.add(new Square(10,10,-1,-5,Color.Green));
} // end makeShapes
98
Wright State University, College of Engineering
Dr. T. Doom, Computer Science & Engineering CS 241
Computer Programming II
public void paintComponent (Graphics g) {
for (Shape shape : shapeList) {
shape.move();
shape.draw();
}
} // end method paintComponent
pf3
pf4
pf5

Partial preview of the text

Download Understanding Object-Oriented Programming: Shapes, Abstract Classes, and Polymorphism and more Papers Computer Science in PDF only on Docsity!

The object contractThe object contract

Abstract classes Abstract Vs ConcreteAbstract Vs. Concrete Reference type Vs. Object type Multiple Inheritance Interfaces

The object contractThe object contract

z The superclass defines how the objects are to be used (the contract).

  • Functionality can be overridden, but the method name/purpose

defines the contract.

z Example: Say we wish to keep track of a bunch of 2D shapes to bounce

around a Jpanel

z All shapes need: constructor, draw(), move()

Shape Shape (x, y, dx, dy, color)

97 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering

CS 241 Computer Programming II

z constructor and move() is the same all shapes

z draw() is different code for each shape

z Polymorphism allows us to treat all objects similarly

  • object.move()
  • object.draw()

z This is the “contract” – how to use the object

  • The contract is the same for all subclasses

move() draw()

Triangle draw ()

Circle draw ()

Square draw ()

Polymorphism as a contract:Polymorphism as a contract:

Using the Shape ClassUsing the Shape Class

ArrayList shapeList; … public void makeShapes () { shapeList = new ArrayList(); shapeList.add(new Circle (0,0,5,3,Color.Red)); shapeList.add(new Square (10,10,-1,-5,Color.Green)); } // end makeShapes

98 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering

CS 241 Computer Programming II

public void paintComponent (Graphics g) { for (Shape shape : shapeList) { shape.move(); shape.draw(); } } // end method paintComponent …

How do I define Shape?How do I define Shape?

z What should the default code be for draw()?

z What should happen if I try to make a new Shape()?

  • What would it look like?

Shape Shape (x, y, dx, dy, color)

Arra List shapeList

99 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering

CS 241 Computer Programming II

move() draw()

Triangle draw ()

Circle draw ()

Square draw ()

ArrayList shapeList; … public void makeShapes () { shapeList = new ArrayList(); shapeList.add( new Circle(0,0,5,3,Color.Red)); shapeList.add( new Shape(10,10,1,-5,Color.Green)); } // end makeShapes …

Abstract classesAbstract classes

z Some classes should never be instantiated

  • Some superclasses exists only to define a contract
  • You may never want to allow anyone to create one!

z Marking a class abstract tells the compiler to create an instance of the

class. Its only use is to be extended! Shape

abstract class Shape {^ Shape (x, y, dx, dy, color)

100 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering

CS 241 Computer Programming II

move() draw()

Triangle draw ()

Circle draw ()

Square draw ()

p { Shape (int x, int y, int dx, int dy, Color color) { … // my code to inherit here } // end constructor Shape void move () { … // my code to inherit here } // end method move void draw () { … // what should go here? } // end method draw } // end class Shape

Abstract Vs. Concrete classesAbstract Vs. Concrete classes

z Abstract class: A class that cannot be

instantiated

  • An abstract class has virtually no use,

no value, and no purpose

  • It can have static members
  • It can define a contract (this is, it can be

Shape Shape (x, y, dx, dy, color)

ABSTRACT

UML Note: Use italics to indicate abstract

101 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering

CS 241 Computer Programming II

extended)

z Concrete class: A class that is not abstract

  • The objects created and doing the work

at runtime are concrete

  • These objects may be instances of a

subclass of an abstract class

move() draw()

Triangle draw ()

Circle draw ()

Square draw () CONCRETE CONCRETE CONCRETE

The object contractThe object contract

z For an object of type Triangle

  • Everything accessible in class Triangle defines part of your contract
  • Everything accessible in class Shape defines part of your contract
  • Everything accessible in class Object defines part of your contract

z Accessible: public or protected Shape

Shape (x, y, dx, dy, color)

105 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering

CS 241 Computer Programming II

move() draw ()

Triangle draw () rotate()

Circle draw ()

Square draw () rotate()

Object Object() equals (object ) toString () clone(object) getClass() …

Shape Shape() move() draw()

Triangle

draw() rotate()

t

Multiple inheritance IMultiple inheritance I

z What if I want to create an object that fulfills the contracts of two

separate (unrelated) existing classes?

z This would allow us to use all the existing code that works for either

method

z Example: GUI_Card

  • It’s a card, and can be used with existing

106 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering

CS 241 Computer Programming II

Cardgame code

  • It also needs to be displayed on the GUI,

and is essentially just a Square

z We want the following to be true:

  • GUI_Card IS-A Card
  • GUI_Card IS-A Square

Square draw () rotate()

Card show()

GUI_Card draw () rotate()

Multiple inheritance: Here be dragons!Multiple inheritance: Here be dragons!

z What if both superclasses have a member with the same name?

z How do we avoid ambiguity?

  • Extra syntax?
  • How do we help check for errors?
  • Basically, this is hard

107 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering

CS 241 Computer Programming II

z Some languages allow this (C++, for one)

  • C++ is all about handling hard

z Some languages don’t (Java, for one)

  • Java is all about the simple

z Java does provide limited multiple inheritance

Square draw () rotate()

Card show() draw()

GUI_Card draw () Which draw() superclass method do I inherit?!? rotate()

InterfacesInterfaces

z Java allows multiple inheritance to interface classes

z Interface classes are:

  • 100% pure abstract class
  • They contain only abstract, public methods

z Thus there is no ambiguity. The interface defines the contract but forces

the subclass object to define the one and only one implementation.

108 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering

CS 241 Computer Programming II

the subclass object to define the one and only one implementation.

Shape draw () move ()

Card show() draw()

GUI_Card draw () rotate() move()

public interface Shape { public abstract void move (); public abstract void draw (); } // end interface Shape

public class GUI_Card extends Card implements Shape { // Implementation must override move/draw } // end class GUI_Card

Contract StyleContract Style

z When do you make a class, a subclass, an abstract class, or an interface?

z Class

  • Appropriate for objects that don’t extend anything
  • Fails the IS-A test for all other types (except Object)

z Subclass (extend a class)

  • Only when you need to make a more specific version of a class

109 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering

CS 241 Computer Programming II

  • Only when you need to make a more specific version of a class
  • Override or add new behaviors

z Abstract class

  • Define a template/contract for a group of subclasses
  • Has some implementation code that all subclasses can use
  • Guarantee that nobody can make an object of that type

z Interface

  • Define a role that other classes can play, regardless of their type