Lecture1 Object Oriented Paradigms, Abstraction, Principles
Lecture1 Object Oriented Paradigms, Abstraction, Principles
Abstraction, Principles
Compiled By: Umm-e-Laila
Lecture # 1
1
Course Books
Text Book:
Herbert Schildt, Java: The Complete Reference, McGraw-Hill
Education, Eleventh Edition
Craig Larman, Applying UML & patterns, 2 edition
Reference Books:
Cay S. Horstmann, Big Java: Early Objects, Wiley, 7th Edition
Herbert Schildt, Java: A Beginner's Guide, McGraw-Hill Education,
Eighth Edition
2
Course Instructors
Umm-e-Laila [email protected]
Assistant Professor, CED
Room Number: BS-04
Tel: 111-994-994, Ext. 536
http://sites.google.com/site/ulaila206
4
Background
5
Limitations
data).
Poor Real World Modeling.
6
Unrestricted Access
7
Procedural Paradigm
8
Poor Real World Modeling
9
Attributes
10
Behaviors
It is something , a real world object does in
response to some stimulus.
For e.g.
11
New data type: one more problem with
procedural language
Creating new data type in procedural
language is impossible
OOP approach provides you the opportunity
to create your own data type. This is called
extensibility.
12
Example of Structured Program
#include <stdio.h>
int main(void) {
float pie = 3.14;
int radius = 6;
printf("The radius of the circle is %d \n" , radius);
float area = (float)(pie* radius * radius);
printf("The area of the given circle is %f", area);
return 0;
}
13
Example of Structured Program
with functions
/**
* C program to find diameter, circumference and area of a circle /* Calculate diameter of circle whose radius is given
using functions */
*/ double getDiameter(double radius)
#include <stdio.h> {
#include <math.h> // Used for constant PI referred as M_PI return (2 * radius);
/* Function declaration */ }
double getDiameter(double radius);
double getCircumference(double radius); /* Calculate circumference of circle whose radius is given
double getArea(double radius); */
int main() double getCircumference(double radius)
{ float radius, dia, circ, area; {
/* Input radius of circle from user */ return (2 * M_PI * radius); // M_PI = PI = 3.14 ...
printf("Enter radius of circle: "); }
scanf("%f", &radius); /* Find area of circle whose radius is given
dia = getDiameter(radius); // Call getDiameter function */
circ = getCircumference(radius); // Call getCircumference double getArea(double radius)
function {
area = getArea(radius); // Call getArea function return (M_PI * radius * radius); // M_PI = PI = 3.14 ...
}
printf("Diameter of the circle = %.2f units\n", dia);
printf("Circumference of the circle = %.2f units\n", circ);
printf("Area of the circle = %.2f sq. units", area);
return 0;}
14
Object Oriented Programming
15
Idea behind OOP
16
Objects
17
Objects
Dogs have
state (name, color, breed, hungry) and
behavior (barking, fetching, wagging tail)
Bicycles have
state (current gear, current pedal cadence, current
speed) and
behavior (changing gear, changing pedal
cadence, applying brakes).
18
Software Objects
19
Software Objects
20
Software Objects
21
Examples Of Tangible Objects
22
Examples Of Intangible Objects
23
Date is intangible(conceptual) object.
24
Object Orientation
25
26
Class
27
Objects and Classes
Classes reflect concepts, objects reflect
instances that embody those concepts.
Object
class Girl
28
Objects and Classes
29
Classes
31
Classes
A class is a collection of fields (data) and methods
(procedure or function) that operate on that data.
The basic syntax for a class definition:
class ClassName [extends SuperClassName]
{
[fields declaration]
[methods declaration]
}
32
Adding Fields: Class Circle with fields
Add fields
public class Circle {
public double x, y; // centre coordinate
public double r; // radius of the circle
33
Adding Methods
34
Adding Methods to Class Circle
Circle aCircle;
Circle bCircle;
36
Object-oriented programming
An object-oriented program can be viewed as a collection
of cooperating objects
Lets think about the data and operations of some other
possible objects
Circle — Data: radius; Operations: area, circumference…
Person class
Attributes: name, address, phone number
Alice object
Name is Alice, address is …
Bob object
Name is Bob, address is …
Compiled By Umm-e-Laila 37
A Class as an Outline
38
Defining objects
Compiled By Umm-e-Laila 39
Constructor methods
This method will set the class variable to the value of the
parameter
A quick note on scope… it may seem like we’ve used
radius twice…
Compiled By Umm-e-Laila 42
Creating objects
With arrays, the new keyword just reserves enough memory
and sets default values
With objects, the new keyword reserves enough memory
and also calls the constructor function we just wrote
The syntax is almost the same as for arrays
[ObjectType] variableName = new [ObjectType]([parameters]);
This will call the constructor method in the Circle class with
the radius value of 0.5
OOP: Lecture #8 Compiled By Umm-e-Laila 43
Creating objects
c.area());
System.out.println(“The circumference of c2 is: ”
c2.circumference());
}
}
Compiled By Umm-e-Laila 44
Creating objects
Compiled By Umm-e-Laila 45
Differences between variables of
primitive Data types and object types
c: Circle
Created using
new Circle() radius = 1
46
Copying Variables of Primitive
Data Types and Object Types
Primitive type assignment Object type assignment
i=j c1 = c2
i 1 i 2 c1 c1
j 2 j 2 c2 c2
radius = 5 radius = 9
47
Passing Objects to Methods, cont.
main printAreas
method method
times
myCircle: Circle
radius = 1
48
Array of Objects
Circle[] circleArray = new Circle[10];
49
Array of Objects, cont.
Circle[] circleArray = new Circle[10];
… Circle object 1
50
Automatic garbage collection
51
Person Class Example
Person.java ------ defining Person
------------
public class Person public class PersonTest
{ {
private String _name; public static void main(String[] args)
private String _iceCream; {
Person joe = new Person();
public void setName(String newName) joe.setName(“Joseph”);
{ joe.setIceCream(“Rocky Road”);
this._name = newName; // this. is
optional Person mary = new Person();
} mary.setName(“Mary”);
public void setIceCream(String mary.setIceCream(“Chocolate Fudge”);
newIceCream) { … } mary.print(); }
}
public void print()
{ System.out.println(this._name + “
likes “ + this._IceCream); // this.
optional }}
52
What is Model