Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

SOE 201:

INTRODUCTION TO SOFTWARE ENGINEERING

OBJECT-ORIENTED PROGRAMMING
OBJECT ORIENTED PROGRAMMING (OOP)

• Is the use of computer programs to model real life problems.


• Is so-named because it involves the use of objects to represent real-life
entities, rather than using functions and logic.
• Each real-life entity is modelled into objects using their characteristic
features known as attributes.
• Objects render services to one another to maintain program cohesion.
• Examples of OOP Languages: Simula, Java, Python, C++, Visual Basic
.NET, Ruby, Scala, etc.

Object-Oriented Programming by Dr. Caesar Nwandu 2


OOP Building Blocks
• Object
• Class
• Methods
• Attributes

Object-Oriented Programming by Dr. Caesar Nwandu 3


Object
• Is a model that represents real-life entity
• Is a data field that models the attributes and characteristics of real-life entity.
• Is characterized by 3 basic features: IDENTITY, ATTRIBUTE (or State), and
METHOD (or Behaviour).
• Identity – The unique name that differentiates an object from others. It
reflects real life representation of the object, meant for easy manipulations
such as calling/invoking.
• Attribute – The situation in which an object exist at a given time. Attributes
are direct pointer to object states.
• Method – The behaviour used in operation. It performs actions, i.e., it
shows how an object interacts with other objects.
• Example: Object_Id = Dog;
Attributes {breed, colour, size, age};
Methods {sleep, bark, happy, run, jump}
Object-Oriented Programming by Dr. Caesar Nwandu 4
Class
• Is a model created with some characteristics that are shared by similar objects.
• Is a broad category that binds several objects with similar attributes. i.e.,
objects of the same class share similar characteristics
• Is a user-defined abstract template or guide for the modelling of objects, a
phenomenon called instantiation.
• Is a logical entity that binds data and functions together.
• A class is instantiated when an object that belongs to that class is created. i.e.,
an object is an instance of a class.
• A class is capable of having several instances, each sharing similar attributes
with others. E.g. Dog[Ascesian, Rottweiler, Police, German-Shepherd, Bull, …]
• Instances (or objects) of class Dog share similar attributes. E.g. breed, colour,
size.
Object-Oriented Programming by Dr. Caesar Nwandu 5
public class Dog //Class Declaration
//Instance Variables
String name;
String breed;
String colour;
//Constructor Declaration of Class
public dog(String name, String breed, String colour){
this.name = name;
this.breed = breed;
this.colour = colour;}
// methods
public String getName() {
return name;}
public String getBreed() {
return breed;}
public String getColour() {
return colour;} Object-Oriented Programming by Dr. Caesar Nwandu 6
Class – Object Illustration
• We created a class Dog to contain all properties of a dog (breed,
colour and size). We then create an instance of a dog, myDog to
represent my specific dog. myDog is an object of Dog class.
Class
Dog
Attributes: Methods:
Breed, Colour, Size bark()

Object Object
myDog: Rufus lewisDog: Melvin
Breed: Ascesian bark(); Breed: Rottweiler bark();
Colour: brown Colour: black
Size: big Size: medium

Object-Oriented Programming by Dr. Caesar Nwandu 7


Parent Class and Subclass
• Class can be a parent class or a subclass.
• Parent class (or superclass or base class) is the very first class created with certain
defined attributes, i.e., it originates attributes.
• Subclass (or derived class or extended class) refers to another class created to possess
some attributes of an existing (parent) class, in addition to some other attributes peculiar
to it. It extends the parent class by borrowing its attributes to use them within its own
block. Parent Class
dog
Attributes: Methods:
Breed, Colour, Size bark()

Subclass Subclass
HerdingDog TrackingDog
Breed, Colour, bark(); Breed, Colour, bark();
Size Herd(); Size track();

Object-Oriented Programming by Dr. Caesar Nwandu 8


OOP Principles
• Inheritance
• Encapsulation
• Abstraction
• Polymorphism

Object-Oriented Programming by Dr. Caesar Nwandu 9


Inheritance
• Inheritance is likened to adoption.
• It refers to a situation when a new class adopts or acquires the
properties of an existing class.
• The newly created class is called a subclass while the previously existing
class is called parent class.
• This means that instance(s) of subclass share attributes of instance(s) of
parent class.
• E.g., a HerdingDog has the ability to bark (inherited from Dog class) but uniquely
able to herd animals
• Inheritance supports reusability.
Object-Oriented Programming by Dr. Caesar Nwandu 10
Encapsulation
• Is hiding important codes of program components from the user (or
programmer) but gives him access to use it in his own work.
• It only exposes selected information to the outside world.
• When object is instantiated, data and methods are encapsulated in the object.
• It requires defining some fields as private and some as public.
• Private: methods and properties accessible by other methods within the same class
• Public: methods and properties accessible from outside the class
• E.g., outsiders will only see HerdingDog and TrackingDog as just dogs, without knowing
their unique abilities of herding and tracking respectively
• Symbolically, a car has private properties via the engine and public properties via
trafficators
• Encapsulation adds code security.
• Encapsulation gave rise to libraries offered by languages like Java and Python,
e.g., pandas, numpy, etc.

Object-Oriented Programming by Dr. Caesar Nwandu 11


Abstraction
• Is described as an extended encapsulation.
• It hides the code’s internal details from users.
• It presents the user with simple usable interfaces, without full
understanding of the code’s complexity.
• Selected data can only be accessed through classes and modified
through methods.
• Symbolically, a car driver operates the car with facilities on the dashboard
(e.g., steering wheel, accelerator, brakes) without worrying about how the
engine works

Object-Oriented Programming by Dr. Caesar Nwandu 12


Polymorphism
Polymorphism can be described as:
• The existence of objects in more than one form.
• Exhibiting one inherited behaviour as that of the parent class and uniquely as
that of a subclass.
• Behaviour can execute in 2 ways: method overriding and method overloading.
• Method overriding: Consider subclasses A [define Van], B [define Salon], & C
[define Trailer] from Parent class [define Vehicle], in which all 3 inherit
carryLoad() method,
• Method overriding says that all 3 vehicles can carry load but the sizes and weights are
defined differently
• Method overloading: Consider subclasses A [define HerdingDog] & B [define
TrackingDog] from Parent class [define Dog], in which both inherit
attendMedication() method,
• Method overloading says there may be different results for this method depending
on the number of parameters passed in it, i.e., number of times each dog visits a
doctor
Object-Oriented Programming by Dr. Caesar Nwandu 13
Review Questions

Object-Oriented Programming by Dr. Caesar Nwandu 14

You might also like