Neal Stublen Key Concepts  Classes  Objects  Inheritance  Polymorphism  Encapsulation.

Slides:



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

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
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.
Road Map Introduction to object oriented programming. Classes
Starting Classes and Methods. Objects have behaviors In old style programming, you had: –data, which was completely passive –functions, which could manipulate.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
ASP.NET Programming with C# and SQL Server First Edition
CSI 101 Elements of Computing Spring 2009 Lecture # 14 – Classes and Objects Wednesday, April 15th, 2009 and Monday, April 20th, 2009.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
LECTURE 07 Programming using C# Inheritance
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Object Oriented Software Development
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Writing Classes (Chapter 4)
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Visual Programming Fall 2012 – FUUAST Topic: Development environment.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Agenda Object Oriented Programming Reading: Chapter 14.
10-Nov-15 Java Object Oriented Programming What is it?
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
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
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”
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Neal Stublen Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 Introduction to Object Oriented Programming Chapter 10.
Object-Oriented Programming “The Rest of the Story”, CS 4450 – Chapter 16.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Classes (Part 1) Lecture 3
Programming Logic and Design Seventh Edition
Phil Tayco Slide version 1.0 Created Sep 18, 2017
Static data members Constructors and Destructors
Classes and OOP.
Chapter 3: Using Methods, Classes, and Objects
Classes.
CS 302 Week 11 Jim Williams, PhD.
More Object Oriented Programming
Corresponds with Chapter 7
Object-Oriented Programming
CIS 199 Final Review.
Presentation transcript:

Neal Stublen

Key Concepts  Classes  Objects  Inheritance  Polymorphism  Encapsulation

Classes and Objects  A class describes what an object looks like Attributes Behavior  An object is an instance of a particular class  A class is like the definition for “mouse” in the dictionary.  Object instances are like a dozen of them sitting on desks in the room.

Attributes  An object’s attributes are represented by a class’ instance variables (or member variables, or fields)  The content of all of an object’s instance variables define its state class Door bool opened bool locked endClass Each instance of Door can have one of four states

Behaviors  An object’s behaviors are represented by its methods class Door void closeDoor return void lockDoor return void openDoor return void unlockDoor return endClass

Similar Yet Different  Consider two objects Movie Song  What attributes do they have in common?  What behaviors do they have in common?  What attributes and behaviors are unique to each one?

Inheritance  If we consider a Movie and a Song to both be types of a MediaItem, we can define a MediaItem and say Movie and Song both inherit certain attributes and behaviors from MediaItem  Heirarchy: MediaItem ○ Song ○ Movie

Polymorphism  We know a MediaItem has behaviors to play and pause, but the behavior is different depending on whether we are referring to a Movie or Song  Defining play and pause methods on a Movie or Song class is an example of polymorphism  The method call looks the same for both objects, but the specific behavior is different A MediaItem can take on many (poly) shapes (morph)

Encapsulation  Every class encapsulates internal information by hiding it from other objects (information hiding)  Hiding an object’s internal information makes it possible to modify the internal structure of an object without affecting other objects that may use it

Defining Classes  Every class has a unique name  Most classes contain data (fields)  Most classes contain methods  A class that only contains simple data values may not define any methods, but this is rare  Our Program classes have always contained methods, but no data – again, this is rare

An Employee Class class Employee string name string getName() return name void setName(string newName) name = newName return endClass  The class has a unique name

An Employee Class class Employee string name string getName() return name void setName(string newName) name = newName return endClass  The class contains a field

An Employee Class class Employee string name string getName() return name void setName(string newName) name = newName return endClass  The class contains methods

A CardStack Class class CardStack num cards[52] num draw() num card = the next card in the stack return card void shuffle() rearrange the cards in the stack return endClass

A Card Class class Card num value// 0 – 51 num getRank() return (value % 13) + 1// 1 – 13 num getSuit() return value / 13// 0 – 3 bool isFaceCard return getRank() > 10// true or false endClass

Declaring Instances  Since a class is like a definition in a dictionary, we need to create an instance of the class to actually do anything  Creating an instance allocates memory for the class variables

