02 OOP Intro.ppt - Uet.vnu.edu.vn

1y ago
11 Views
1 Downloads
4.24 MB
30 Pages
Last View : Today
Last Download : 3m ago
Upload by : Roy Essex
Transcription

OOP Concepts Object-Oriented Programming

Outline What is object-oriented programming? Procedural vs. object-oriented programming OOP concepts Readings: HFJ: Ch.2. GT: Ch.1. OOP Concepts 2

What is OOP? Engine HouseBot * ID - name washUp() cook() cleanUp() * SerialNumber - make 1 uses * forward() turnLeft() turnRight() setPower() OOP Map your problem in the real world Define “things” (objects) which can either do something or have something done to them Create a “type” (class) for these objects so that you don’t have to redo all the work in defining an objects properties and behavior An OO program: “a bunch of objects telling each other what to do by sending messages”. (Smalltalk) OOP Concepts 3

Procedural vs. Object-oriented Procedural program passive data Function 1 Object-oriented program active data Function 2 Global data Function 4 Function 3 OOP Concepts 4

Procedural vs. Object-oriented Example Given a specification: Procedural solution? Object-oriented solution? OOP Concepts 5

Procedural vs. Object-oriented Example Procedural Object-oriented rotate(shapeNum) { //make the shape //.rotate 360o } playSound(shapeNum) { //use shapeNum to look up //.which AIF to play //and play it } OOP Concepts 6

Procedural vs. Object-oriented Example Then comes a change to the specification: Procedural solution? Object-oriented solution? OOP Concepts 7

Procedural vs. Object-oriented Example Procedural playSound() has to change Object-oriented class Amoeba is added playSound(shapeNum) { // if the shape is not amoeba //use shapeNum to look up //.which AIF to play //and play it // else //play amoeba .hif sound } OOP Concepts 8

Procedural vs. Object-oriented Example Then comes another change to the specification: Procedural solution? Object-oriented solution? OOP Concepts 9

Procedural vs. Object-oriented Example Procedural rotate() is modified so is ALL the related code Object-oriented class Amoeba is changed the rest is NOT affected rotate(shapeNum, xPt, yPt ) { // if the shape is not amoeba //calculate center point //based on a rectangle //then rotate // else //use xPt,yPt as //the rotation point offset //and then rotate } OOP Concepts 10

OOP solution OOP Concepts 11

The goals of object-oriented design Robustness: software is capable of handling unexpected inputs that are not explicitly defined for its application. Nuclear plant control software Airplane control software Adaptability: software that can evolve over time in response to changing conditions in its environment. Web browsers and Internet search engines typically involve large programs that are used for many years. Reusability: the same code should be usable as a component of different systems in various applications. Save time and money OOP Concepts 12

Important OO concepts encapsulation Abstraction Objects & Class Object state and behavior abstraction Object identity Messages inheritance Encapsulation Information/implementation hiding Inheritance Polymorphism OOP Concepts "P.I.E“ triangle polymorphism 13

Abstraction Abstraction: to distill a complicated system down to its most fundamental parts and describe these parts in a simple, precise language. naming the parts explaining their functionality Examples: Design of data abstract data types (ADT) OOP Concepts 14

Abstraction Sue’s car: Fuel: 20 liter Speed: 0 km/h License plate: “143 WJT” Automobile: Martin’s car: Fuel: 49.2 liter Speed: 76 km/h License plate: “947 JST” Abstraction fuel speed license plate speed up slow down stop Tom’s car: Fuel: 12 liter Speed: 40 km/h License plate: “241 NGO” OOP Concepts 15

Objects An object has State Behavior Changes over time What the object does in response to messages Identity What makes the object unique OOP Concepts 16

State Given by object’s attributes Dave Brett Age: 32 Age: 35 Height: 6’ 2” Height: 5’ 10” Gary Age: 61 Height: 5’ 8” OOP Concepts 17

Behavior What the object can do responding to a message. Get the mail. Cook dinner. OOP Concepts 18

Identity Something to distinguish between objects. Okay, which one of you wise guys is the real Poppini? I am the great Poppini. De great Poppini at-a your service. I am the great Poppini! I’m the great Poppini! OOP Concepts No, I’m the great Poppini. 19

Classes Define the properties and behavior of objects Can have behavior and properties that are defined in the class but are independent of the individual objects OOP Concepts 20

Classes instantiate Classes are the templates to create objects (instantiate). Each object has the same structure and behaviour as the class from which it was created “Data type – Variable” relation Classes are what we design and code. Class definitions make up programs. Objects are what are created (from a class) at run-time OOP Concepts 21

Objects instantiate State Attributes / Instant variables Variables holding state information of the object Behavior Methods Operations/services performed on the object. OOP Concepts 22

Get the mail. Cook dinner. Messages myCar.accelerate(80); A means for object A to request object B to perform one method of B’s. A message consists of: Handle of the destination object – host (myCar) Name of the method to perform (accelerate) Other necessary information – arguments (80) In effect, a message is a function call with the host object as the implicit argument (method invocation) However, the concept of messages has great significance to OOP: Data become active! OOP Concepts 23

Encapsulation Wait. How’d he do that? Where’s the bunny gone? Two Three. And Abracadabra, the rabbit is gone! OOP Concepts 24

Encapsulation / Information hiding class Car { public void setSpeed( ) { // check validity // set new values . } . private double speed; } Encapsulation: to group related things together Functions/procedures encapsulate instructions Objects encapsulate data and related procedures Information hiding: encapsulate to hide internal implementation details from outsiders Outsiders see only interfaces Programmers have the freedom in implementing the details of a system. Hence, the ability to make changes to an object’s implementation without affecting other parts of the program OOP Concepts 25

Inheritance Mom’s eyes Dad’s smile Mom’s love of ROCK Dad’s sports obsession OOP Concepts 26

what all 4 subclasses have in common Shape Inheritance superclass rotate() playSound() inheritance subclasses Square Circle Triangle Amoeba rotate() { // code to rotate // amoeba only } playSound() { // code to play music // amoeba-specific } “is-a” relations The general classes can be overriding specialized to more specific classes Reuse of interfaces & implementation Mechanism to allow derived classes to possess attributes and operations of base class, as if they were defined at the derived class We can design generic services before specialising them OOP Concepts 27

Polymophism Jump! Polymorphism: "more than one form" Object polymorphism: Different types of objects can respond to the same message. And they can respond differently. Example: the square and the amoeba both can receive message rotate(), they respond by doing different things. OOP Concepts 28

OOP languages Some OOP features can be implemented in C or other procedural programming languages, but not enforced by these languages OOP languages: OOP concepts are embeded in and enforced by the languages. OOP languages vary in degrees of object-oriented Pure: Smalltalk, Eiffel, Ruby, JADE. Original OO plus some procedural features: Python, Java (very high), C (mixed), C#. OO features as extension: VB.NET, Fortran 2003, PHP, Perl. OOP Concepts 29

Example OOP Concepts 30

Some OOP features can be implemented in C or other procedural programming languages, but not enforced by these languages OOP languages: OOP concepts are embeded in and enforced by the languages. OOP Concepts 29 OOP languages vary in degrees of object-oriented Pure: Smalltalk, Eiffel, Ruby, JADE. Original OO plus some procedural features .

Related Documents:

polis NR 11/2011 Revistë shkeNcoRe e Fakultetit të shkeNcave sociale Bordi Editorial Prof. Dr. Romeo Gurakuqi, Universiteti Europian i Tiranës (UET) Fatos Tarifa Ph.D., Universiteti Europian i Tiranës (UET) Prof. Dr. Erleta Mato, Universiteti Europian i Tiranës (UET) Dr. Enis Sulstarova, Universiteti i Tiranës Blendi Kajsiu, Dokt., Essex University, Britani e Madhe

polis NR 14/2015 Revistë shkeNcoRe e Fakultetit të shkeNcave sociale Bordi Editorial Prof. Dr. Ferit Duka, Universiteti Europian i Tiranës (UET) - Drejtor Prof. Dr. Romeo Gurakuqi, Universiteti Europian i Tiranës (UET) - Anëtar Dr. Blendi Kajsiu, University of Antioquia, Colombia - Anëtar Professor John Parrish Sprowl

Jan 11, 2020 · 3rd edition (1999) OOP need to check ask 1725 MechWarrior's Guide to the Clans 3rd edition (1999) OOP need to check 10975 Classic BattleTech Companion 3rd edition (1999) OOP 35008 A Guide to Covert Ops 3rd edition (1999) OOP BattleTech by Catalyst 14

9/5/22 Compsci 201, Fall 2022, OOP 3. Recapping some Java Themes 9/5/22 Compsci 201, Fall 2022, OOP 4. Comments on Java Style Code blocks: Opening {ends first line of if, for, while, or method . Aside: Python uses objects too 9/5/22 Compsci 201, Fall 2022, OOP 15 Split is a methodwe are calling on this String object, not a

OOP IntroductionType & SubtypeInheritanceOverloading and OverridingTemplateDynamic BindingOO-language ImplementationSummary OOP (Object Oriented Programming) So far the languages that we encountered treat data and computation separately. In OOP, the data and computation are combined into an "object". 1/55

OOP expenditure, the proportion of OOP . payments to total expenses per visit ("OOP share") for all facilities was higher for outpatient care (99 percent) than for inpatient care (80 percent). Interestingly, the OOP share for inpatient care was close to the same whether the patient attended a public or a private facility, 79 percent for private

1 1: oop 2 2 Examples 2 2 OOP 2 Intoduction 2 OOP 2 Java 3 C 3 3 Java 3 C 3 3 3 4 5 6 2: 7 Examples 7 7 7 8 3: 9 Examples 9 9 4: 10 Examples 10 10 5: 11

The American Revolution, 1775-1781 Where was the American Revolution fought? Building a Professional Army nWashington’s task was to defendas much territory as possible: Relied on guerrilla tactics & avoided all-out-war with Britain Washington’s Continental Army served as the symbol of the “republican cause” But, colonial militias played a major role in “forcing” neutrals .