27
Object Oriented Programming Principles Object Oriented Programming Principles Lecturer: Lecturer: Kalamullah Kalamullah Ramli Ramli Electrical Engineering Dept. Electrical Engineering Dept. University of Indonesia University of Indonesia Session Session - - 3 3

Chapter3 bag1

Embed Size (px)

Citation preview

Page 1: Chapter3 bag1

Object Oriented Programming PrinciplesObject Oriented Programming Principles

Lecturer: Lecturer:

KalamullahKalamullah RamliRamli

Electrical Engineering Dept.Electrical Engineering Dept.

University of IndonesiaUniversity of Indonesia

SessionSession--33

Page 2: Chapter3 bag1

Slide Slide -- 22OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

ObjectObject--Oriented Programming:Oriented Programming:

A New Programming ParadigmA New Programming Paradigm……

�� What is What is Matlab'sMatlab's programming paradigm?programming paradigm?

�� Matlab is Matlab is proceduralprocedural: the focus is on the : the focus is on the

functionsfunctions (procedures) (procedures)

�� What is the programming paradigm in OOP?What is the programming paradigm in OOP?

�� As the name suggests, in OOP the focus is onAs the name suggests, in OOP the focus is on

�� OBJECTSOBJECTS

�� This doesn't say much unless we understand This doesn't say much unless we understand

what we mean with the term what we mean with the term ObjectObject…… So So

let's move on.let's move on.

Page 3: Chapter3 bag1

Slide Slide -- 33OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Objects Combine Properties & Behavior Objects Combine Properties & Behavior [1/2][1/2]

�� Remember Remember following Structures:following Structures:

� person.firstname = 'John';

� person.lastname = 'Leonard';

� person.address1 = '803 Shallowford

Lane';

� person.city = 'Peachtree City';

� person.state = 'GA';

� person.zip = '30269-4289';

�� A structure contains data in an organized fashion.A structure contains data in an organized fashion.

Page 4: Chapter3 bag1

Slide Slide -- 44OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Objects Combine Properties & Behavior Objects Combine Properties & Behavior [2/2][2/2]

�� If we add functions or If we add functions or methodsmethods to a structure, it to a structure, it

becomes an object:becomes an object:� person.print_address();

� person.set_lastName('Paredis');

Page 5: Chapter3 bag1

Slide Slide -- 55OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

More Examples of Objects More Examples of Objects [1/2][1/2]

�� Car:Car:

� Properties: color, speed, fuel consumption, HP…

� Behaviors: start, stop, turning, …

�� Triangle:Triangle:

� Properties: area, perimeter, linestyle, location,…

� Behaviors: translate, rotate, shrink, flip,…

�� Date:Date:

� Properties: year, month, day, …

� Behaviors: setDate, getMonth, isGreater, …

Page 6: Chapter3 bag1

Slide Slide -- 66OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

More Examples of Objects More Examples of Objects [1/2][1/2]

�� Properties: Properties: information about the objectinformation about the object

�� Behaviors: Behaviors: methods to set, get and process methods to set, get and process

propertiesproperties

�� Combining properties and behaviors in objects is Combining properties and behaviors in objects is

calledcalled

�� EncapsulationEncapsulation

Page 7: Chapter3 bag1

Slide Slide -- 77OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Objects Combine Properties and Behavior:Objects Combine Properties and Behavior:

So What? Why should we care? So What? Why should we care? [1/2][1/2]

�� Black Box PhilosophyBlack Box Philosophy::

�� Objects perform computation Objects perform computation by making requests of each by making requests of each other through the passing of other through the passing of messagesmessages

�� The only way to interact with The only way to interact with an object is through its an object is through its methods!methods!

http://java.sun.com/docs/books/tutorial/java/concepts/object.html

http://catalog.com/softinfo/objects.html

Messages

Code

Data

Page 8: Chapter3 bag1

Slide Slide -- 88OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Objects Combine Properties and Behavior:Objects Combine Properties and Behavior:

So What? Why should we care? So What? Why should we care? [2/2][2/2]

�� This is called This is called Information HidingInformation Hiding

�� (the data is hidden from the user)(the data is hidden from the user)

�� The collection of all methods is called The collection of all methods is called thethe interface interface of the objectof the object

http://java.sun.com/docs/books/tutorial/java/concepts/object.html

http://catalog.com/softinfo/objects.html

Page 9: Chapter3 bag1

Slide Slide -- 99OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Encapsulation and Data Hiding Encapsulation and Data Hiding [1/2][1/2]

�� Properties (Variables)Properties (Variables)

� Speed

� RPM

� Current Gear

�� Methods (Functions)Methods (Functions)

� Braking

� Acceleration

� Turn

� Changing Gears