Declaring Instances  Declaring variables uses the computer’s memory  Declaring class instances works the same way intint intint intint string intint CardStack... intint intint intint intint intint intint intint intint intint intint intint intint intint intint intint intint

Declaring Instances  Each instance declaration creates a new object num counter = 0 string name = “John Smith” Employee assistant Employee manager Employee president Employee intint string

An Employee with More Fields class Employee int id string name string hireDate …Methods for the class… endClass

Memory for Each Instance Employee intint string intint intint intint  Each instance of Employee has its own allocation of class variables

Using Methods  Methods are accessed using a “dot” notation on an instance of the class // Create an instance of the Employee class Employee manager // We now have an employee object. // Set and get the name for an instance of // the Employee class manager.setName(“John Smith”) output manager.getName() // John Smith

Using Multiple Instances Employee manager manager.setName(“John Smith”) Employee assistant assistant.setName(“Fred Jones”) output manager.getName() // John Smith output assistant.getName() // Fred Jones

Review Which of the following is the same as object? a) class b) field c) instance d) category

Review Which of the following is the same as object? a) class b) field c) instance d) category

Common Types of Methods  “Set” methods are used to set fields in the class class Employee string name void setName(string newName) name = newName return endClass

Common Types of Methods  “Get” methods are used to retrieve data from the class class Employee string name string getName() return name endClass

Common Types of Methods  “Get” methods may combine fields class Employee string firstName string lastName string getFullName() string name = lastName + ", " + firstName return name endClass

Common Types of Methods  “Work” methods may calculate a return value class Circle num radius num calcArea() num area = PI * radius * radius return area num calcCircumference() num circumference = 2 * PI * radius return circumference endClass

Public and Private Access  We may want to control access to some fields and methods  Fields and methods can be declared as public or private  Public fields and methods can be accessed from outside the class  Private fields and methods can only be accessed from within the class

Back to the CardStack  Which fields and methods do we want to protect? class CardStack num cards[52] num draw() num card = the next card in the stack return card void shuffle() swap random cards in the stack return void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 return endClass

Back to the CardStack  We don’t want free access to any card in the stack class CardStack private num cards[52] num draw() num card = the next card in the stack return card void shuffle() swap random cards in the stack return void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 return endClass

Back to the CardStack  We do want access to the card on top of the stack class CardStack private num cards[52] public num draw() num card = the next card in the stack return card void shuffle() swap random cards in the stack return void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 return endClass

Back to the CardStack  We do want ability to shuffle the stack class CardStack private num cards[52] public num draw() num card = the next card in the stack return card public void shuffle() swap random cards in the stack return void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 return endClass

Back to the CardStack  We don’t want the ability to swap any two cards in the stack class CardStack private num cards[52] public num draw() num card = the next card in the stack return card public void shuffle() swap random cards in the stack return private void swap(num cardIndex1, num cardIndex2) switch the card at index 1 with the card at index 2 return endClass

Back to the Card Class  Which fields and methods do we want to protect? class Card num value// 0 – 51 num getRank() return (value % 13) + 1// 1 – 13 num getSuit() return value / 13// 0 – 3 bool isFaceCard return getRank() > 10// true or false endClass

Back to the Card Class  We don’t want the card to be changed class Card private num value// 0 – 51 num getRank() return (value % 13) + 1// 1 – 13 num getSuit() return value / 13// 0 – 3 bool isFaceCard return getRank() > 10// true or false endClass

Back to the Card Class  We do want to access the card’s rank class Card private num value// 0 – 51 public num getRank() return (value % 13) + 1// 1 – 13 num getSuit() return value / 13// 0 – 3 bool isFaceCard return getRank() > 10// true or false endClass

Back to the Card Class  We do want to access the card’s suit class Card private num value// 0 – 51 public num getRank() return (value % 13) + 1// 1 – 13 public num getSuit() return value / 13// 0 – 3 bool isFaceCard return getRank() > 10// true or false endClass

