SEEM4570 System Design and Implementation. Lecture 11 From Design to Implementation

Similar documents
Object Orientated Analysis and Design. Benjamin Kenwright

CLASSES AND OBJECTS IN JAVA

SEEM4570 System Design and Implementation Lecture 09 Object Oriented Programming

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

SEEM4570 System Design and Implementation. Lecture 10 UML

Chapter 7. Inheritance

Inheritance and Polymorphism

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Chapter 5 Object-Oriented Programming

What are the characteristics of Object Oriented programming language?

9/21/2010. Based on Chapter 2 in Advanced Programming Using Visual Basic.NET by Bradley and Millspaugh

Programming II (CS300)

SEEM4570 System Design and Implementation Lecture 11 UML

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.


Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

PROGRAMMING LANGUAGE 2

Object-Oriented Design

Object-Oriented Modeling Using UML. CS151 Chris Pollett Aug. 29, 2005.

Programming Exercise 14: Inheritance and Polymorphism

Exam Duration: 2hrs and 30min Software Design

Data Structures (INE2011)

Practice for Chapter 11

Object oriented programming Concepts

Review and Recursion

OVERRIDING. 7/11/2015 Budditha Hettige 82

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Inheritance, and Polymorphism.

CS105 C++ Lecture 7. More on Classes, Inheritance

ECE 122. Engineering Problem Solving with Java

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Overview. OOP: model, map, reuse, extend. Examples of objects. Introduction to Object Oriented Design

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

ROBOT OOP. Learning the basics of Object Oriented Programming using robots from popular culture

7. Implementation Phase. 7.1 Architecture Diagrams 7.2 OO Languages: Java 7.3 Constraint Languages: OCL

Object-Oriented Design

Lecture Notes on Programming Languages

Advanced Programming Using Visual Basic 2008

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

CS313D: ADVANCED PROGRAMMING LANGUAGE

(11-1) OOP: Inheritance in C++ D & D Chapter 11. Instructor - Andrew S. O Fallon CptS 122 (October 29, 2018) Washington State University

CGS 2405 Advanced Programming with C++ Course Justification

Data Structures (list, dictionary, tuples, sets, strings)

C++ Important Questions with Answers

WEB APPLICATION ENGINEERING II

Object Fundamentals. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 2 08/30/2007

Object-Oriented Analysis and Design Using UML

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Introduction to OOP. Procedural Programming sequence of statements to solve a problem.

What is Inheritance?

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Object Fundamentals, Part One. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 2 08/27/2009

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design

Advanced UML Class Models

What does it mean by information hiding? What are the advantages of it? {5 Marks}

CS304 Object Oriented Programming Final Term

Programming, numerics and optimization

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Programming II (CS300)

Microsoft Visual Basic 2005: Reloaded

INHERITANCE: EXTENDING CLASSES

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Object-Oriented Design

drawobject Circle draw

Chapter 02 Building Multitier Programs with Classes

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Chapter 5: Classes and Objects in Depth. Information Hiding

A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

G Programming Languages - Fall 2012

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Classes and Inheritance Extending Classes, Chapter 5.2

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Darrell Bethea June 6, 2011

Class Diagram. Classes consist of. Note that class diagrams contain only classes, not objects.

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

JAVA MOCK TEST JAVA MOCK TEST II

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

CS111: PROGRAMMING LANGUAGE II

Super-Classes and sub-classes

Chapter 1: Object-Oriented Programming Using C++

Algorithms and Programming

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

ITI Introduction to Computing II

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

C++ Inheritance and Encapsulation

Class Diagram. Classes consist of. Note that class diagrams contain only classes, not objects.

OBJECT ORIENTED PROGRAMMING

C++ (Non for C Programmer) (BT307) 40 Hours

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

COP 3330 Final Exam Review

Introduction to Object- Oriented Programming

Final Examination Semester 3 / Year 2010

Transcription:

SEEM4570 System Design and Implementation Lecture 11 From Design to Implementation

Introduction We learned programming and we learned UML, but separately. Now, the question is how can we translate a design specification from UML to Implementation (i.e. programming)? In this lecture, we will focus on class diagram and use PHP for demonstration. PHP is chosen because its syntax is easy to understand and initiative. 2017. Gabriel Fung. 2

