1 / 43

CHAPTER 2 Introduction to OOP

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.

hgarrido
Download Presentation

CHAPTER 2 Introduction to OOP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 2 Introduction to OOP OBJECT ORIENTED PROGRAMMING (CSC238) Zanariah Idrus | Computer Science Department | UiTM Kedah

  2. 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

  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

  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

  17. BASIC TERMINOLOGY

  18. 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

  19. 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

  20. CLASS VS OBJECT

  21. CLASS VS OBJECT

  22. 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

  23. 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.

  24. 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

  25. CHARACTERISTICS/CONCEPTS OF OOP

  26. CHARACTERISTICS OF OOP

  27. 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

  28. ABSTRACTION

  29. 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.

  30. 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.

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

  32. 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.

  33. ABSTRACTION VS ENCAPSULATION

  34. 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

  35. 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.

  36. 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?

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

  38. 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).

  39. 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 -

  40. 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

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

  42. 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.

  43. 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.

More Related