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

Lec 6: Introduction To OOP: DR Ziaul Hossain

This document discusses object-oriented programming concepts including classes, objects, methods, and fields. It provides examples of how classes define common properties and behaviors of objects through methods. Structured programming is introduced as breaking problems into smaller pieces to solve top-down, while object-oriented programming uses reusable class templates to create objects that encapsulate both data and behaviors.

Uploaded by

Shah Zahid Newaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Lec 6: Introduction To OOP: DR Ziaul Hossain

This document discusses object-oriented programming concepts including classes, objects, methods, and fields. It provides examples of how classes define common properties and behaviors of objects through methods. Structured programming is introduced as breaking problems into smaller pieces to solve top-down, while object-oriented programming uses reusable class templates to create objects that encapsulate both data and behaviors.

Uploaded by

Shah Zahid Newaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Dept.

of Electrical & Computer


Engineering (ECE)

CSE 215 : Programming Language II

Lec 6: Introduction to OOP


Dr Ziaul Hossain
Structured Programming
• To solve a large problem, break the problem into
several pieces and work on each piece separately

• to solve each piece, treat it as a new problem that can


itself be broken down into smaller problems

• repeat the process with each new piece until each can
be solved directly, without further decomposition.

• This approach is also called top-down program design

Dept. of Electrical & Computer


Date Slide 2
Engineering (ECE)
Structured Programming Example
• Write a program to find the maximum of 10
integers and find the square of that number to
print it.

• Top-down approach
– Level 0: display square of the maximum number
– Next level:
• Make an array to store 10 integers
• Find maximum of these integers
• Find square of that number
• Print the square

Dept. of Electrical & Computer


Date Slide 3
Engineering (ECE)
Structured Programming: Problem
• Almost entirely produces the instructions necessary to solve a
particular problem.

• Difficult to reuse for other projects.


– a particular problem, subdividing it into convenient pieces, top-down
program design tends to produce a unique design.
– Adapting a piece of programming from another project usually involves
a lot of effort and time.

• Some problems by their very nature do not fit the top-down


program design.
– solution cannot be expressed easily in sequence of instructions.
– When the order in which instructions are to be executed cannot be
determined in advance, easily, a different approach is required.

Dept. of Electrical & Computer


Date Slide 4
Engineering (ECE)
Bottom-Up Design: the alternative
• Start “at the bottom”
– Already existing solution and a reusable software
component.
– From this position, work upward towards a solution to the
overall problem.

• It is important, in this approach, that the reusable


components are as “modular” as possible.

• A module is a component of a larger system that


interacts with the rest of the system in a simple and
well-defined manner.
Dept. of Electrical & Computer
Date Slide 5
Engineering (ECE)
Transforming into Modular Approach
F1(. .){
. . .
}
F2()
F2(. . .){
. . . F1()
}

F3(. . .){
. . .
}

int main void(){ F3()


. . . Task 1 Main()
. . .
. . . Task 2
}

Dept. of Electrical & Computer


Date Slide 6
Engineering (ECE)
Advantage of Modular Approach
• Data is protected, since it can be manipulated only in
known, well-defined ways (information hiding/
Encapsulation)

• Easier to write programs to use a module because the


details of how the data is represented and stored need not
be known (Abstraction)

• the storage structure of the data and the code for the
subroutines in a module may be altered without affecting
programs that make use of the module as long as the
published interfaces and the module’s functionality remain
the same

Dept. of Electrical & Computer


Date Slide 7
Engineering (ECE)
Object-Oriented Programming
• The central concept is the object, which is a kind of
module containing data and subroutines.
• An object is a kind of self-sufficient entity that has an
internal state (the data it contains) and that can
respond to messages (calls to its subroutines).

• Example
• A student-records object
– ID, Name, Address, Courselist etc. (internal state)
– Add a new student , add a course, print the student
information (messages)
Dept. of Electrical & Computer
Date Slide 8
Engineering (ECE)
Realising The Interaction Between
Objects
• Consider the following exchange between two people:
– First person: Jack, are you hungry?
– Second Person: Yes I am, Jill.
Are you hungry?
Name Name
JACK JILL
Question Question
Answer Yes, I am Answer