Example 1 class { private $name; public function construct($name){ $this->name = $name public function getname(){ return $this->name; - name string + (name string) + getname() string $person = new ( John ); echo $person->getname(); // output John echo $person->name; // Error! name is private! 2017. Gabriel Fung. 3

Example 2 class { public $name; public function construct($name){ $this->name = $name public function getname(){ return $this->name; + name string + (name string) + getname() string $person = new ( John ); echo $person->getname(); // output John echo $person->name; // output John 2017. Gabriel Fung. 4

Example 3 class { protected $name; public function construct($name){ $this->name = $name public function getname(){ return $this->name; # name string + (name string) + getname() string $person = new ( John ); echo $person->getname(); // output John echo $person->name; // Error! name is protected! 2017. Gabriel Fung. 5

Note 1 By setting the variables into different kinds of visibilities (i.e. private, protected and public), we can control how the other programs can access the variables, and avoid other programs accidentally changed the variable. In Example 1, this will return an error $person->name = Mary ; So there is no valid way to change the name of the person. However, in Example 2, this is valid $person->name = Mary ; // the name is changed to Mary now For protected, we will explain it very soon. 2017. Gabriel Fung. 6

Example 4 class Staff extends { private $salary; public function construct(salary, name){ $this->salary= $salary; $this->name = $name; public function getsalary(){ return $this->salary; $staff = new Staff( John ); echo $staff->getname(); // output John Staff - salary int echo $staff->name; // Error! name is private! # name string + (name string) + getname() string + Staff(salary int, name string) + getsalay() int 2017. Gabriel Fung. 7

Example 5 class Staff extends { private $salary; public function construct(salary, name){ $this->salary= $salary; $this->name = $name; // invalid! public function getsalary(){ return $this->salary; Staff - salary int - name string + (name string) + getname() string + Staff(salary int, name string) + getsalay() int 2017. Gabriel Fung. 8

Note 2 Did you know why it is valid to use $this->name (line 6) in Example 4? It is because name is a protected variable in the parent class, so that any classes that extend it can access this protected variable directly. To make Example 5 valid, we need to change the code a little bit. See the next eample. 2017. Gabriel Fung. 9

Example 6 class Staff extends { private $salary; public function construct(salary, name){ $this->salary = $salary; parent construct(name); // valid public function getsalary(){ return $this->salary; Staff - salary int - name string + (name string) + getname() string + Staff(salary int, name string) + getsalay() int 2017. Gabriel Fung. 10

Example 7 interface { public function getname(); class Staff implements { private $name; private $salary; public function construct(salary, name){ $this->salary = $salary; $this->name = $name; public getname(){ return $this->name; Staff - name string - salary int <<interface>> + getname() string + Staff(salary int, name string) + getsalay() int 2017. Gabriel Fung. 11

Example 8 interface { public function getname(); // The class below is invalid! It did not // have the method getname() class Staff implements { private $name; private $salary; public function construct(salary, name){ $this->salary = $salary; $this->name = $name; Staff - name string - salary int <<interface>> + getname() string + Staff(salary int, name string) + getsalay() int 2017. Gabriel Fung. 12

Example 9 // The class below is invalid! // An interface cannot contain // implementation detail interface { private $name; public function getname(){ return $this->name; Staff - name string - salary int <<interface>> + getname() string + Staff(salary int, name string) + getsalay() int 2017. Gabriel Fung. 13

Note 3 In interface, we should not have any variable and should not have any constructor. We should not have any implementation detail as well. If you need to contain variables and implementation detail, you should write abstract class. Why interface is useful? It can make sure all classes that implemented a particular interface must contain all the methods specified in the implemented interface. 2017. Gabriel Fung. 14

Example 10 abstract class { private $name; public function getname(){ return $this->name; class Staff extends { Note Italic! + getname() string Staff - name string - salary int + Staff(salary int, name string) + getsalay() int 2017. Gabriel Fung. 15

Note 4 Why interface and abstract class are useful? It can make sure all classes that implemented a particular interface must contain all the methods specified in the implemented interface. 2017. Gabriel Fung. 16

Example 11 class { class Building{ private $securityguard; public function construct($guard){ $this->securityguard = $guard; Building - securityguard $person = new ( John ); $building = new Building($person); 2017. Gabriel Fung. 17

Example 12 class Room{ class Building{ private $room; public function construct($room){ $this->room = new Room(); Room Building - room Room 2017. Gabriel Fung. 18

Note 5 Can you identify the major difference between aggregation and composition? In aggregation, when an object that contains other objects is destroyed, those objects that are used inside will not destroy. In composition, when an object is destroyed, all associated objects will be destroyed as well. 2017. Gabriel Fung. 19

Example 13 // This is correct class {... class Printer{... public function print(person){... Printer + print(person ) 2017. Gabriel Fung. 20

Example 14 // This is incorrect class {... public function print(printer){... class Printer{... public function print(person){... + print(printer Printer) Printer + print(person ) 2017. Gabriel Fung. 21

Example 15 // This is also incorrect class {... public function x(){ $printer = new Printer(); class Printer{... public function print(person){... + x() Printer + print(person ) 2017. Gabriel Fung. 22

Note 6 When the relationship is uni-directional from A to B (i.e. A > B), you should never refer to B from A. 2017. Gabriel Fung. 23

Example 16 class {... public function getname(){ return $this->name; Professor class Professor{... + getname() string // override the previously defined method public function getname(){ return Prof.. $this->name; + getname() string 2017. Gabriel Fung. 24

Visibility and Encapsulation In UML class diagram, the symbol #, + and are known as visibility. In OOP, it is usually called Encapsulation. The ability of an object to hide its data and methods from the rest of the world The internal representation of an object is generally hidden from view outside of the object's definition. One of the fundamental principles of OOP. 2017. Gabriel Fung. 25

Realization and Data Abstraction Example 7 and Example 10 are two examples of realization. In OOP, we usually call this as Data Abstraction 2017. Gabriel Fung. 26

Generalization and Inheritance Example 6 and Example 10 are two examples of generalization. You can use one class as the basic for another class. Multiple classes may share some of the same characteristics, and have things in common with one another, but also may have certain properties that make them different. In OOP, this is usually known as Inheritance. 2017. Gabriel Fung. 27

Polymorphism In Example 16, we override a method that is defined in the parent class. In OOP, this is known as Polymorphism. Polymorphism is the ability to create a variable, a function, or an object that has more than one form. The most common cases are Method Overriding Re-define the method in a subclass (Example 16) Method Overloading (Not in PHP) Define some methods with the same name but different number of arguments 2017. Gabriel Fung. 28

More About OOP Encapsulation, Data Abstraction, Inheritance and Polymorphism are four major properties of OOP. Note that not all OOP must necessarily contains all these properties. 2017. Gabriel Fung. 29