Back to the Card Class  We do want to know if it’s a face card class Card private num value// 0 – 51 public num getRank() return (value % 13) + 1// 1 – 13 public num getSuit() return value / 13// 0 – 3 public bool isFaceCard return getRank() > 10// true or false endClass

Review Assume that a working program contains the following statements: Dog myDog myDog.setName(“Bowser”) Which of the following do you know? a) setName() is a public method of the Dog class b) setName() accepts a string parameter c) both of the above d) none of the above

Review Assume that a working program contains the following statements: Dog myDog myDog.setName(“Bowser”) Which of the following do you know? a) setName() is a public method of the Dog class b) setName() accepts a string parameter c) both of the above d) none of the above

Instance Methods  The methods we’ve seen so far are called instance methods  They operate on a particular instance of a class // Get the name from a specific instance // of the Employee class Employee manager manager.setName(newName) output manager.getName()

But which instance?  From outside a method we reference an instance of a class using an identifier  The identifier determines the instance Employee manager output manager.getName()

But which instance? class Employee private string name public string getName() return name endClass output manager.getName() output assistant.getName()  The compiler generates object code that knows which instance of name should be used when calling getName() manager - name assistant - name

But which instance?  What if we want an instance to reference itself?  There’s no identifier defined within the class and there may be many instances of the class

Use “this” one…  From within a method, you can reference the instance that is “in scope” using the “this” keyword class Employee private string name public string getName() return this.name void setName(string newName) this.name = newName return endClass

Why do I care?  One example… class Employee private string name // What if a parameter identifier overlaps // a field identifier? void setName(string name) name = name return endClass

“this” Resolves Ambiguity  One example… class Employee private string name // What if a parameter identifier overlaps // a field identifier? void setName(string name) this.name = name return endClass

You’re right…  Why not just avoid using the same identifier? class Employee private string mName // What if a parameter identifier overlaps // a field identifier? void setName(string name) mName = name return endClass

Or…  More likely, change the parameter name… class Employee private string mName // What if a parameter identifier overlaps // a field identifier? void setName(string inName) mName = inName return endClass

But…  We will see “this” again where it is more useful.  For now, just know it’s there.

Static Methods  Instance methods operate on a specific instance of a class  Static methods do not operate on a specific instance of the class Which means they cannot modify fields defined within the class  Static methods are typically useful methods that are closely related to the class, but not associated with a particular instance

Examples From our C# examples… Console.ReadLine(); Console.WriteLine();  Console is the class name, but we never create an instance of the class  The Console class is used to group together all the functionality for interacting with a console window

Using Objects (Instances)  We can pass class instances as method parameters and return values class CardStack void add(Card newCard) add a new card to the stack return Card draw() Card nextCard = find the next card in the stack return nextCard endClass

Using Objects (Instances)  But object are passed by reference Counter hiccups hiccups.reset()// count = 0 useCounter(hiccups) output hiccups.getCount() // count = 1 void useCounter(Counter c) // The variables “c” and “hiccups” refer // to the same object. Changing one changes // the other. c.increment() return

Using Objects (Instances)  Objects can be stored in an array Employee[] newHires = Employee[10]

Review Assume that a working program contains the following statements: Cat myCat myCat.setName(“Socks”) Which of the following do you know? a) myCat is an object of a class named Cat b) setName() is a static method c) both of the above d) none of the above

Review Assume that a working program contains the following statements: Cat myCat myCat.setName(“Socks”) Which of the following do you know? a) myCat is an object of a class named Cat b) setName() is a static method c) both of the above d) none of the above

Bringing It All Together  Let’s create a Card class and a CardStack class  A Card represent any one of the standard 52 playing cards  A CardStack can be shuffled  A Card can be drawn from a CardStack  A Card can be added to a CardStack  We should be able to determine how many Cards are in a CardStack

Graphical Objects  Let’s take a look at GUI programming in Visual Studio

Class Diagrams  Represent the definition of a class  Language independent way of describing the class  p. 272  Programming tools can often consume or generate class diagrams

Summary  Principles of object-oriented programming  Using objects  Public and private access  Instance methods  Static methods  GUI object  Class diagrams