• Classifying this exchange in object-oriented terms,


there are two messages flying between two objects.
– The objects Jack and Jill.
– The messages are “are you hungry?” and “yes I am”.

Dept. of Electrical & Computer


Date Slide 9
Engineering (ECE)
Reusability leads to Class
• Objects may belong to the same family.

• These contain the same type of data and that


respond to the same messages in the same way.

• Such objects are said to all belong to the same


class. In programming terms, a class is created
and then one or more objects are created using
that class as a template.

Dept. of Electrical & Computer


Date Slide 10
Engineering (ECE)
Classes and Objects: Car

Class
<CAR>

Dept. of Electrical & Computer


Date Slide 11
Engineering (ECE)
Classes and Objects: Objects

Object
<7_series_BMW Object
> <VW_Beetle
>

Object
<Ford_Mustang
>

Dept. of Electrical & Computer


Date Slide 12
Engineering (ECE)
State
• The state of an object • State of the object
encompasses all of the (static) Ford_Mustang is THIS
properties of the object plus the
current (dynamic) values of each
COOL LOOKING RED
of these properties Ford_Mustang.
• A property is an inherent or
distinctive characteristic, trait, • A property of THIS
quality, or feature that contribute
to making an object uniquely that
Mustang is its Color.
object
• We will use the word attribute, or • Attribute is another
data member, to refer to the name for the property
state of an object
e.g. color.
Dept. of Electrical & Computer
Date Slide 13
Engineering (ECE)
Messages to Objects

“Start the
engine of the Object
BMW” <7_series_BMW>

Start_Engine

Dept. of Electrical & Computer


Date Slide 14
Engineering (ECE)
Method of a Class

Class
<CAR>

Start_Engine

Dept. of Electrical & Computer


Date Slide 15
Engineering (ECE)
Behavior
• Behavior is how an object acts and reacts, in
terms of state changes and interactions with
other objects.
• An operation is some action that one object
performs upon another in order to elicit a
reaction.
• We will use the word method to describe
object behavior in java.
• Invoking a method causes the behavior to take
place.
Dept. of Electrical & Computer
Date Slide 16
Engineering (ECE)
Class Members

• A class can have three kinds of members:


– fields: data variables which determine the status of
the class or an object
– methods: executable code of the class built from
statements. It allows us to manipulate/change the
status of an object or access the value of the data
member
– nested classes and nested interfaces

Dept. of Electrical & Computer


Date Slide 17
Engineering (ECE)
Fields – Declaration

• Field Declaration
– a type name followed by the field name, and optionally an
initialization clause
– primitive data type vs. Object reference
• boolean, char, byte, short, int, long, float, double
– field declarations can be preceded by different modifiers
• access control modifiers
• static
• final

Dept. of Electrical & Computer


Date Slide 18
Engineering (ECE)
Types of Methods
• There are 4 basic types of methods:
– Modifier (sometimes called a mutator)
• Changes the value associated with an attribute of the object
• E.g. A method like Change_Car_Color
– Accessor
• Returns the value associated with an attribute of the object
• E.g. A method like Price_of_Car
– Constructor
• Called once when the object is created (before any other method will
be invoked)
• E.g. Car(Mustang)
– Destructor
• Called when the object is destroyed
• E.g.~Car( )

Dept. of Electrical & Computer


Date Slide 19
Engineering (ECE)
First Example in Java

public class Test1{


public static void main (String args[]){
System.out.println(
"This test is Successfull");
}
}

Dept. of Electrical & Computer


Date Slide 20
Engineering (ECE)
Dept. of Electrical & Computer
Date Slide 21
Engineering (ECE)
• End of Lecture 5

Dept. of Electrical & Computer


Date Slide 22
Engineering (ECE)

You might also like