Object-Oriented Programming and class Design

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
C++ Classes How to Create and Use Them. 2 Overview Terminology File Topology Designing Classes Functions in Classes (methods)  Constructor  Accessors/Modifiers.
CSE 1301 Lecture 5 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
10-Nov-15 Java Object Oriented Programming What is it?
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
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.
Class Fundamentals BCIS 3680 Enterprise Programming.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Andrew(amwallis) Classes!
Classes (Part 1) Lecture 3
Phil Tayco Slide version 1.0 Created Sep 18, 2017
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?
Static data members Constructors and Destructors
Chapter 3: Using Methods, Classes, and Objects
Creating Your OwnClasses
Chapter 4: Writing Classes
Namespaces, Scopes, Access privileges
Lecture 14 Writing Classes part 2 Richard Gesick.
Lecture 11 B Methods and Data Passing
Java LESSON 7 Objects, Part 1
Lecture 13 Writing Classes Richard Gesick.
CS2102: Lecture on Abstract Classes and Inheritance
Lesson 2: Building Blocks of Programming
Encapsulation and Constructors
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 9 Objects and Classes
Object Oriented Programming
Outline Anatomy of a Class Encapsulation Anatomy of a Method
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming
Object-Oriented Programming and class Design
Java Programming Language
Object-Oriented Programming
Object-Oriented Programming and class Design
Lecture 18 Making C# Objects
Object-Oriented Programming and class Design
Object-Oriented Design AND CLASS PROPERTIES
Visibilities and Static-ness
Parameters, Overloading Methods, and Random Garbage
Object-Oriented Programming and class Design
C++ Object Oriented 1.
Object-Oriented Design AND CLASS PROPERTIES
Creating and Using Classes
CSG2H3 Object Oriented Programming
Object-Oriented Programming and class Design
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

Object-Oriented Programming and class Design Ps Module 6 Object-Oriented Programming and class Design 8/10/2019 CSE 1321 Module 5

Overview Object-Oriented Programming (OOP) is based on the concept of classes, from which objects are created. Remember: Classes are “templates” Objects are instances of those templates 8/10/2019 CSE 1321 Module 6

Classes Using classes, we’re actually creating new data types! A class represents the concept of things in the real world, like Dogs. Classes follow a template, and have: a name variables (often called “attributes”) functions (which are now called “methods”, “behaviors” or “member functions”) 8/10/2019 CSE 1321 Module 6 3

The Smallest Class class Dog { } 8/10/2019 CSE 1321 Module 6

Oh no! What have we done? We’ve: created a new (complex) data type! created the “concept” of a Dog We don’t have any Dogs yet, just the concept 8/10/2019 CSE 1321 Module 6

Objects An object: is an entity in the real world that can be distinctly identified might represent a particular dog, employee, student, etc. has a unique identity, state, and behavior. 8/10/2019 CSE 1321 Module 6 6

A “real life” example Let’s make a Dog! Attributes (characteristics) rabid or not rabid (boolean) weight (a number) name (string) Behaviors growl eat

Step 1: The Skeleton CLASS Dog BEGIN // attributes will go here – name, weight, rabid // behaviors will go here – growl, eat END CLASS Ps

Ps Step 2: Add attributes CLASS Dog BEGIN CREATE rabid ← false CREATE weight ← 0.0 CREATE name ← “ ” // Behaviors go here END CLASS Ps

Step 3: The Constructor This is a special method Used to give initial values to ALL attributes Is activated when someone creates a new instance of the class Doesn’t have a return type In most languages, the name of this method MUST be the same name as the class

Step 3: Designing the Constructor Constructors will vary, depending on design Ask questions: Are all Dogs born with the same rabid state? (yes – they are all born non-rabid) Are all Dogs born with the same weight? (no – they are born with different weights) Are all Dogs born with the same name? (no – they all have different names) If ever “no”, then you need information passed in as parameters.

Ps Step 3: The Constructor CLASS Dog BEGIN CREATE rabid ← false CREATE weight ← 0.0 CREATE name ← “ ” // Constructor CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR // Behaviors go here END CLASS Ps

Step 4: Relax and Take a break We’ll get to growl and eat later We have a new data type (Dog) Let’s play around with the main algorithm

The “Driver” Usually put this in a whole new file! The Driver contains main It’s the controlling algorithm Steps: Type in the skeleton Create a couple of instances of classes Start telling them what to do