Page 10: Chapter3 bag1

Slide Slide -- 1010OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Encapsulation and Data Hiding Encapsulation and Data Hiding [2/2][2/2]

�� These methods are independent of how the These methods are independent of how the

bicycle has been built (hides the bicycle has been built (hides the

implementation)implementation)

�� You can control access to members of an You can control access to members of an

objectobject

Page 11: Chapter3 bag1

Slide Slide -- 1111OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Working with Objects: Messages Working with Objects: Messages [1/2][1/2]

�� Objects perform computation by making Objects perform computation by making

requests of each other through the passing of requests of each other through the passing of

messages messages

�� Parts of a message Parts of a message ––

� The object to which the message is addressed

� The name of the method to perform

� Any parameters needed by the method\

Page 12: Chapter3 bag1

Slide Slide -- 1212OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Working with Objects: Messages Working with Objects: Messages [2/2][2/2]

�� Different objects may respond differently to an Different objects may respond differently to an identical message:identical message:� bicycle.changeGears(lowerGear)

� car.changeGears(lowerGear)

�� The same name and the same argument, but a The same name and the same argument, but a different method = different method = POLYMORPHISMPOLYMORPHISM

�� A method is defined only in the scope of a A method is defined only in the scope of a particular type of object, called classparticular type of object, called class

�� Polymorphism is also called: Polymorphism is also called: function overloadingfunction overloading

Page 13: Chapter3 bag1

Slide Slide -- 1313OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Class and Object Class and Object [1/2][1/2]

�� Now that we know what an object is all about, Now that we know what an object is all about,

let's look at how we can let's look at how we can organizeorganize these objectsthese objects

�� Class =Class = A blueprint, or prototype, that defines A blueprint, or prototype, that defines

the variables and the methods common to all the variables and the methods common to all

objects of a certain kindobjects of a certain kind

Objects are individual

instances of a class

Page 14: Chapter3 bag1

Slide Slide -- 1414OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Class and Object Class and Object [2/2][2/2]

��House Plans:House Plans:

��the architectural drawings that the architectural drawings that

describe how a house is to be describe how a house is to be

constructedconstructed

��A House:A House:

��The house built from the plans The house built from the plans

is an instance of the House Class. is an instance of the House Class.

The process of building the The process of building the

house is called house is called InstantiationInstantiation

Page 15: Chapter3 bag1

Slide Slide -- 1515OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Organizing Objects: Inheritance Organizing Objects: Inheritance [1/2][1/2]

Super class

(parent)

Sub class

(child)

Children inherit

• properties• behaviors

Page 16: Chapter3 bag1

Slide Slide -- 1616OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Organizing Objects: Inheritance Organizing Objects: Inheritance [2/2][2/2]

�� Sub classSub class (Child)(Child)

• A class that is derived from a particular class,

perhaps with one or more classes in between

�� Super classSuper class (Parent)(Parent)

• A class from which a particular class is derived,

perhaps with one or more classes in between

�� Inheritance promotesInheritance promotes

� Reuse of code

� Better Management of code

Page 17: Chapter3 bag1

Slide Slide -- 1717OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Abstraction in OOP Abstraction in OOP [1/2][1/2]

�� Abstractions Abstractions reveal causes and effects, expose reveal causes and effects, expose

patterns and frameworks and separate what's patterns and frameworks and separate what's

important from what's notimportant from what's not

�� Abstraction in programmingAbstraction in programming helps you make your helps you make your

ideas concrete in your code without obscuring the ideas concrete in your code without obscuring the

architecture with detailsarchitecture with details

�� In procedural languages:In procedural languages:

� structures and functions are abstractions

Page 18: Chapter3 bag1

Slide Slide -- 1818OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Abstraction in OOP Abstraction in OOP [2/2][2/2]

OOP takes abstraction one step further through:OOP takes abstraction one step further through:

�� encapsulationencapsulation

�� data hidingdata hiding

�� polymorphismpolymorphism

�� InheritanceInheritance

Page 19: Chapter3 bag1

Slide Slide -- 1919OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

ObjectObject--Oriented Programming Oriented Programming [1/2][1/2]

�� A Quick Review:A Quick Review:

�� ““OOP is more a way of thinking than just a programming OOP is more a way of thinking than just a programming

techniquetechnique”” –– ““conceptual tool that you use in thinking how to conceptual tool that you use in thinking how to

solve a problemsolve a problem””

�� Computer Computer objectsobjects form the basisform the basis

�� Objects treated as real life objectsObjects treated as real life objects

� Identities

� Properties

� Behaviors

�� Data and functionality Data and functionality encapsulatedencapsulated in an objectin an object

�� Data Data hiddenhidden behind methodsbehind methods

Page 20: Chapter3 bag1

