Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming and class Design

Similar presentations


Presentation on theme: "Object-Oriented Programming and class Design"— Presentation transcript:

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

2 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

3 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

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

5 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

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

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

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

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

10 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

11 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.

12 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

13 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

14 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

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

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

17 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

18 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

19 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

20 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

21 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”

22 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”

23 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”

24 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”

25 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”

26 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”

27 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

28 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

29 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>

30 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

31 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

32 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

33 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

34 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

35 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

36 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

37 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

38 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

39 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

40 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

41 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

42 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

43 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

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

45 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

46 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

47 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

48 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

49 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

50 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

51 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

52 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

53 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

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 CLASS Ps 8/10/2019 CSE 1321 Module 4

55 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

56 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

57 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

58 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

59 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

60 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


Download ppt "Object-Oriented Programming and class Design"

Similar presentations


Ads by Google