1 - CS112 - Week1 Part 1 & 2 - 31
1 - CS112 - Week1 Part 1 & 2 - 31
INTRO TO OOP
Prof. Mohamed Zayed 1
OUTLINE
2
3
4
INTRODUCTION TO OOP
6
INTRODUCTION TO OOP
7
OUTLINE
Introduction to OOP
Brief history of OOP
Procedural vs. OO programming
OOP Fundamental: Understanding Class & Object
4 Principles of OOP
Access Modifiers of Class Members
8
BRIEF HISTORY OF OOP
Simula 67 – the first OOP language; extension of ALGOL60
Smalltalk – conceived by Alan Kay (Smalltalk-72, Smalltalk-80); dynamically typed;
Strongtalk (1993) – Smalltalk + type system
mid 80’s – many languages added support for OO: Objective C, C++, Object Pascal,
Modula 3, Oberon, Objective CAML, CLOS.
Eiffel – Bertrand Meyer (1988) – Pascal-like syntax, design-by-contract
Other “exotic” OO languages: Sather, Trellis/Owl, Emerald, Beta (evolution of Simula), Self
Java – James Gosling (1995); Java 1.5 (2004) – support for generic programming
(Theoretical) extensions to Java: e.g. GJ (1998)
Java 2 (1999) - multiple configurations for different platforms. 9
OUTLINE
Introduction to OOP
Brief history of OOP
Procedural vs. OO programming
OOP Fundamental: Understanding Class & Object
4 Principles of OOP
Access Modifiers of Class Members
10
PROCEDURAL VS. OO PROGRAMMING
11
PROCEDURAL VS. OO PROGRAMMING
The problem with procedural programming is that code reusability is hard &
limited.
The difficulties with this type of programming is that software maintenance can be
difficult & time consuming.
When changes are made to the main procedure (top), those changes can cascade to
the sub procedures of main, & the sub-sub procedures & so on, where the change may
impact all procedures in the pyramid.
The aim of OOP is to try to increase the flexibility & maintainability of programs.
o Because programs created using an OO language are modular, they can be easier to develop,
12
& simpler to understand after development.
PROCEDURAL
VS.
OO
PROGRAMMING
13
OUTLINE
Introduction to OOP
Brief history of OOP
Procedural vs. OO programming
OOP Fundamental: Understanding Class & Object
4 Principles of OOP
Access Modifiers of Class Members
14
15
OOP FUNDAMENTAL: UNDERSTANDING CLASS & OBJECT
Data & Methods can be visible only within the scope of the class which declared them &
their descendants (private / protected), or visible to all other classes (public) –
Access Modifier.
17
OOP FUNDAMENTAL: OBJECT
19
CLASS VS OBJECT
20
OOP & JAVA
Class Example Object Example
public class Car { public class CarApp {
private String model; public static void main(String[] args) {
private double cc;
Car toyota = new Car();
public Car() { Car nissan = new Car();
this.model = model;
this.cc = cc; toyota.move();
} nissan.move();
}
public void move() { }
System.out.println(“Vrom-vrom”)
}
}
21
OUTLINE
Introduction to OOP
Brief history of OOP
Procedural vs. OO programming
OOP Fundamental: Understanding Class & Object
4 Principles of OOP
Access Modifiers of Class Members
22
4 PRINCIPLES OF OOP
23
24
4 PRINCIPLES OF OOP: INHERITANCE
26
4 PRINCIPLES OF OOP: ABSTRACTION
“An abstraction denotes the essential characteristics of an object that distinguish it from all other
kinds of objects & thus provide crisply defined conceptual boundaries, relative to the perspective
of the viewer” — G. Booch.
Abstraction is the process of hiding all but the relevant information about a thing to
make things less complex and more efficient for the user.
o Shows only essential characteristics.
27
4 PRINCIPLES OF OOP: POLYMORPHISM
Methods with same name but different arguments, performing different actions.
Methods with same name, but functioning in different ways.
Polymorphism means one name, many forms.
There are 2 basic types of polymorphism:
o Overloading
o Overriding
28
OUTLINE
Introduction to OOP
Brief history of OOP
Procedural vs. OO programming
OOP Fundamental: Understanding Class & Object
4 Principles of OOP
Access Modifiers of Class Members
29
ACCESS MODIFIERS OF CLASS MEMBERS
Java provides a number of access modifiers to set access levels for classes, variables,
methods, & constructors.
The 4 access levels are:-
Visible to the package, the default – No modifiers are needed.
Visible to the class only – private.
Visible to the world – public.
Visible to the package & all subclasses – protected.
30
ACCESS MODIFIERS OF CLASS MEMBERS
31