Step 1: Type in the Skeleton BEGIN MAIN END MAIN Ps

Step 2: Declare Two Dogs Memory null j1 j2 BEGIN MAIN CREATE j1, j2 AS Dog END MAIN Memory null j1 j2

The new operator Right now, we have two “dead” dogs new Brings instances “to life” Calls the class’s constructor Opens up enough space in memory to fit an instance of that class

Step 3: Bring j1 to Life Memory null j1 j2 BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) END MAIN Memory null CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR j1 j2

Opens Space Memory null j1 j2 BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) END MAIN Memory null CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR j1 j2

Data Passing Memory null j1 j2 BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) END MAIN Memory null CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR j1 j2

Data Passing Memory null j1 j2 BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) END MAIN Memory null CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR j1 j2 rabid:false weight:14 name:"Bob”

Bring j2 to Life Memory null j1 j2 BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) j2 ← new Dog (7, “Ethel”) END MAIN Memory null j1 j2 rabid:false weight:14 name:”Bob”

New calls the Constructor BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) j2 ← new Dog (7, “Ethel”) END MAIN Memory null CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR j1 j2 rabid:false weight:14 name:”Bob”

Opens Space for j2 Memory null j1 j2 BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) j2 ← new Dog (7, “Ethel”) END MAIN Memory null j1 j2 rabid:false weight:14 name:”Bob”

Passes Data to the Constructor BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) j2 ← new Dog (7, “Ethel”) END MAIN Memory null CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR j1 j2 rabid:false weight:14 name:”Bob”

Passes Data to the Constructor BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) j2 ← new Dog (7, “Ethel”) END MAIN Memory null CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR j1 j2 rabid:false rabid:false weight:14 weight:7 name:”Bob” name:”Ethel”

Adding Eat() and Growl() CLASS Dog BEGIN CREATE rabid ← false CREATE weight ← 0.0 CREATE name ← “ ” // Constructor CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR // WE STILL NEED EAT AND GROWL END CLASS Ps

Ps CLASS Dog BEGIN CREATE rabid ← false CREATE weight ← 0.0 CREATE name ← “ ” CONSTRUCTOR Dog (parameters w, n) rabid ← false weight ← w name ← n END CONSTRUCTOR METHOD eat (parameters: amountOfFood) weight ← weight + amountOfFood PRINT (name, “ weighs”, weight, “ lbs”) END METHOD METHOD growl (parameters: none) PRINT (name, “ says GRRRRR!”) END METHOD END CLASS Ps

The “.” operator “Dot” operator used to Format: Get to an instances attributes Get to an instances methods Basically get inside the instance Format: <instance>.<attribute or method>

Using the “.” operator BEGIN MAIN CREATE j1, j2 AS Dog j1 ← new Dog (14, “Bob”) j2 ← new Dog (7, “Ethel”) j1.eat(2) // Bob weighs 16 lbs j2.growl() // Ethel says GRRRR j1.name ← “Fluffy” // This is just cruel j1.eat(4) // Fluffy weighs 20 lbs j2.eat(-1) // Ethel weighs 6 lbs END MAIN

Default Constructors In many languages, there’s a default constructor If you don’t create one, it’s created for you AND IT’S INVISIBLE! AND It takes no parameters AND It sets variables/attributes: To zero (0) for numbers To FALSE for booleans To NULL (empty) for objects like strings 8/10/2019 CSE 1321 Module 4

What it would look like (if you could see it) CLASS Dog BEGIN CREATE rabid CREATE weight CREATE name // It’s INVISIBLE! CONSTRUCTOR Dog ( ) rabid ← false weight ← 0 name ← NULL END CONSTRUCTOR END CLASS Ps

Multiple Constructors (a.k.a. overloading) We can have more than one constructor Remember, constructors are used to initialize our objects We can use parameters with the constructor to customize the initialization Sometimes we have more or less data to use in the customization We’d like to be able to pass in only what we know 8/10/2019 CSE 1321 Module 6 33

Implementation Overloading the constructor involves using the same method/function name Vary the number of parameters; AND/OR Vary the type of parameters 8/10/2019 CSE 1321 Module 6 34

