Object-Oriented Programming & Design Lecture 14 Martin van Bommel

Slides:



Advertisements
Similar presentations
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Road Map Introduction to object oriented programming. Classes
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Classes CMPS Overview Terms Class vs instance Class definitions in various languags Access modifiers Methods Constants Separating definition and.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Messages, Instances, and Initialization (Methods) CMPS 2143.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Agenda Object Oriented Programming Reading: Chapter 14.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Lecture 4 : Data Abstraction with C++ : class
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
OOP: Encapsulation &Abstraction
Classes (Part 1) Lecture 3
Classes C++ representation of an object
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 13 Abstract Classes and Interfaces
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Abstract Data Types and Encapsulation Concepts
Inheritance Basics Programming with Inheritance
Corresponds with Chapter 7
Interface.
Chapter 6 Methods: A Deeper Look
Chapter 9 Objects and Classes
Computer Programming with JAVA
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Review of Previous Lesson
Classes C++ representation of an object
C++ Object Oriented 1.
Presentation transcript:

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

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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

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 14 M. van Bommel

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

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel

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 14 M. van Bommel