Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming & Design Lecture 14 Martin van Bommel

Similar presentations


Presentation on theme: "Object-Oriented Programming & Design Lecture 14 Martin van Bommel"— Presentation transcript:

1 Object-Oriented Programming & Design Lecture 14 Martin van Bommel
CSCI 383 Object-Oriented Programming & Design Lecture 14 Martin van Bommel

2 Same Ideas, Different Terms
All OOP languages have the following concepts, although the terms they use may differ classes, object type, factory object instances, objects methods, member function, method function message passing, method lookup, method binding, member function invocation inheritance, subclassing Fall 2010 CSCI 383 Lecture M. van Bommel

3 Encapsulation and Instantiation
Classes provide a number of important capabilities Encapsulation The purposeful hiding of information Reduces the amount of detail that needs to be remembered/communicated among programmers A Service View The ability to characterize an object by the service it provides, without knowing how it performs its task Instantiation The ability to create multiple instances of an abstraction Fall 2010 CSCI 383 Lecture M. van Bommel

4 Internal and External Views
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 Fall 2010 CSCI 383 Lecture M. van Bommel

5 CSCI 383 Lecture 14 M. van Bommel
Behavior and State A class can also be viewed as a combination of behavior and state Behavior The actions an instance performs in response to a request Implemented by methods State The data that an object maintains in order to complete its behavior Stored in instance variables (also known as data members, or data fields) Fall 2010 CSCI 383 Lecture M. van Bommel

6 CSCI 383 Lecture 14 M. van Bommel
Class Definitions We will use as running example the class definition for a playing card abstraction, and show how this appears in several languages Languages considered in the book include: Java, C++, C# Delphi Pascal, Apple Pascal Ruby, Python, Eiffel Objective-C and Smalltalk Fall 2010 CSCI 383 Lecture M. van Bommel

7 CSCI 383 Lecture 14 M. van Bommel
A C++ Class Definition class PlayingCard { public: enum Suits {Spades, Diamonds, Clubs, Hearts}; Suits suit() { return suitValue; } int rank() { return rankValue; } private: suit suitValue; rank rankValue; }; Fall 2010 CSCI 383 Lecture M. van Bommel

8 CSCI 383 Lecture 14 M. van Bommel
Visibility Modifiers The terms public and private differentiate the internal and external aspects of a class public features can be seen and manipulated by anybody they are the external (interface or service) view private features can be manipulated only within a class. They are the internal (implementation) view Typically methods are public and data fields are private, but either can be placed in either category Fall 2010 CSCI 383 Lecture M. van Bommel

9 CSCI 383 Lecture 14 M. van Bommel
A C# Class Definition enum Suits {Spades, Diamonds, Clubs, Hearts}; class PlayingCard { public Suits suit() { return suitValue; } public int rank() { return rankValue; } private Suits suitValue; private rank rankValue; } C# class definitions have minor differences: no semicolon at the end enum cannot be nested inside a class visibility modifiers are applied to methods and data fields individually Fall 2010 CSCI 383 Lecture M. van Bommel

10 CSCI 383 Lecture 14 M. van Bommel
Java Class Definition class PlayingCard { public int suit () { return suitValue; } public int rank () { return rankValue; } private int SuitValue; private int rankValue; public static final int Spades = 1; public static final int Diamonds = 2; public static final int Clubs = 3; public static final int Hearts = 4; } Java also applies visibility modifiers to each item individually. Does not have enumerated data types, uses symbolic constants instead Fall 2010 CSCI 383 Lecture M. van Bommel

11 CSCI 383 Lecture 14 M. van Bommel
Static and Final Notice how symbolic constants are defined in Java static means that all instance share the same value. One per class. Similar meaning in many languages final is Java specific, and means it will not be reassigned. (C++ has const keyword that is similar, although not exactly the same). public static final int Spades = 1; public static final int Diamonds = 2; public static final int Clubs = 3; public static final int Hearts = 4; Fall 2010 CSCI 383 Lecture M. van Bommel

12 CSCI 383 Lecture 14 M. van Bommel
Pascal Dialects We will consider two dialects of Pascal, both descended from the earlier language Apple Object Pascal, defined by Apple Computer once widely used on the Macintosh much less commonly used now Delphi Pascal, defined by Borland on the PC still fairly widely used on that platform called Kylix on the Linux platform Many similarities due to the common heritage, but some important differences Fall 2010 CSCI 383 Lecture M. van Bommel

13 Class Definition in Apple Object Pascal
Type Suits {Spades, Diamonds, Clubs, Hearts}; PlayingCard = object suit : Suits; rank : integer; end; No explicit visibility modifiers (will see syntax for methods later) Fall 2010 CSCI 383 Lecture M. van Bommel

14 CSCI 383 Lecture 14 M. van Bommel
Delphi Pascal Type Suits {Spades, Diamonds, Clubs, Hearts}; TPlayingCard = class (TObject) public constructor Create(r:integer, s:Suits); function suit : Suits; function rank : integer; private suitValue : Suits; rankValue : integer; end; Slightly different syntax, must name parent class, has visibility modifier, requires creation of a constructor Fall 2010 CSCI 383 Lecture M. van Bommel

15 CSCI 383 Lecture 14 M. van Bommel
Smalltalk Smalltalk does not have 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 Fall 2010 CSCI 383 Lecture M. van Bommel

16 CSCI 383 Lecture 14 M. van Bommel
Methods Although syntax will differ depending upon language, all methods have the following A name that will be matched to a message to determine when the method should be executed A signature, which is the combination of return type and argument types. Methods with the same name can be distinguished by different signatures A body, which is the code that will be executed when the method is invoked in response to a message Fall 2010 CSCI 383 Lecture M. van Bommel

17 CSCI 383 Lecture 14 M. van Bommel
An Example, from C# Fall 2010 CSCI 383 Lecture M. van Bommel

18 CSCI 383 Lecture 14 M. van Bommel
Constructor 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 Fall 2010 CSCI 383 Lecture M. van Bommel

19 Accessor (or getter) Methods
An accessor (or getter) is a method that simply returns an internal data value Fall 2010 CSCI 383 Lecture M. van Bommel

20 CSCI 383 Lecture 14 M. van Bommel
Why Use an Accessor? There are many reasons why an accessor is preferable to providing direct access to a data field You can make the data field read-only It provides better documentation that the data field is accessible It makes it easier to later change the access behavior (count number of accesses, whatever) Some conventions encourage the use of a name that begins with get, (as in getRank()), but this is not universally followed Fall 2010 CSCI 383 Lecture M. van Bommel

21 CSCI 383 Lecture 14 M. van Bommel
Setters (or mutators) A setter (sometimes called a mutator method) is a method that is used to change the state of an object Mutators are less common than accessors, but reasons for using are similar Fall 2010 CSCI 383 Lecture M. van Bommel

22 CSCI 383 Lecture 14 M. van Bommel
Constant Data Fields 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 Fall 2010 CSCI 383 Lecture M. van Bommel

23 CSCI 383 Lecture 14 M. van Bommel
Order of Methods For the most part, languages don't care about the order that methods are declared. Here are some guidelines List important methods first Constructors are generally very important, list them first Put public features before private ones Break long lists into groups List items in alphabetical order to make it easier to search Remember that class definitions will often be read by people other than the original programmer. Remember the reader, and make it easy for them Fall 2010 CSCI 383 Lecture M. van Bommel

24 Separation of Definition and Implementation
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 (e.g., the “header” file and the “implementation” file) Fall 2010 CSCI 383 Lecture M. van Bommel

25 Considerations in Method Definitions
In C++ you have a choice to define a method in the class interface, or separately in an implementation file. How do you decide? Readability Only put very small methods in the class definition, so that it is easier to read Semantics. Methods defined in class interface may be expanded in-line (at the discretion of the compiler). Another reason for only defining very small methods this way Fall 2010 CSCI 383 Lecture M. van Bommel

26 CSCI 383 Lecture 14 M. van Bommel
Variations on Classes We will consider a few of the mostly language-specific variations on the idea of a class Methods without classes in Oberon Interfaces in Java (methods without implementations) Nested classes in Java and C++ Fall 2010 CSCI 383 Lecture M. van Bommel

27 Methods without Classes in Oberon
Oberon does not have classes, per se, but allows methods to be defined as a funny type of function Fall 2010 CSCI 383 Lecture M. van Bommel

28 CSCI 383 Lecture 14 M. van Bommel
Interfaces in Java An interface is like a class with no implementation. Later, another class declared that supports the interface, and it must then give an implementation. We will have much more to say about interfaces later after we discuss inheritance Fall 2010 CSCI 383 Lecture M. van Bommel

29 Inner or Nested Classes
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 Fall 2010 CSCI 383 Lecture M. van Bommel


Download ppt "Object-Oriented Programming & Design Lecture 14 Martin van Bommel"

Similar presentations


Ads by Google