Example CLASS BMW_Z4 BEGIN CONSTRUCTOR BMW_Z4 () // constructor #1 BEGIN ModelYear ← 2004 TopUp ← false LicensePlate ← "DEALER" END CONSTRUCTOR CONSTRUCTOR BMW_Z4 (parameter: year) // constructor #2 BEGIN ModelYear ← year TopUp ← false LicensePlate ← "DEALER" END CONSTRUCTOR END CLASS Ps 8/10/2019 CSE 1321 Module 6 35

Which is Called? BMW_Z4 myCar = new BMW_Z4(); BMW_Z4 yourCar = new BMW_Z4(2007); BMW_Z4 herCar = new BMW_Z4(2008); The constructor with the matching parameter definition will be called If no parameters, the () constructor If one parameter, the (int) constructor 8/10/2019 CSE 1321 Module 6 36

Java - Constructor Example class BMW_Z4 { int modelYear; String licensePlate; boolean topUp; BMW_Z4 () { modelYear = 2004; topUp = false; licensePlate = "DEALER"; } BMW_Z4 (int year) modelYear = year; topUp = false; } 8/10/2019 CSE 1321 Module 6 37

C# - Constructor Example class BMW_Z4 { int modelYear; string licensePlate; bool topUp; BMW_Z4 () { modelYear = 2004; topUp = false; licensePlate = "DEALER"; } BMW_Z4 (int year) modelYear = year; topUp = false; } 8/10/2019 CSE 1321 Module 6 38

Visibility of Class Content – Public and Private Problem: Imagine class “Person” with an “age” attribute Then, a “bad guy” directly sets the age to -3! We need protection/security We need a way of selectively “publishing” parts of a class and “hiding” other parts of the class Use public & private keywords 8/10/2019 CSE 1321 Module 6 39

Sloppy Definitions public: anyone (or anything) can see it from anywhere! That is, anything outside the class. private: can only be seen/called inside the class. Note, variables, methods and even classes can be marked either public or private 8/10/2019 CSE 1321 Module 4

General Rules Variables/attributes are normally marked private public variables can be tampered with and abused! Methods are most often marked public However, some methods should be marked as private 8/10/2019 CSE 1321 Module 4

Visibility Example Class Name: BMW_Z4 Attributes: ModelYear (private visibility, integer type) LicensePlate (public visibility, string type – why public?) TopUp (private visibility, boolean type) Methods: Drive (public visibility, no return value) OpenTop() (public visibility, returns boolean) EmitPollutantsWhenNoOneIsLooking() (private?) 8/10/2019 CSE 1321 Module 6 42

Visibility and Access from main CREATE myCar AS BMW_Z4 // Create an object called myCar myCar = new BMW_Z4 // Bring it to life myCar.LicensePlate = "BMR4ME” // Legal. LicensePlate is public myCar.ModelYear = 2018 // Illegal because ModelYear is private myCar.Drive() // Legal. Drive() is public myCar.OpenTop() // Legal. OpenTop() is public // Illegal for multiple reasons… myCar.EmitPollutantsWhenNoOneIsLooking() 8/10/2019 CSE 1321 Module 6

Wait! Then, how do we change private variables? Let’s review, shall we? 8/10/2019 CSE 1321 Module 4

Public Doggies (with private lives) PUBLIC CLASS Dog BEGIN PRIVATE CREATE rabid // change this? PRIVATE CREATE weight // and this? PUBLIC CREATE name // Boring constructor PUBLIC CONSTRUCTOR Dog ( ) rabid ← false weight ← 0 name ← NULL END CONSTRUCTOR END CLASS Ps

Accessors/Modifiers Main idea: create methods to access and change attributes Also called “getters” and “setters” Why is this cool? Can put in protections to prevent bad data PUBLIC setAge (parameter: newAge) BEGIN IF (newAge < 110) THEN age = newAge ELSE PRINT (“That’s too old!”) ENDIF END 8/10/2019 CSE 1321 Module 4

Properties Example Pseudocode CLASS BMW_Z4 BEGIN PRIVATE ModelYear PRIVATE LicensePlate PUBLIC METHOD SetModelYear (Year) BEGIN ModelYear ← Year END PUBLIC METHOD GetModelYear () BEGIN RETURN ModelYear END PUBLIC METHOD SetLicensePlate (value) BEGIN LicensePlate ← value END PUBLIC METHOD GetLicensePlate () BEGIN RETURN LicensePlate END END CLASS Ps 4/26/2018 CSE 1321 Module 6 5

