450 likes | 489 Views
CHAPTER 2 Introduction to OOP. OBJECT ORIENTED PROGRAMMING (CSC238). Zanariah Idrus | Computer Science Department | UiTM Kedah. OBJECTIVES. At the end of this topic, you should be able to : know the elements of an object. differentiate between objects and classes.
E N D
CHAPTER 2 Introduction to OOP OBJECT ORIENTED PROGRAMMING (CSC238) Zanariah Idrus | Computer Science Department | UiTM Kedah
OBJECTIVES 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
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.
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
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
BEHAVIOR • It can tell what the process can do/will be • Example: • Start engine • Speeding • Change gear • Stop
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
EXAMPLE 2: -state: dark brown colours on its face, ears and feet -behaviours : playing, fighting, hunting A Siamese cat
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
EXAMPLE 4: Different type of cats share the same behaviours.
EXAMPLE 5: • How about these cars? • Try to identify their common attributes and behaviours. car
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.
OBJECTS • 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
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
Student name id setName() setId() : Student name=“Sarah” id=“1234” EXAMPLE 6: • Represents the data/ attributes variables. • a set of properties class Variables Methods • Represents the behaviours. • A sequence of instructions that a class or an object follows to perform a task. object / instance of the class
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.
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
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
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.
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.
EXAMPLE 7: Variables: name student id address course Methods: changeAddress(String) changeCourse(String) Student INTERFACE changeAddress(String) changeCourse(String)
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.
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
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.
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?
CameraPhone model manufacturer price pixel … PdaPhone model manufacturer price memoryCap … EXAMPLE 8 : MOBILE PHONE MobilePhone model manufacturer price … subclass superclass subclass
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).
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 -
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
OBJECT-ORIENTED PROGRAMMING LANGUAGES • Pure OO Languages Smalltalk, Eiffel, Actor, Java • Hybrid OO Languages C++, Objective-C, Object-Pascal
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.
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.