Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to OOP with Java, C. Thomas Wu CHAPTER 2 Introduction to OOP

Similar presentations


Presentation on theme: "Intro to OOP with Java, C. Thomas Wu CHAPTER 2 Introduction to OOP"— Presentation transcript:

1 Intro to OOP with Java, C. Thomas Wu CHAPTER 2 Introduction to OOP
OBJECT ORIENTED PROGRAMMING (CSC238) ©The McGraw-Hill Companies, Inc.

2 OBJECTIVES know the elements of an object.
At the end of this topic, you should be able to : know the elements of an object. differentiate between objects and classes. understand the characteristics of OOP

3 INTRODUCTION TO OBJECT
Objects are key to understanding object-oriented technology. In real life, things that you see such as cars, trees, cats, mobile phones and so on are objects. Even, you as a student is an object. Each object has states, behaviours and identity.

4 ELEMENTS OF AN OBJECT Attribute/
state properties used to define characteristics Behaviour means the object can perform actions & can have actions performed on it. Identity means the object can be called & used as a single unit.

5 ATTRIBUTE We used it to save the information of object
Every object was different of attribute such as type, quality. Example: color: red, blue category: sport, turbo, door type: proton, perodua In class car

6 STATE Attribute also can describe about state of object Example:
Current gear (1/2/3/4/5) Engine (on/of) This attribute will declare as variable

7 ELEMENTS OF AN OBJECT

8 ELEMENTS OF AN OBJECT

9 BEHAVIOR It can tell what the process can do/will be Example:
Start engine Speeding Change gear Stop

10 EXAMPLE 1: An air-conditioner at MK07 is an object -state:  turn on
 current temperature is at 20 degree Celcius. -behaviour : change the temperature level

11 EXAMPLE 2: -state:  dark brown colours on its face, ears and feet
-behaviours :  playing, fighting, hunting A Siamese cat

12 EXAMPLE 3: How about the following cats?
They are 3 different cats which two of them are of the same type. What makes them different?  their states, behaviours and identities

13 EXAMPLE 4: Different type of cats share the same behaviours.

14 EXAMPLE 5: How about these cars?
Try to identify their common attributes and behaviours. car

15 WHAT CAN BE CONCLUDED FROM EX 3 - 5 ?
A group of animals or things that are similar in some way. They share the same attributes and behaviours. This group of objects represents a class.

16 BASIC TERMINOLOGY Object Method Attribute Class
usually a person, place or thing (a noun) Method an action performed by an object (a verb) Attribute description of objects in a class Class a category of similar objects (such as automobiles) does not hold any values of the object’s attributes

17 OBJECTS state - descriptive characteristics
An object has: state - descriptive characteristics behaviors - what it can do (or what can be done to it) The state of a bank account includes its account number and its current balance The behaviors associated with a bank account include the ability to make deposits and withdrawals Note that the behavior of an object might change its state

18 CLASSES An object is defined by a class
A class is the blueprint of an object The class uses methods to define the behaviors of the object The class that contains the main method of a Java program represents the entire program A class represents a concept, and an object represents the embodiment of that concept Multiple objects can be created from the same class

19 CLASS VS OBJECT Class Object a thing, both tangible and intangible.
a kind of template. represents the common structure & behaviour shared by the same type. A collection of objects of similar type. must be defined before creating an instance of the class. Class a thing, both tangible and intangible. is comprised of data & operations that manipulate these data. is called an instance of a class. Object

20 CLASS VS OBJECT

21 EXAMPLE 6: Variables Methods Student name id setName() setId() class
Represents the data/ attributes variables. a set of properties Methods Represents the behaviours. A sequence of instructions that a class or an object follows to perform a task. : Student name=“Sarah” id=“1234” object / instance of the class

22 MESSAGES An object can’t exist on its own.
An object communicates with other objects. Therefore, a message is used to instruct a class or an object to perform a task. An object or a class only responds to messages that it can understand. Messages must match the method that it possess. A list of messages is called an interface.

23 WHY OOP? Save development time (and cost) by reusing code
once an object class is created it can be used in other applications Easier debugging classes can be tested independently reused objects have already been tested

