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

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
©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.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
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.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT],MPhil (Comp.Sci), PGDCA, ADCA, Dc. Sc. & Engg.
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 and software development Lecture 1.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
The Java Programming Language
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
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.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
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.
Object-Oriented Programming Chapter Chapter
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
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 Lesson 2.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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.
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?
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
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.
Packages and Interfaces
Review of Previous Lesson
Object-Oriented PHP (1)
C++ Object Oriented 1.
Presentation transcript:

CSCI-383 Object-Oriented Programming & Design Lecture 13

Chapter 4 Classes and Methods

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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)

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd A Typical Example, Class Definition in C++

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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).

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Class Definition in Apple Object Pascal  No explicit visibility modifiers (will later see syntax for methods)

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Delphi Pascal  Slightly different syntax, must name parent class, has visibility modifier, requires the creation of a constructor

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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)

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd Edition, by Timothy Budd Accessor (or getter) Methods  An accessor (or getter) is a method that simply returns an internal data value

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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++

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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

Adapted From: An Introduction to Object Oriented Programming, 3 rd 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