Class BMW_Z4 public class BMW_Z4 { private int ModelYear; private String LicensePlate; private boolean TopUp; public void SetModelYear(int Year) { ModelYear = Year; } public int getModelYear() { return ModelYear; } public void SetLicensePlate(String value) {LicensePlate = value;} public String getLicensePlate(){ return LicensePlate; } public void SetTopUp(boolean value) { TopUp = value; } public boolean getTopUp() { return TopUp; } } 4/26/2018 CSE 1321 Module 6 6

Class BMW_Z4 public class BMW_Z4 { private int modelYear; private string licensePlate; private bool topUp; public int ModelYear { get {return modelYear;} set {modelYear = value; }} public string SetLicensePlate { get {return licensePlate;} set{licensePlate = value;}} public bool TopUp { get {return TopUp;} set{topUp = value; }} } 4/26/2018 CSE 1321 Module 6 7

A common point of confusion We see a lot of keywords like this and self What is that? A reference to something inside the class we’re coding in Commonly used to resolve ambiguity of variables: 8/10/2019 CSE 1321 Module 4

A point of confusion Ps PUBLIC CLASS Dog BEGIN PRIVATE CREATE rabid PRIVATE CREATE weight PUBLIC CREATE name PUBLIC CONSTRUCTOR Dog (parameter: rabid, weight, name) rabid ← rabid // what? weight ← weight // huh? name ← name // wuh? END CONSTRUCTOR END CLASS 8/10/2019 CSE 1321 Module 4

Better Ps PUBLIC CLASS Dog BEGIN PRIVATE CREATE rabid PRIVATE CREATE weight PUBLIC CREATE name PUBLIC CONSTRUCTOR Dog (parameter: rabid, weight, name) this.rabid ← rabid // assign attributes this.weight ← weight // the parameters this.name ← name END CONSTRUCTOR END CLASS 8/10/2019 CSE 1321 Module 4

Constructor Chaining It’s possible for one constructor to leverages/calls another You can do this as many times as you want Let’s see an example 8/10/2019 CSE 1321 Module 4

PUBLIC CLASS Dog BEGIN PRIVATE CREATE rabid PRIVATE CREATE weight PUBLIC CREATE name PUBLIC CONSTRUCTOR Dog ( ) // Default constructor // Call the constructor below this (FALSE, 4, “Fluffy”) END CONSTRUCTOR PUBLIC CONSTRUCTOR Dog (parameter: rabid, weight, name) this.rabid ← rabid this.weight ← weight this.name ← name END CLASS Ps 8/10/2019 CSE 1321 Module 4

The static keyword Only attributes and methods can be marked static Static attributes: Previously, each instance/object had it’s own independent set of variables With static all instances share one variable Good for counting the number of instances 8/10/2019 CSE 1321 Module 4

Example of static attributes class Dog { static int dogCounter = 0; // Shared by all dogs private int dogID; // Each dog has its own copy Dog() { dogID = dogCounter++; } } Dog d1 = new Dog(); // d1’s ID is 0, dogCounter is now 1 Dog d2 = new Dog(); // d2’s ID is 1, dogCounter is now 2 8/10/2019 CSE 1321 Module 4

Static methods A method marked static: Can call these methods like normal, BUT… …don’t have to create an instance to use the method! Can’t access non-static attributes Is called using <Class Name>.<Method Name> 8/10/2019 CSE 1321 Module 4

Static Method Example class Dog { static int dogCounter = 0; private dogID; Dog() { dogID = dogCounter++; } static void allowed() { dogCounter *= 2; static void notAllowed() { // Won’t compile dogID = 10; } Dog d1 = new Dog(); d1.allowed(); Dog.allowed(); Dog.notAllowed(); // Why doesn’t this work? 8/10/2019 CSE 1321 Module 4

Summary A class: has attributes (which are just variables) has a constructor which is used to initialize attributes has other methods (like eat and growl) is a blueprint (template) for creating objects creates a new data type An object: is an instance of a class has a state (variables) that is independent from others 8/10/2019 CSE 1321 Module 6

Summary Variables should (almost) always be declared private Methods intended for class client should be public. Other methods for local use should be private. Controlled access to the variables promotes encapsulation. Methods (setters and getters) provide controlled access to variables 8/10/2019 CSE 1321 Module 6