introduction to oop chapter 1 csc238 1. objectives 2 at the end of this topic, you should be able to...
TRANSCRIPT
INTRODUCTION TO OOP
Chapter 1
CSC238 1
Objectives2
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.
NH2008
Object3
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.
NH2008
NH2008
4
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.
Elements of an Object
Example 1:
State :
Turn on
Current temperature is at 20 degree celcius
Behaviours :
Change the temperature level
5
NH2008
An air-conditioner at MK02
Example 2:
State :
Dark brown colours on its face, ears and feet
Behaviours :
Playing, fighting, hunting
6
NH2008
A Siamese Cat
Example 3:
They are 3 different cats which two of them are of the same type.
What makes them different?
their states , behaviours and identity.
7
NH2008
How about the following cats?
Example 4:8
NH2008
Different types of cats Share the same behaviours
Example 5:
Try to identify their common attributes and behaviours .
9
NH2008
How about these cars?
Lesson Learned…10
What can you tell from example 3 to 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.
NH2008
● 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.
● a thing, both tangible and intangible.
● is comprised of data & operations that manipulate these data.
● is called an instance of a class.
11
NH2008
Class Object
Class ? Object ?
Example 6:12
● Represents the data/ attributes variables.
● a set of properties
Methods
Variables
• Represents the behaviours.• A sequence of instructions that a class
or an object follows to perform a task.
Student
nameid
setName()setId()
: Student
name=“Sarah”id=“1234”
class
object / instance of the class
NH2008
Messages13
● 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.
NH2008
14
Abstraction Encapsulation Inheritance Polymorphism
NH2008
Characteristics of OOP
Characteristic of OOP
● 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.
● 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.
15
NH2008
Abstraction Encapsulation
16
NH2008
Student
Variables:name student idaddresscourse
Methods:changeAddress(String)changeCourse(String)
INTERFACEchangeAddress(String)changeCourse(String)
Example 7:
● 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
17
NH2008
Inheritance
Characteristics of OOP
18
Mobile Phone
NH2008
MobilePhone
modelmanufacturerprice
…
CameraPhone
modelmanufacturerpricepixel
…
superclass subclass
subclass
PdaPhone
modelmanufacturerpricememoryCap
…
Example 8:
Characteristics of OOP
● 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).
19
NH2008
Polymorphism
20
● 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
NH2008
Example 9:
21
● 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.
NH2008
Conclusion