/
Module 6 Object-Oriented Programming and Module 6 Object-Oriented Programming and

Module 6 Object-Oriented Programming and - PowerPoint Presentation

calandra-battersby
calandra-battersby . @calandra-battersby
Follow
344 views
Uploaded On 2019-12-22

Module 6 Object-Oriented Programming and - PPT Presentation

Module 6 ObjectOriented Programming and class Design 52919 CSE 1321 Module 5 1 Ps Overview ObjectOriented Programming OOP is based on the concept of classes from which objects are created ID: 771213

constructor dog create class dog constructor class create weight rabid public module 1321 cse private false main bmw modelyear

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Module 6 Object-Oriented Programming and" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Module 6 Object-Oriented Programming andclass Design 5/29/19 CSE 1321 Module 5 1 Ps

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 5/29/19 CSE 1321 Module 6 2

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”) 5/29/19 CSE 1321 Module 6 3

The Smallest Class class Dog {} 5/29/19 CSE 1321 Module 6 4

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

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. 5/29/19 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) Behaviorsgrowleat

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

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

Step 3: The Constructor This is a special method Used to give initial values to ALL attributesIs activated when someone creates a new instance of the class Doesn’t have a return typeIn 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.

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 mainIt’s the controlling algorithmSteps: Type in the skeletonCreate a couple of instances of classesStart telling them what to do

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

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

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

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

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

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

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

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

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

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

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

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

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

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 Get to an instances attributesGet to an instances methodsBasically 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 numbersTo FALSE for booleansTo NULL (empty) for objects like strings 5/29/19CSE 1321 Module 431

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

5/29/19 CSE 1321 Module 6 33 Multip l e Constructors (a.k.a. overload i ng) 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

Implementation Overloading the constructor involves using the same method/function name Vary the number of parameters; AND/OR Vary the type of parameters 5/29/19 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 5/29/19 CSE 1321 Module 6 35 Ps

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 5/29/19 CSE 1321 Module 6 36

5/29/19 CSE 1321 Module 6 37 Java - Constructor Example cl a ss B M W _Z 4 { int modelYear ; String licensePlate ; boolean topUp ; BMW_Z4 () { modelYear = 2004; topUp = false ; licensePlate = "DEALER" ; } BMW_Z4 ( int year) { modelYear = year; topUp = false ; licensePlate = "DEALER" ; } }

5/29/19 CSE 1321 Module 6 38 C# - Constructor Example cl a ss B M W _Z 4 { int modelYear ; string licensePlate ; bool topUp ; BMW_Z4 () { modelYear = 2004; topUp = false ; licensePlate = "DEALER" ; } BMW_Z4 ( int year) { modelYear = year; topUp = false ; licensePlate = "DEALER" ; } }

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 5/29/19 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 5/29/19 CSE 1321 Module 440

General RulesVariables/attributes are normally marked privatepublic variables can be tampered with and abused!Methods are most often marked publicHowever, some methods should be marked as private 5/29/19 CSE 1321 Module 4 41

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?) 5/29/19 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 () 5/29/19 CSE 1321 Module 6 43

Wait! Then, how do we change private variables? Let’s review, shall we? 5/29/19 CSE 1321 Module 4 44

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 dataPUBLIC setAge (parameter: newAge) BEGIN IF (newAge < 110) THEN age = newAge ELSE PRINT (“That’s too old!”) ENDIF END 5/29/19 CSE 1321 Module 4 46

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 4/26/2018 CSE 1321 Module 6 5 Ps

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 selfWhat is that? A reference to something inside the class we’re coding inCommonly used to resolve ambiguity of variables: 5/29/19 CSE 1321 Module 4 50

A point of confusion 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 5/29/19 CSE 1321 Module 4 51 Ps

Better 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 5/29/19 CSE 1321 Module 4 52 Ps

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 5/29/19 CSE 1321 Module 4 53

5/29/19 CSE 1321 Module 4 54 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 CONSTRUCTOR END CLASS Ps

The static keyword Only attributes and methods can be marked static Static attributes:Previously, each instance/object had it’s own independent set of variablesWith static all instances share one variableGood for counting the number of instances 5/29/19CSE 1321 Module 4 55

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 5/29/19 CSE 1321 Module 4 56

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 attributesIs called using <Class Name>.<Method Name> 5/29/19 CSE 1321 Module 4 57

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? 5/29/19 CSE 1321 Module 4 58

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 typeAn object: is an instance of a class has a state (variables) that is independent from others 5/29/19 CSE 1321 Module 6 59

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 5/29/19 CSE 1321 Module 6 60