30
CSCI-383 Object-Oriented Programming & Design Lecture 13

CSCI-383 Object-Oriented Programming & Design Lecture 13

Embed Size (px)

Citation preview

Page 1: CSCI-383 Object-Oriented Programming & Design Lecture 13

CSCI-383

Object-Oriented Programming & Design

Lecture 13

Page 2: CSCI-383 Object-Oriented Programming & Design Lecture 13

Chapter 4

Classes and Methods

Page 3: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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 message passing, method lookup, member function

invocation, method binding methods, member function, method function inheritance, subclassing

Page 4: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Encapsulation and Instantiation

Classes provide a number of very important capabilities Encapsulation - The purposeful hiding of

information, thereby reducing the amount of details that need 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

Page 5: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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

Page 6: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Behavior and State

A class can also be viewed as a combination of behavior and state Behavior: The actions that an instance can perform

in response to a request. Implemented by methods State: The data that an object must maintain in

order to successfully complete its behavior. Stored in instance variables (also known as data members, or data fields)

Page 7: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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

Page 8: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

A Typical Example, Class Definition in C++

Page 9: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Visibility Modifiers

The terms public and private are used to 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

Page 10: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

A C# Class Definition

C# class definitions have minor differences, no semicolon at the end, enum cannot be nested inside a class, and visibility modifiers are applied to methods and data fields individually

Page 11: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Java Class Definition

Java also applied visibility modifiers to each item individually. Does not have enumerated data types, uses symbolic constants instead

Page 12: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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).

Page 13: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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, now much less commonly used

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

Page 14: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Class Definition in Apple Object Pascal

No explicit visibility modifiers (will later see syntax for methods)

Page 15: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Delphi Pascal

Slightly different syntax, must name parent class, has visibility modifier, requires the creation of a constructor

Page 16: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Smalltalk

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)

Page 17: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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

Page 18: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

An Example, from C#

Page 19: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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

Page 20: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Accessor (or getter) Methods

An accessor (or getter) is a method that simply returns an internal data value

Page 21: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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

Page 22: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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

Page 23: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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

Page 24: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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

Page 25: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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)

Notice need for fully qualified names

Page 26: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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

(at the discretion of the compiler) be expanded in-line. Another reason for only defining very small methods this way

Page 27: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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++

Page 28: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Methods without Classes in Oberon

Oberon does not have classes, per se, but allows methods to be defined as a funny type of function

Page 29: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

Interfaces in Java

An interface is like a class, but it provides no implementation. Later, another class declare that it supports the interface, and it must then give an implementation. We will have much more to say about interfaces later after we discuss inheritance

Page 30: CSCI-383 Object-Oriented Programming & Design Lecture 13

Adapted From: An Introduction to Object Oriented Programming, 3rd Edition, by Timothy Budd

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