0% found this document useful (0 votes)
21 views

Lecture1 Object Oriented Paradigms, Abstraction, Principles

The document provides an overview of object-oriented programming concepts including: 1) Classes are blueprints that describe objects and contain their fields and methods. Objects are instances of classes that represent real-world entities. 2) Objects have both attributes (state/properties) and behaviors (actions/methods). They encapsulate related data and functions into a single unit. 3) OOP addresses limitations of procedural programming like unrestricted access between functions/data and poor modeling of real-world objects that have both attributes and behaviors.

Uploaded by

Abdul Haseeb
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Lecture1 Object Oriented Paradigms, Abstraction, Principles

The document provides an overview of object-oriented programming concepts including: 1) Classes are blueprints that describe objects and contain their fields and methods. Objects are instances of classes that represent real-world entities. 2) Objects have both attributes (state/properties) and behaviors (actions/methods). They encapsulate related data and functions into a single unit. 3) OOP addresses limitations of procedural programming like unrestricted access between functions/data and poor modeling of real-world objects that have both attributes and behaviors.

Uploaded by

Abdul Haseeb
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Object Oriented Paradigms,

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

 Aneeta Siddiqui [email protected]


Assistant Professor, CED
Room Number: BS-03
Tel: 111-994-994,
Course Website

 http://sites.google.com/site/ulaila206

4
Background

 OOP was developed due to some limitations in


earlier approaches to programming. (structured
programming)
 Structured program is “a list of instructions to
tell the computer what to do step-by-step”,
design through loops, sequences, decisions,
and logic blocks.

5
Limitations

There are basically two weaknesses in the


structured/procedural paradigm.
 Unrestricted Access (between functions and

data).
 Poor Real World Modeling.

6
Unrestricted Access

 In procedural language there are two types of


Data : Local Data and Global Data.
 Large number of connections between functions
and data makes program structure difficult to
conceptualize (see Figure 1)
 Program will become difficult to modify. A change
in global data may result in rewriting all functions
that access that item.

7
Procedural Paradigm

Global Global Global


data data data

Function Function Function Function

Figure 1 Representing large number of connections between


functions and data

8
Poor Real World Modeling

 In real world we deal with objects like people


and cars .
 These objects are not like data and functions
 Real world objects have attributes and
behavior.

9
Attributes

 Attribute are characteristics of an object .


Like:

People eye color and job titles.


Car Color, wheel size, interior
horsepower, number of doors.

10
Behaviors
 It is something , a real world object does in
response to some stimulus.
 For e.g.

If apply breaks in a car ,it will STOP


If you ask your boss for a raise , he will say
YES or NO.
Stopping and saying something are example of
behavior

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

 OOP is a methodology modeled in real life.


 An Object represent anything that can have
properties and behavior.
 Properties characterizes “Objects” , and its
current state.
 Behavior is the way and object acts and
reacts.

15
Idea behind OOP

 Both data and functions are combined into a


single unit called Object.
 The problem will no longer divide into
functions ( like procedural language) but it
will be divided into objects.

16
Objects

 Objects are key to understand object-oriented


technology.
 Look around right now and you'll find many
examples of real-world objects:
 your dog, your desk, your television set, your
bicycle.
 As Real-world objects have:
 state and behavior

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

 Software objects are conceptually similar to real-


world objects:
 They too consist of state and related behavior.
 An object stores its state in fields (variables in
some programming languages) and exposes its
behavior through methods (functions in some
programming languages).

19
Software Objects

 By Collecting the state


 (current speed, current pedal change and current gear)
 and providing methods for changing that state, the object
remains in control of how the outside world is allowed to
use it.
 For example, if the bicycle only has 6 gears, a
method to change gears could reject any value
that is less than 1 or greater than 6.

20
Software Objects

There are two types of Objects.

 Tangible Objects(real world entities like car, laptop etc)

 Intangible Objects ( abstract entities like math formula, event etc)

21
Examples Of Tangible Objects

 Ali is tangible objects, having some


characteristics (attributes and behavior) given
below.

22
Examples Of Intangible Objects

 Time is intangible(conceptual) object.

23
 Date is intangible(conceptual) object.

24
Object Orientation

 It is a technique in which we visualize our


programming problems in the form of objects
and their interaction as happens in real life.

25
26
Class

 Every object belongs to (is an instance of) a class.


 You may define many objects of the same class.

 An object may have fields, or variables (states)


 The class describes those fields

 An object may have methods (behaviors)


 The class describes those methods

27
Objects and Classes
Classes reflect concepts, objects reflect
instances that embody those concepts.

Object

class Girl

Jodie Daria Jane Brittany

28
Objects and Classes

29
Classes

 When working with objects, the first thing to do is to


create a blueprint of the object…
 An object is a specific instance of this blueprint
 The blueprint just describes how we create objects
 In Java, we call these blueprints Classes
Classes

A class is a collection of fields (data) and


methods (procedure or function) that operate on
that data.
Circle
centre
radius
circumference()
area()

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]
}

 Bare bone class – no fields, no methods


public class Circle {
// my circle class
}

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

 The fields (data) are also called the instance


varaibles.

33
Adding Methods

 A class with only data fields has no life. Objects


created by such a class cannot respond to any
messages.
 Methods are declared inside the body of the
class but immediately after the declaration of
data fields.
 The general form of a method declaration is:
type MethodName (parameter-list)
{
Method-body;
}

34
Adding Methods to Class Circle

public class Circle {

public double x, y; // centre of the circle


public double r; // radius of circle

//Methods to return circumference and area


public double circumference() {
return 2*3.14*r;
}
public double area() {
return 3.14 * r * r;
}}
Method Body
35
Data Abstraction

 Declare the Circle class, have created a new


data type – Data Abstraction

 Can define variables (objects) of that type:

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

 Methods: change address, change 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

 Our Circle class isn’t quite ready yet…


 You can only run a class if it has a main method and
even then you’re not creating Objects!
 The class we have tells Java what the object looks like
but it doesn’t tell Java how to make a circle Object
 The class variable, radius, doesn’t have a default value
so we need to find a way to give it one

Compiled By Umm-e-Laila 39
Constructor methods

 A constructor method is a special method that…


 … has no return type (not even void!)
 … has the same name as the class
 Its purpose is to set-up the object according to some
rules
 For our Circle class…
public class Circle {
// Object data
double radius;

public Circle(double radius) {


this.radius = radius;
}

}
Compiled By Umm-e-Laila 40
Constructor methods
public class Circle {
// Object data
double radius;

public Circle(double radius) {


this.radius = radius;
}
… We can have method variables with the same name
} as class variables… However, if we want to refer to
the class variable, we must prefix it with this.

 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…

OOP: Lecture #1 Compiled By Umm-e-Laila 41


Creating objects

 Now our Circle is complete we can start using Circle


objects
 They require a small amount of contiguous memory to
store all their properties
 To give them memory we use the new keyword
 Object always occupy memory in heap.

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]);

 So to create one of our Circles…

Circle c = new Circle(0.5);

 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

 Lets create a new class to test our Circle out


 CircleTest.java
public class CircleTest {
public static void main(String[] args) {
Circle c = new Circle(0.5);
Circle c2 = new Circle(1.0);

System.out.println(“The area of c is: “ +

c.area());
System.out.println(“The circumference of c2 is: ”
c2.circumference());
}
}

Compiled By Umm-e-Laila 44
Creating objects

 You don’t have to write a constructor


 If you choose not to, Java will create a default
constructor
 The default constructor does not set any values so the
object will do nothing until you start interacting with it
 Also, we can write multiple different constructors with
different parameter lists

Compiled By Umm-e-Laila 45
Differences between variables of
primitive Data types and object types

Primitive type int i = 1 i 1

Object type Circle c c reference

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

Before: After: Before: After:

i 1 i 2 c1 c1

j 2 j 2 c2 c2

c1: Circle c2: Circle

radius = 5 radius = 9

47
Passing Objects to Methods, cont.
main printAreas
method method
times

n 5 5 Pass by value (here the value is 5)

myCircle Reference Reference Pass by value (here the value is the


reference for the object)

myCircle: Circle

radius = 1

48
Array of Objects
Circle[] circleArray = new Circle[10];

An array of objects is actually an array of reference


variables. So invoking circleArray[1].findArea()
involves two levels of referencing as shown in the
next figure. circleArray references to the entire
array. circleArray[1] references to a Circle object.

49
Array of Objects, cont.
Circle[] circleArray = new Circle[10];

circleArray reference circleArray[0] Circle object 0


circleArray[1]

… Circle object 1

circleArray[9] Circle object 9

50
Automatic garbage collection

 If an object does not have a reference than that Object cannot be


used in future.

 That object becomes a candidate for automatic garbage collection.

 Java automatically collects garbage periodically and releases the


memory used to be used in the future.

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

 A Model is an abstraction of something real


or conceptual.

 We need models to understand as aspect of


reality.
 Highway maps
 Architectural models
 Mechanical models
Objects in School

You might also like