Introduction To OOP Concepts
Introduction To OOP Concepts
Introduction To OOP Concepts
2
Introduction
– A Brief Programming History
– OOP
– Classes & Objects
2
Computer Programming
An algorithm is a step-by-step process.
3
Computer Programming
The history of computer programming is a
steady move away from machine-oriented
views of programming towards concepts and
metaphors that more closely reflect the way
in which we ourselves see & understand the
world
4
Programming Languages
• Programming languages allow programmers
to develop software.
• The three major families of languages are:
– Machine languages
– Assembly languages
– High-Level languages
5
Machine Languages
• Comprised of 1s and 0s
• The “native” language of a computer
• Difficult to program – one misplaced 1 or 0
will cause the program to fail.
• Example of code:
1110100010101 111010101110
10111010110100 10100011110111
6
Assembly Languages
• Assembly languages are a step towards easier
programming.
• Assembly languages are comprised of a set of
elemental commands which are tied to a
specific processor.
• Assembly language code needs to be translated
to machine language before the computer
processes it.
• Example:
ADD 1001010, 1011010
7
High-Level Languages
• High-level languages represent a giant leap
towards easier programming.
• The syntax of HL languages is similar to English.
• Example:
grossPay = basePay + overTimePay
• Interpreter – Executes high level language programs without compilation.
9
Object-Oriented Languages
• The focus of OOP languages is not on structure,
but on modeling data.
• Programmers code using “blueprints” of data
models called classes.
• Examples of OOP languages include C++,
Visual Basic.NET and Java.
10
Early programming languages
1950s 1960s 1970s
Simula
Lisp
Algol60 Algol68
Fortran Pascal
BCPL Classic C
COBOL
PL\1
12
OOP
• OOP is mainly a program design philosophy.
• OOP is an approach use to develop
software programming.
• OOP was introduced to overcome drawback
in procedural approach to programming
such as Reusability and Maintainability.
13
OOP
• In OOP programmers define not only the data type of a data
structure, but also the types of operations/methods
(functions) that can be applied to the data structure.
14
Object-Oriented Programming Languages
· Pure OO Languages
Eiffel, Actor, Emerald, JADE, Obix, Ruby, Python, Scala,
Smalltalk, Self.
· Hybrid OO Languages
Delphi/Object Pascal, C++, Java, C#, VB.NET, Pascal,
Visual Basic, MATLAB, Fortran, Perl, COBOL 2002,
PHP, ABAP, Ada 95.
15
Benefits of OOP
• It can easily upgrade from small to large system.
• It can easy to partition works for same program.
• Having message passing technique makes
communication easier.
• Software complexity can be easily managed.
• Maintenance cost is less.
• Simple to implement.
16
OOP Basic Terminology
Object
- usually a person, place or thing (a noun)
Method
- an action performed by an object (a verb)
Property or attribute
- Characteristics of certain object.
Class
- a category of similar objects (such as automobiles), does
not hold any values of the object’s attributes/properties
17
Introduction
– A Brief Programming History
– OOP
– Classes & Objects
18
Classes and Objects
• A class is a prototype, idea, and blueprint for
creating objects. It is a user defined data-
type.
• An object is an instance of a class.
• For example, in Java we define classes,
which in turn are used to create objects
• A class has a constructor for creating objects
• Class is composed of three things: its name,
attributes/properties, and methods.
19
Classes (objects)
Class
Class Properties:
Objects: Belong to the class
Instances of the class
Methods:
Functions of class
Instance Properties:
Belong to the object
20
Classes & Objects
A class is a definition of
objects with the same
properties and the same
methods.
21
Classes Example
22
Almost everything in the world can be
represented as an object
24
More about objects
– Physical entity
Truck
– Conceptual entity
Chemical
Process
– Software entity
Linked List
25
More formal definition of an “Object”
26
Class/Object
Each copy of an
object from a
particular class is
called an instance
of the class.
27
Class/Object
28
In short…
• An Object is a Class when it comes alive!
• Homo Sapien is a class, John and Jack are
objects
29
Technical contrast between Objects & Classes
CLASS OBJECT
Class is a data type Object is an instance of Class.
31
Object Interaction
Object 1 Object 2
Message
Property Property
Method Method
Object 3
Property
Method
32
OOP Basic Concepts
– Encapsulation
– Inheritance
– Abstraction
– Polymorphism
33
Encapsulation
34
Encapsulation
• Is the inclusion of property & method within a
class/object in which it needs to function properly.
35
Encapsulation
• The class is kind of a container or capsule or a cell,
which encapsulate the set of methods, attributes and
properties to provide its indented functionalities to other
classes.
• In that sense, encapsulation also allows a class to
change its internal implementation without hurting the
overall functioning of the system.
• That idea of encapsulation is to hide how a class does its
operations while allowing requesting its operations.
36
Encapsulation in action
Example:
• Let’s say we have a class called “Date” (day,
month, year). And then you need to define
another class called “Person” that has the
following attributes (first name, last name, and
birthdate). So in this case we can instantiate an
object from class “Date” inside class “Person”.
37
Encapsulation – Benefits
Ensures that structural changes remain local:
Changing the class internals does not affect any code
outside of the class
Changing methods' implementation
does not reflect the clients using them
Encapsulation allows adding some logic when
accessing client's data
E.g. validation on modifying a property value
39
Inheritance
• Inheritance—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 in parent class.
• Super class – inherit its attributes & methods to
the subclass(es).
• Sub class – can inherit all its superclass
attributes & methods besides having its own
unique attributes & methods.
40
Inheritance
• Inheritance allows child classes to inherit the
characteristics of existing parent class
• Attributes (fields and properties)
• Operations (methods)
• Child class can extend the parent class
• Add new fields and methods
• Redefine methods (modify existing behavior)
• A class can implement an interface by providing
implementation for all its methods
47
47
Modes of Inheritance
Public mode: If we derive a sub class from a public base
class. Then the public member of the base class will
become public in the derived class and protected members
of the base class will become protected in derived class.
44
Inheritance
45
Inheritance – Example
Base class
Person
+Name: String
+Address: String
Employee Student
+Company: String +School: String
+Salary: double
+Name: String +Name: String
+Address: String +Address: String
50
50
An Inheritance Hierarchy
Superclass
Vehicle
Subclasses
51
Example: Single Inheritance
One class inherits from another.
Ancestor
Account
- balance
Superclass - name
- number
(parent)
+ withdraw()
+ createStatement()
Inheritance
Relationship
Savings Checking
Subclasses
Descendents 52
Example: Multiple Inheritance
Multiple Inheritance
50
Type of Classes
51
Abstraction
• Abstraction is a design principle.
• Is the process of removing characteristics from something in
order to reduce it to a set of essential characteristics.
52
Abstraction
• Abstraction allows programmers to represent complex
real world in the simplest manner.
• It is a process of identifying the relevant qualities and
behaviors an object should possess, in other word
represent the necessary features without representing
the back ground details
• You should always use abstraction to ease reusability,
and understanding for the design and enable extension.
• When we design the abstract classes, we define the
framework for later extensions.
53
Abstraction
• An abstract class, which declared with the
“abstract” keyword, cannot be instantiated.
• It can only be used as a super-class for
other classes that extend the abstract
class. Abstract class is a design concept
and implementation gets completed when
it is being realized by a subclass.
54
Abstraction - type of classes
DOB:
Person Name:
Address:
DOB: DOB:
Name: Teacher Student Name:
Address: Address:
Shape
{abstract} Abstract class
Circle Rectangle
draw () draw () 56
OOP Basic Concepts
– Encapsulation
– Inheritance
– Abstraction
– Polymorphism
57
Polymorphism
• Encapsulation, Inheritance, and
Abstraction concepts are very related
to Polymorphism.
58
Polymorphism
• Polymorphisms is a generic term that means 'many
shapes'. More precisely Polymorphisms means the
ability to request that the same methods be performed
by a wide range of different types of things.
60
Types of Polymorphism
1. Compile time polymorphism: This type of polymorphism is achieved by
function overloading or operator overloading
• Function Overloading: When there are multiple functions with same
name but different parameters then these functions are said to
be overloaded. Functions can be overloaded by change in number of
arguments or/and change in type of arguments.
• Operator Overloading: C++ also provide option to overload operators. For
example, we can make the operator (‘+’) for string class to concatenate
two strings. We know that this is the addition operator whose task is to
add two operands. So a single operator ‘+’ when placed between integer
operands , adds them and when placed between string operands,
concatenates them.
61
Types of Polymorphism
2. Runtime polymorphism: This type of polymorphism is
achieved by Function Overriding.
• Function overriding on the other hand occurs when a
derived class has a definition for one of the member
functions of the base class. That base function is said to
be overridden.
62
Advantages of OOP
• Code reuse & recycling
• Improved software-development productivity
• Improved software maintainability
• Faster development
• Lower cost of development
• Higher-quality software
• Encapsulation
63
Disadvantages of OOP
• Steep learning curve
• Could lead to larger program sizes
• Could produce slower programs
64
OOP Suitability
• Object oriented programming is good in
complex projects or modular type of
systems. It allows simultaneous system
development teams and also could aid
in agile system development
environments like Xtreme Programming.
65