Outline
Other Material
Chapters 4 and 5 present two sides of OOP:
All OOP languages have the following concepts, although the terms they use may differ:
Classes provide a number of very important capabilities:
As we noted in the last chapter, encapsulation means there are two views of the same system. The outside, or service view, describes what an object does. The inside, or implementation view, describes how it does it.
A class can also be viewed as a combination of behavior and state.
We will use as a running example the class definition for a playing card abstraction, and show how this appears in several languages.
(Not all languages are shown in the overhead slides, more are provided in the textbook and on the web pages for the book).
Languages we will consider in this book include Java, C++, C#, Delphi Pascal, Apple Pascal, Ruby, Python, Eiffel, Objective-C and Smalltalk.
class PlayingCard { public: enum Suits {Spade, Diamond, Club, Heart}; Suits suit () { return suitValue; } int rank () { return rankValue; } private: Suits suitValue; int rankValue; };
Note syntax for methods, data fields, and visibility modifiers. (Will see more on syntax later).
The terms public and private are used to differentiate the internal and external aspects of a class.
Typically methods are public and data fields are private, but either can be placed in either category.
enum Suits {Spade, Diamond, Club, Heart}; class PlayingCard { public Suits suit () { return suitValue; } public int rank () { return rankValue; } private Suits suitValue; private int rankValue; }
C# class definitions have minor differences, no semicolon at end, enum cannot be nested inside class, and visibility modifiers are applied to methods and data fields individually.
class PlayingCard { public int suit () { return suitValue; } public int rank () { return rankValue; } private int suitValue; private int rankValue; public static final int Spade = 1; public static final int Diamond = 2; public static final int Club = 3; public static final int Heart = 4; }
Java also applies visibility modifiers to each item indivually. Does not have enumerated data types, uses symbolic constants instead.
Notice how symbolic constants are defined in Java:
public static final int Spade = 1; public static final int Diamond = 2; public static final int Club = 3; public static final int Heart = 4;
We will consider two dialects of Pascal, both descended from the earlier language.
Many similaries due to the common heritage, but some important differences.
(We consider only the language aspects of Delphi, there are many other
features related to its visual interface that we will not describe).
type Suits = (Heart, Club, Diamond, Spade); PlayingCard = object suit : Suits; rand : integer; end;
No explicit visibility modifiers. (Will later see syntax for methods).
type Suits = (Heart, Club, Diamond, Spade); TPlayingCard = class (TObject) public constructor Create (r : integer; s : Suits); function suit : Suits; function rank : int; private suitValue : Suits; rankValue : integer; end;
Slightly different syntax, must name parent class, has visibility modifiers and constructors (more on those later).
Smalltalk doesn't have a textual description for classes, but instead you define classes in a visual interface. (Revolutionary idea in 1980, but now Visual Basic and Delphi programmers are used to similar facilities).
Although syntax will differ depending upon language, all methods have the following:
class PlayingCard { // constructor, initialize new playing card public PlayingCard (Suits is, int ir) { suit = is; rank = ir; faceUp = true; } // operations on a playing card public boolean isFaceUp () { return faceUp; } public int rank () { return rankValue; } public Suits suit () { return suitValue; } public void setFaceUp (boolean up) { faceUp = up; } public void flip () { setFaceUp( !faceUp);} public Color color () { if ((suit() == Suits.Diamond) || (suit() == Suits.Heart)) return Color.Red; return Color.Black; } // private data values private Suits suitValue; private int rankValue; private boolean faceUp; }
class PlayingCard { // constructor, initialize new playing card public PlayingCard (Suits is, int ir) { suit = is; rank = ir; faceUp = true; } ... }
A constructor is a method that is used to initialize a newly constructed object. In C++, Java, C# and many other languages it has the same name as the class. We will talk about constructors more in the next chapter.
An accessor (or getter) is a method that simply returns an internal data value:
class PlayingCard { ... // operations on a playing card public int rank () { return rankValue; } public Suits suit () { return suitValue; } ... private int rankValue; }
There are many reasons why an accessor is preferable to providing direct access to a data field.
Some conventions encourage the use of a name that begins with get, (as in getRank()), but this is not universally followed.
A setter (sometimes called a mutator method) is a method that is used to change the state of an object:
class PlayingCard { // operations on a playing card public void setFaceUp (boolean up) { faceUp = up; } ... // private data values private boolean faceUp; }
Mutators are less common than accessors, but reasons for using are similar.
Some languages allow data fields to be declared as constant (const modifier in C++, final in Java, other languages have other conventions).
Constant data fields can be declared as public, since they cannot be changed.
class PlayingCard { // Java example ... public static final int Spade = 1; public static final int Diamond = 2; public static final int Club = 3; public static final int Heart = 4; }
For the most part, languages don't care about the order that methods are declared. Here are some guidelines:
Remember that class definitions will often be read by people other than the original programmer. Remember the reader, and make it easy for them.
In some languages (such as C++ or Object Pascal) the definition of a method can be separated from its implementation. They may even be in a different file:
class PlayingCard { public: ... Colors color () ; ... }; PlayingCard::Colors PlayingCard::color ( ) { // return the face color of a playing card if ((suit == Diamond) || (suit == Heart)) return Red; return Black; }
Notice need for fully-qualified names.
In C++ you have a choice to define a method in the class interface, or separately in an implementation file. How do you decide?
We will consider a few of the mostly language-specific variations on the idea of a class.
Oberon does not have classes, per se, but allows methods to be defined as a funny type of function:
TYPE PlayingCard = POINTER TO PlayingCardDesc; PlayingCardDesc = RECORD suit : INTEGER; rank : INTEGER; faceUp: BOOLEAN; END PROCEDURE (aCard: PlayingCard) setFaceUp (b : BOOLEAN); BEGIN aCard.faceUp = b; END
An interface is like a class, but it provides no implementation. Later, another class can declare that it supports the interface, and it must then give an implementation.
public interface Storing { void writeOut (Stream s); void readFrom (Stream s); }; public class BitImage implements Storing { void writeOut (Stream s) { // ... } void readFrom (Stream s) { // ... } };
We will have much more to say about interfaces later after we discuss inheritance.
Properties are a way to define getters and setters, but allow them to be used as if they were simple assignments and expressions:
writeln ('rank is ', aCard.rank); (* rank is property of card *) aCard.rank = 5; (* changing the rank property *)
public class PlayingCard { public int rank { get { return rankValue; } set { rankValue = value; } } ... private int rankValue; }
Omitting a set makes it read-only, omitting a get makes it write-only.
Some languages (C++ or Java) allow a class definition to be given inside another class definition. Whether the inner class can access features of the outer class is different in different languages.
class LinkedList { ... private class Link { // inner class public int value; public Link next; } }
Idea is that all instances of a class can share a common data field. Simple idea, but how to resolve the following paradox. All instances have the same behavior:
Different languages use a variety of mechanisms to get around this. See text for details.
In this chapter we have examined the static, or compile time features of classes: