Download presentation
Presentation is loading. Please wait.
Published byἈγλαΐη Αναγνωστάκης Modified over 5 years ago
1
Object-Oriented Design AND CLASS PROPERTIES
Ps Module 6 – Part II Object-Oriented Design AND CLASS PROPERTIES 6/7/2019 CSE 1321 Module 6
2
Topics Default Constructors Multiple Constructors (overloading)
Constructor “chaining” Control of Properties “Getters” or “Accessors” “Setters” or “Modifiers” 6/7/2019 CSE 1321 Module 6
3
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 6/7/2019 CSE 1321 Module 4
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
5
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 6/7/2019 CSE 1321 Module 6 5
6
Implementation Overloading the constructor involves using the same method/function name Vary the number of parameters; AND/OR Vary the type of parameters 6/7/2019 CSE 1321 Module 6 6
7
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 6/7/2019 CSE 1321 Module 6 7
8
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 6/7/2019 CSE 1321 Module 6 8
9
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; } 6/7/2019 CSE 1321 Module 6 9
10
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; } 6/7/2019 CSE 1321 Module 6 10
11
Python – Overload Constructor Example
Python doesn’t support overloading, but there are ways to work around this. We can still write only one constructor, but create it so that it accept multiple arguments. Then, in the constructor, we can look for certain values and assign them accordingly, as shown below: #construct a BMW object def __init__(self, modelYear = 2018, TopUp = True, licensePlate = "DEALER"): self.modelYear = modelYear self.topUp = TopUp self.licensePlate = licensePlate 6/7/2019 CSE 1321 Module 6 11
12
Visibility of Class Content
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 6/7/2019 CSE 1321 Module 6 12
13
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 6/7/2019 CSE 1321 Module 4
14
Implications of public and private (this is a bit hard to understand…)
6/7/2019 CSE 1321 Module 6 14
15
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 6/7/2019 CSE 1321 Module 4
16
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?) 6/7/2019 CSE 1321 Module 6 16
17
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 = // Illegal because ModelYear is private myCar.Drive() // Legal. Drive() is public myCar.OpenTop() // Legal. OpenTop() is public // Illegal for multiple reasons… myCar.EmitPollutantsWhenNoOneIsLooking() 6/7/2019 CSE 1321 Module 6
18
Wait! How do we change private variables?
Let’s review, shall we? 6/7/2019 CSE 1321 Module 4
19
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
20
Main idea Use methods to access and change the variables/attributes. Instead of direct access 6/7/2019 CSE 1321 Module 6 20
21
Accessors/Modifiers public methods that enable change to 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 6/7/2019 CSE 1321 Module 4
22
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
23
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; } } 6 4/26/2018 CSE 1321 Module 6
24
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; }} } 7 4/26/2018 CSE 1321 Module 6
25
Class BMW_Z4 class BMW: def setModelYear(self, modelYear):
self.modelYear = modelYear def getModelYear(self): return self.modelYear def setTopUp(self, topUp): self.topUp = topUp def getTopUp(self): return self.topUp def setLicensePlate(self, licensePlate): self.licensePlate = licensePlate def getLicensePlate(self): return self.licensePlate 8 4/26/2018 CSE 1321 Module 6
26
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: 6/7/2019 CSE 1321 Module 4
27
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 Ps 6/7/2019 CSE 1321 Module 4
28
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 Ps 6/7/2019 CSE 1321 Module 4
29
Last thing: 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 6/7/2019 CSE 1321 Module 4
30
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 6/7/2019 CSE 1321 Module 4
31
Summary A class is made of variables and methods.
Variables represent the state of the class objects; Variables should (almost) always be declared private Method provide controlled access to variables Methods intended for class client should be public. Other methods for local use should be private. Controlled access to the variables promotes encapsulation. 6/7/2019 CSE 1321 Module 6
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.