PROGRAMMING 1 PROGRAMMING Topic 1 Introduction to Object Oriented Programming Mohd Hanapi Abdul Latif Contents Introduction to object Characteristics of OOP 2 Mohd Hanapi Abdul Latif 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 Mohd Hanapi Abdul Latif Object Oriented Programming (OOP) OOP is a programming technique that uses objects to design the applications and computer programs. The technology focuses on data rather than processes, with programs composed of self-sufficient modules 4 p g p ("classes"), each instance of which ("objects") contains all the information needed to manipulate its own data structure ("members"). An object-oriented program may thus be viewed as a collection of interacting objects - each object is capable of receiving messages, processing data, and sending messages to other objects. Mohd Hanapi Abdul Latif Introduction to Objects Object is a thing, tangible & intangible Tangible objects are objects that we can see & touch such as people (student, staff employee, etc, and vehicle (car, bike) Intangible objects are objects that we cannot see & touch such as laws, policies, events, accounts, etc. 5 as laws, policies, events, accounts, etc. Each object has attribute (state) and behaviors/operation (to manipulate the data). Eg. a bicycle has a attribute (the number of wheels and the current gear), and behavior (braking, accelerating, slowing down, changing gears). Mohd Hanapi Abdul Latif Elements of an object Attribute Behaviour State 6 2 Mohd Hanapi Abdul Latif Elements of an object Attribute The relevant data that relate with the object Also known as state or field or variable Example data for car object Color Model 7 Model Behaviour means the object can perform actions & can have actions performed on it. Also known as methods or functions Example for the operation on car Moving Changing gear Stop Mohd Hanapi Abdul Latif Elements of an object State The description about the data (fields/attribute) contained in an object at any point in time Example: Car have state (color, model) 8 State 1: blue, toyota State 2: red, honda If the data changed, means the state is changed Mohd Hanapi Abdul Latif 9 An air-conditioner at MK14B3 is an object -state: turn on current temperature is at 24 degree celcius. Example 1: -behaviour: change the temperature level Mohd Hanapi Abdul Latif Example 2: state: dark brown colours on its face, ears and feet behaviors : playing, 10 p y g, fighting, hunting A Siamese cat Mohd Hanapi Abdul Latif 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? th i t t d b h i 11 their states and behaviors Mohd Hanapi Abdul Latif 12 Example 4: How about these cars? Try to identify their common attributes and behaviours. car 3 Mohd Hanapi Abdul Latif 13 What can you tell from the Eg 1 to 4 ? 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. g p j p Mohd Hanapi Abdul Latif Class a kind of template (an abstraction of a real world entity) Provides the specifications object is created from a class. is called an instance of a class i i d f d 14 Object for the objects behaviors and attributes. A collection of objects of similar type. must be defined before creating an instance of the class. is comprised of data (attributes) & methods (operations) that manipulate these data. can create an object or many objects from a class Mohd Hanapi Abdul Latif 15 Mth d Variables Variables Represents the data/ attributes variables. a set of properties Student name id setName() setId() Example 5: Methods setId() updName() :Student name=Sarah id=1234 class object / instance of the class Represents the behaviors. Asequence of instructions that a class or an object follows to perform a task. Mohd Hanapi Abdul Latif Message An object cant exist on its own. An object communicates with other objects. Therefore, a message is used to instruct a class bj f k 16 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. Mohd Hanapi Abdul Latif Why OO is chose? Current problem of software: Software is difficult to develop, maintain and modify. Most software is over budget and delivered late. 17 Programmers still have to create software from the ground-up. OO introduces techniques that help: Developing a more cost-effective and efficient software that will be delivered on time. Adapt quickly to new changes or client demand Mohd Hanapi Abdul Latif 18 Characteristics/concepts of OOP Abstraction Encapsulation Inheritance P l hi Polymorphism 4 Mohd Hanapi Abdul Latif Abstraction the act of a software module containing relevant characteristics of the object to be represented (designing classes) Object modeling is base on data that can be 19 stored and functions, which are to manipulate the data. Type of objects are declared in program thru class. Different programmer may model an object from different class hierarchical classifications. Mohd Hanapi Abdul Latif Encapsulation To encapsulate means to enclose in a capsule. It is a way of packaging information. In OOP data (attributes) and methods(behavior) are encapsulated into container called classes. Classes have the property of data hiding. 20 Classes have the property of data hiding. Data hiding declaring the object data to be private so it will not be accessible by non-member modules This means although objects may know how to communicate with one another thru interface, normally the object is not allowed to know how other objects are implemented. Mohd Hanapi Abdul Latif Inheritance - the use of extends 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 l ib f i 21 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 Mohd Hanapi Abdul Latif An Inheritance Hierarchy 22 What properties does each vehicle inherit from the types of vehicles above it in the diagram? Mohd Hanapi Abdul Latif 23 Example 6 : mobile phone MobilePhone CameraPhone model manufacturer price pixel PdaPhone model manufacturer price
superclass subclass subclass model manufacturer price memoryCap
Mohd Hanapi Abdul Latif
Polymorphism From the Greek, meaning many forms. A feature that allows one interface to be used for a general class of actions. i f l i l h d 24 one interface, multiple methods Can be applied in the overloaded methods (a few methods that have the same name but with different parameters). 5 Mohd Hanapi Abdul Latif 25 Example 7 Class : Rectangle Variables : length, width, height Methods : . displayShape(char simbol) di l Sh (i t ) displayShape(int a) This class has 2 methods with the same name but with different type of parameters Mohd Hanapi Abdul Latif 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 26 manipulate these data. Characteristics of OOP is abstraction, encapsulation, inheritance and polymorphism. Mohd Hanapi Abdul Latif Next Java Programming Basics 27 Mohd Hanapi Abdul Latif Exercise Suppose you are given the task of designing a course registration system that keeps the courses register by the student. List the classes you think would be necessary for designing 28 you think would be necessary for designing such a system.