24 CHARACTERISTICS/CONCEPTS OF OOP
Abstraction Encapsulation Inheritance Polymorphism

25 CHARACTERISTICS OF OOP
Abstraction - designing classes The creation of a software module contain relevant characteristics of the object to be represented Encapsulation All data members (fields) of a class are declared private. Some method may be private too Inheritance - the use of extends Ability to create new classes from existing classes Polymorphism - the use of abstract classes and virtual methods One single statement is used as a call to various methods The exact method to be called is only know during runtime

26 ABSTRACTION Focus only on the important facts about the problem at hand to design, produce, and describe so that it can be easily used without knowing the details of how it works. Analogy: When you drive a car, you don’t have to know how the gasoline and air are mixed and ignited. Instead you only have to know how to use the controls. Draw map

27 ABSTRACTION

28 ABSTRACTION The act of representing essential features without including the background details or explanations. Use to manage complexity Abstraction can be managed through the use of hierarchical classifications.

29 ENCAPSULATION The mechanism that binds together code and the data it manipulates and keeps both safe from the outside interference and misuse. Access to the code & data inside the wrapper is tightly controlled through a well-defined interface.

30 EXAMPLE 7: Variables: name student id address course Methods:
changeAddress(String) changeCourse(String) Student INTERFACE changeAddress(String) changeCourse(String)

31 Also known as data hiding
ENCAPSULATION Also known as data hiding Only object’s methods can modify information in the object. Analogy: ATM machine can only update accounts of one person or object only.

32 ABSTRACTION VS ENCAPSULATION

33 INHERITANCE The process by which one object acquires the properties of another object. An object need only to define all those qualities that make it unique within its class. It inherits its general attributes from its parent. A subclass has at least one attribute/method that differs from its superclass Other names : base class-derived class, parent class- child class

34 INHERITANCE Inheritance is a way of organizing classes
Term comes from inheritance of traits like eye color, hair color, and so on. Classes with properties in common can be grouped so that their common properties are only defined once. Superclass – inherit its attributes & methods to the subclass(es). Subclass – can inherit all its superclass attributes & methods besides having its own unique attributes & methods.

35 HIERARCHY OF INHERITANCE
Superclass Vehicle Subclasses Automobile Motorcycle Bus Sedan Sports Car Luxury Bus School Bus What properties does each vehicle inherit from the types of vehicles above it in the diagram?

36 EXAMPLE 8 : MOBILE PHONE MobilePhone model manufacturer price …
CameraPhone model manufacturer price pixel MobilePhone model manufacturer price PdaPhone model manufacturer price memoryCap subclass superclass subclass

37 POLYMORPHISM From the Greek, meaning “many forms”.
A feature that allows one interface to be used for a general class of actions. “one interface, multiple methods” Can be applied in the overloaded methods (a few methods that have the same name but with different parameters).

38 POLYMORPHISM The same word or phrase can mean different things in different contexts Analogy: In English, bank can mean side of a river or a place to put money move -

39 EXAMPLE 9 Class : Rectangle Variables : length, width, height
Methods : …. displayShape(char simbol) displayShape(int a) This class has 2 methods with the same name but with different type of parameters

40 OBJECT-ORIENTED PROGRAMMING LANGUAGES
Pure OO Languages Smalltalk, Eiffel, Actor, Java Hybrid OO Languages C++, Objective-C, Object-Pascal

41 CONCLUSION Elements of an object are attribute, behaviour and identity. A class is a collection of objects of similar type. An object is comprised of data and operations that manipulate these data. Characteristics of OOP is abstraction, encapsulation, inheritance and polymorphism.

42 REVIEW What are the four basic principles of object orientation? Provide a brief description of each. What is an Object and what is a Class? What is the difference between them? What is an Attribute? What is an Operation? What is inheritance? What is polymorphism? Describe the strengths of object orientation.


Download ppt "Intro to OOP with Java, C. Thomas Wu CHAPTER 2 Introduction to OOP"

Similar presentations


Ads by Google