Introduction To Oop v2
Introduction To Oop v2
ORIENTED PROGRAMMING
Introduction
– A Brief Programming History
– OOP
– Classes & Objects
2
COMPUTER PROGRAMMING
3
COMPUTER PROGRAMMING
4
PROGRAMMING 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
10
EARLY PROGRAMMING
LANGUAGES
1950s 1960s 1970s
Simula
Lisp
Algol60 Algol68
Fortran Pascal
PL\1
Ada Ada98 C#
14
OOP
• OOP is mainly a program design philosophy.
• OOP uses a different set of programming
languages than old procedural
languages (C, Pascal, etc.). programming
• Everything in OOP is as
grouped sustainable "objects". self
Hence,by means
usability gain re-
you of four main object-oriented
programming concepts.
15
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.
• In this way, the data structure becomes an object that
includes both data and functions (methods) in one unit. In
addition, programmers can create relationships between
one object and another.
• For example, objects can inherit characteristics from other
objects.
16
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.
17
OOP
Key idea in object-oriented:
18
OOP BASIC
Object TERMINOLOGY
- 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
19
CLASSES AND OBJECTS
• A class is a prototype, idea, and blueprint for
creating objects.
• 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.
20
CLASSES (OBJECTS)
Class
Class Properties:
Objects: Belong to the class
Instances of the class
Methods:
Functions of class
Instance Properties:
Belong to the object
21
CLASSES & OBJECTS
A class is a definition of
objects with the same
properties and the same
methods.
22
CLASSES EXAMPLE
23
ALMOST EVERYTHING IN THE WORLD CAN
BE REPRESENTED AS AN OBJECT
• A flower, a tree, an animal
• A student, a professor
• A desk, a chair, a classroom, a building
• A university, a city, a country
• The world, the universe
• A subject such as CS, IS, Math, History, …
• An information system, financial, legal, etc..
24
What Is an Object, again?
25
MORE ABOUT OBJECTS
• Informally, an object represents an entity, either
physical, conceptual, or software.
– Physical entity
Truck
– Conceptual entity
Chemical
Process
– Software entity
Linked List
26
MORE FORMAL DEFINITION OF AN
“OBJECT”
An object is a computational entity that:
1. Encapsulates some state
2. Is able to perform actions, or methods, on
this state
3. Communicates with other objects via
message passing
27
CLASSES & OBJECTS
CLASS = CAR
PROPERTY METHOD
Plate no: AD47483
PROPERTY
Color: Blue
Moves
backward
Moves Do
forward
something:
Typeof car: Sedan Moves right
function & procedure
Model: CLK Moves left
Gear type: Automatic Stops
28
Classes &
CLASS
Objects
Tim
inTim
e hour
eAttribut
es:
minut hour =
e 8
minute =
void 30
addMinutes( int m ) Methods:
outTim void
addMinut
eAttribut es(int m)
es:
hour =
17
minute =
objects
35
Methods:
void
addMinut
es(int m)
29
CLASS/OBJECT
Each copy of an
object from a
particular class is
called an instance
of the class.
30
IN SHORT…
• An Object is a Class when it comes alive!
• Homo Sapien is a class, John and Jack are
objects
• Animal is a class “Snowball” the cat is an
object
• Vehicle is a class My neighbor's BMW is an
object
• Galaxy is a class, the MilkyWay is an object
31
TECHNICAL CONTRAST
BETWEEN OBJECTS & CLASSES
CLASS OBJECT
Class is a data type Object is an instance of Class.
Message
Property Property
Method Method
Object 3
Property
Method
34
EXAMPLE OF OBJECT
INTERACTION
• The OrderEntryForm wants Order to calculate the total NOK
value for the order.
calculateOrderTotal()
orderID
date
salesTotal
tax
shipDate
Message
OrderEntryForm Order
The class Order has the responsibility to calculate the total NOK value.
35
OOP Basic Concepts
– Encapsulation
– Inheritance
– Abstraction
– Polymorphism
36
ENCAPSULATION
37
ENCAPSULATION
• Is the inclusion of property & method within a
class/object in which it needs to function properly.
• Also, enables reusability of an instant of an
already implemented class within a new class
while hiding & protecting the method and
properties from the client classes.
38
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 encapsulation also allows a class
sense,
change to
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.
39
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”.
40
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
42
INHERITANCE
• Inheritance—a way of organizing classes
• Term comes from inheritance of traits like
eye color, hair color, and so on.
• Classes properties in common can be
with
grouped so that their common properties
are only defined once in parent class.
• Superclass – inherit its attributes & methods to
the subclass(es).
• Subclass can inherit all its superclass
attributes
– & methods besides having its
own unique attributes & methods.
43
INHERITANCE
45
INHERITANCE
46
INHERITANCE –
EXAMPLE Base
Person class
+Name: String
+Address:
String
Derived Derived
class class
Employee Student
+Company: +School: String
String
+Salary: double
+Name: String +Name: String
+Address: +Address:
String String 50
50
AN INHERITANCE HIERARCHY
Superclass
Vehicle
Subclasses
51
EXAMPLE: SINGLE INHERITANCE
One class inherits from another.
Ancestor
Account
- balance
- name
Superclass - number
(parent)
+ withdraw()
+ createStatement()
Inheritance
Relationship
Savings Checking
Subclasses
Descendents 52
EXAMPLE: MULTIPLE
INHERITANCE
• A class can inherit from several other
classes.
FlyingThing Animal
Multiple Inheritance
51
TYPE OF CLASSES
52
ABSTRACTION
• Abstraction is a design principle.
53
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. 54
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.
55
ABSTRACTION - TYPE OF
CLASSES
DOB:
Person Name:
Address:
DOB: DOB:
Name: Teacher Student Name:
Address: Address:
Specialization: GPA:
Academic Title: Courses:
Etc…: Etc…:
Can be instantiated directly 59
ABSTRACTION
• An abstract class is a class that may not have any direct
instances.
• An abstract operation is an that it
operation incomplete and requires
a
implementation child
of to
the operation. is
supply
Shape
{abstract} Abstract class an
draw () {abstract} Abstract operation
Circle Rectangle
draw () draw () 57
OOP Basic Concepts
– Encapsulation
– Inheritance
– Abstraction
– Polymorphism
58
POLYMORPHISM
59
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.
• In OOP, polymorphisms is a technical issue and
principle.
• It is achieved by using many different techniques named
method overloading, operator overloading, and method
overriding.
60
POLYMORPHISM
61
POLYMORPHISM
• In Java, two or more classes could each have a
method called output
• Each output method would do the right thing
for the class that it was in.
• One output might display a number
(output.number) in one class, whereas it might
display a name (output.text) in another class.
62
POLYMORPHISM
• It is the ability to look at a class in
its parent image.
• Lets see the robot example throughout the
following few slides
63
ADVANTAGES OF OOP
• Faster development
• Higher-quality software
• Encapsulation
64
DISADVANTAGES OF OOP
• Steep learning curve
• Could lead to larger program sizes
• Could produce slower programs
65
OOP SUITABILITY
66
R EF ER EN C E S
MIT OpenCourseWare:
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-01sc-
introduction-to-electrical-engineering-and-computer-science-i-spring-2011/unit-1-
software-engineering/object-oriented-programming
Larman, C. (2012). Applying UML and Patterns: An Introduction to
Object- Oriented Analysis and Design and Iterative Development, 3/e. Pearson
Education
India.
http://docs.oracle.com/javase/tutorial/java/concepts
http://en.wikipedia.org/wiki/Object-oriented_programming
https://www.udacity.com/wiki/classes
https://weblogs.java.net/blog/potty/archive/2014/01/20/introduction-object-
oriented-programming-oop-part-i
Lischner, Ray. "Introduction to Object-Oriented Programming." Exploring C++ 11.
67
Apress, 2013. 239-244.