Chapter 1 (Part 1) - Introduction to Object-Oriented Programming
BIC20904 - Object Oriented Programming
Content
• History of Object-Oriented Programming
• Procedural vs OOP
• OOP Terminologies
• Advantages of OOP
Learning Outcomes
• At the end of this chapter, you should be able to:• Understand the differences between procedural programming and object-
oriented programming.
• Understand the terminologies in OOP.
• Know the advantages of OOP.
History of Object-Oriented Programming
• There are two common programming methods in practice today:procedural programming and object-oriented programming (OOP).So far, we have been creating procedural programs.
• In a procedural program, data is typically stored in a collection ofvariables and there is a set of functions that perform operations on thedata. The data and the functions are separate entities.
• Usually, the variables are passed to the functions that perform the desiredoperations. As you might imagine, the focus of procedural programmingis on creating the functions, or procedures, that operate on the program’sdata.
History of Object-Oriented Programming (cont.)
• Procedural programming has worked well for software developers formany years. However, as programs become larger and more complex, theseparation of a program’s data and the code that operates on the datacan lead to problems.
• For example, the data in a procedural program is stored in variables, aswell as in more complex structures that are created from variables. Theprocedures (functions) that operate on the data must be designedwith those variables and data structures in mind. But what happensif the format of the data is altered? Quite often, a program’sspecifications change, resulting in redesigned data structures.
History of Object-Oriented Programming (cont.)
• When the structure of the data changes, the code that operates on thedata must also be changed to accept the new format. Finding all thecode that needs changing results in additional work forprogrammers and an opportunity for bugs to be introduced intothe code.
• This has helped influence the shift from procedural programming toobject-oriented programming. Whereas procedural programming iscentered on creating procedures, OOP is centered on creatingobjects. An object is a software entity that combines both data andprocedures in a single unit.
History of Object-Oriented Programming (cont.)
• An object’s data items, also referred to as its attributes, are stored inmember variables. The procedures that an object performs are called itsmember functions. This bundling of an object’s data and procedurestogether is called encapsulation.
History of Object-Oriented Programming (cont.)
Figure 1
• Figure 1 shows a representation of a Circle object. It has just onemember variable to hold data and two member functions. The Circleobject’s data item is its radius. Its first function sets the radius, and itssecond function calculates and returns the area.
History of Object-Oriented Programming (cont.)
• The member variable and the member functions are all membersof the Circle object. They are bound together in a single unit.
• When an operation is to be performed, such as calculating the area ofthe circle, a message is passed to the object telling it to perform thegetArea function. Because getArea is a member of the Circle object, itautomatically has access to the object’s member variables. Therefore,there is no need to pass radius to the getArea function.
Procedural vs OOP
OOP Procedural
Based on the concept of objects, which contains attributes (data) and methods (procedures)
Based upon the concept of procedure calls
Divide the program into multiple objects Divide the program into multiple functions
Modification is easier as object is independent Modifications are difficult as they can affect the entire program
Objects communicate with each other by passing messages
Functions communicate with each other by passing parameters
Each object controls its own data Functions share global variables
Possible to hide data No data hiding mechanism
Has access specifiers Do not have access specifiers
Supported by C++, Java, Python Supported by C, Pascal, FORTRAN
OOP Terminologies - Class
• The most important term is the class. A class is the template orblueprint from which objects are actually made. It is a basic concept ofObject-Oriented Programming which revolve around the living/non-living entities.
• All code that you write in Java is inside a class.
• The standard Java library supplies several thousand classes.
• You can create your own classes in Java to describe the objects of theproblem domains of your applications and to adapt the classes that aresupplied by the standard library to your own purposes.
Exercise 1
• Define the classes based on the scenario given below:
“A student ride his bicycle from his home to the faculty using the same road everyday.”
OOP Terminologies - Objects
• Classes can be used to instantiate as many objects as are needed.
• Each object that is created from a class is called an instance of the class.
• A program is simply a collection of objects that interact with each other to accomplish a goal.
OOP Terminologies - Objects
Car class
The Car class defines the attributes and
methods that will exist in all objects
that are an instances of the vehicle
class.
Proton object
The Proton object is an
instance of the Car class.
Audi object
The Audi object is an
instance of the Car class.
OOP Terminologies – Attributes and Methods
• The properties of an object is represented by data fields (or attributes)with their current values. A circle object, for example, has a data fieldradius, which is the property that characterizes a circle. A rectangleobject has the data fields width and height, which are the properties thatcharacterize a rectangle.
• The behavior of an object (also known as its actions) is defined bymethods. To invoke a method on an object is to ask the object toperform an action. For example, you may define methods namedgetArea() and getPerimeter() for circle objects. A circle object mayinvoke getArea() to return its area and getPerimeter() to return itsperimeter. You may also define the setRadius(radius) method. A circleobject can invoke this method to change its radius.
OOP Terminologies – Attributes and Methods
Car
Attributes
•Door
•Seat
•Type
•Model
Methods
•Drive
•Stop
•Lock
•Unlock
Diagram 2: MyCar as an Object
OOP Terminologies - Abstraction
• Abstraction aims to hide complexity from the users and show them onlythe relevant information. For example, if you want to drive a car, youdon’t need to know about its internal workings.
• The same is true of Java classes. You can hide internal implementationdetails by using abstract classes or interfaces. On the abstract level, youonly need to define the method signatures (name and parameter list) andlet each class implement them in their own way.
OOP Terminologies - Encapsulation
• Encapsulation is a key concept in working with objects.
• It combines data (attributes) and procedures (methods) in one package and hiding the implementation of the data from the user of the object.
• The key to making encapsulation work is to have methods that can never directly access attributes in a class other than their own.
• Programs should interact with object attributes only through the object's methods.
• By encapsulating object attributes, you maximize reusability, reduce data dependency, and minimize debugging time.
OOP Terminologies – Data Hiding
• Data hiding makes your class attributes inaccessible from the outside andby providing getter and/or setter methods for attributes that shall bereadable or updatable by other classes.
• Data hiding is important for several reasons.• It protects of attributes from accidental corruption by outside objects.
• It hides the details of how an object works, so the programmer can concentrateon using it.
• It allows the maintainer of the object to have the ability to modify the internalfunctioning of the object without “breaking” someone else's code.
OOP Terminologies – Inheritance
• Inheritance is the ability of one class to extend the capabilities of another.• It allows code defined in one class to be reused in other classes.
• Consider a class named Car. A Car is a specialized form of the Vehicle class.• So, it is said that the Vehicle class is the base or parent class of the Car class.
• The Car class is the derived or child class of the Vehicle class.
OOP Terminologies – Polymorphism
• Polymorphism means "many forms."
• A reference variable is always of a single, unchangeable type, but it canrefer to a subtype object.
• A single object can be referred to by reference variables of manydifferent types—as long as they are the same type or a supertype of theobject. The reference variable's type (not the object's type), determineswhich methods can be called!
• Polymorphism allows extensibility.
Advantages of OOP
• Reusability• Reusability can be obtained by reusable software modules or components.
• Can be achieved by using the concepts of OOP.
• Extensibility• Extensibility is also a way of moving traditional software towards object-based
software.
• Instead of adding applications, OOP allows adding of small modules that work together to replace the notion of isolated applications.
• Maintainability• Maintaining the software or a system will become easier and cheaper.
• Software or a system is built with smaller and many applications.