Slide Slide -- 2020OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

ObjectObject--Oriented Programming Oriented Programming [2/2][2/2]

�� Objects organized in class hierarchies: inheritanceObjects organized in class hierarchies: inheritance

�� Objects interact through messages Objects interact through messages –– methodsmethods

�� Polymorphism: the same message has a different meaning for Polymorphism: the same message has a different meaning for

different objectsdifferent objects

Page 21: Chapter3 bag1

Slide Slide -- 2121OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Benefits of Object Oriented Benefits of Object Oriented

ProgrammingProgramming

�� Analysis and Design made easierAnalysis and Design made easier

�� Understanding of code made easierUnderstanding of code made easier

�� Code ReuseCode Reuse

�� Ease of maintenance and enhancement Ease of maintenance and enhancement

�� Simplifies collaborationSimplifies collaboration

�� Fewer and shorter design iterationsFewer and shorter design iterations

Page 22: Chapter3 bag1

Slide Slide -- 2222OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Examples of Programming LanguagesExamples of Programming Languages

�� Procedural languagesProcedural languages

� C

� FORTRAN

� BASIC

� Pascal

�� Object Oriented languagesObject Oriented languages

� C++

� Smalltalk

� Java

Page 23: Chapter3 bag1

Slide Slide -- 2323OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Assignment#2a: OOP Assignment#2a: OOP vsvs Procedural ProgrammingProcedural Programming

��ObjectObject--OrientedOriented

��

��

��

��

��ProceduralProcedural

��

��

��

��

Due date: Due date: Friday, February 27Friday, February 27thth, 2004, 2004

Page 24: Chapter3 bag1

Slide Slide -- 2424OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

Assignment#2b: Object Oriented PhilosophyAssignment#2b: Object Oriented Philosophy

�� ““Simulate the functionality of your computer using the Simulate the functionality of your computer using the

object oriented object oriented philosophyphilosophy””

�� Draw a diagram of the components present in the Draw a diagram of the components present in the

computer. What is the flow of information between computer. What is the flow of information between

these components? Leverage from previous lectures.these components? Leverage from previous lectures.

�� Identify the classes and objects that you would need to Identify the classes and objects that you would need to

simulate these components. How do these classes simulate these components. How do these classes

interact?interact?

�� Identify the variables and methods that you would Identify the variables and methods that you would

assign to each class.assign to each class.

Due Date: Due Date: Friday, March 5Friday, March 5thth, 2004, 2004

Page 25: Chapter3 bag1

Slide Slide -- 2525OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

References and Additional ReadingReferences and Additional Reading

�� ““Object Oriented Programming, Object Oriented Programming, ””http://http://www.toodarkpark.net/computers/objc/oop.hwww.toodarkpark.net/computers/objc/oop.htmltml

�� ““Core Java, Core Java, ”” Cay S. Cay S. HorstmannHorstmann and Gary Cornell, and Gary Cornell, Sun MicrosystemsSun Microsystems

�� ““ Thinking in C++Thinking in C++, , ”” Bruce Bruce EckelEckel, , http://www.mindview.net/Books/TICPP/ThinkingInChttp://www.mindview.net/Books/TICPP/ThinkingInCPP2e.htmlPP2e.html

�� ““ Introduction to Object Oriented Programming, Introduction to Object Oriented Programming, ””Timothy Budd, Timothy Budd, ftp://ftp.cs.orst.edu/pub/budd/oopintro/3rdEditionftp://ftp.cs.orst.edu/pub/budd/oopintro/3rdEdition/info.html/info.html

Page 26: Chapter3 bag1

Slide Slide -- 2626OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

ClosureClosure

�� LearningLearning

�� Object oriented programming is more a philosophy than mere Object oriented programming is more a philosophy than mere

technique to develop programstechnique to develop programs……

�� This philosophy provides modularity to programsThis philosophy provides modularity to programs

�� Modularity provides openness to changeModularity provides openness to change……

�� Enables developing complex systemsEnables developing complex systems

�� Food for ThoughtFood for Thought

�� How will object oriented philosophy help in the future of softwaHow will object oriented philosophy help in the future of software re

development?development?

�� How can the object oriented philosophy based on modularity be How can the object oriented philosophy based on modularity be

used in your field of specialization?used in your field of specialization?

�� What changes are required to adapt this philosophy to your fieldWhat changes are required to adapt this philosophy to your field??

Page 27: Chapter3 bag1

Slide Slide -- 2727OOP Lecture 2004 Dr. OOP Lecture 2004 Dr. ––IngIng. Ir. . Ir. KalamullahKalamullah RamliRamli, , M.EngM.Eng

The EndThe End

QUESTIONS & COMMENTS ?QUESTIONS & COMMENTS ?