You are on page 1of 30

Bahria University Karachi Campus

CSC- 210
Object Oriented Programming

Lecturer
Sameena Javaid

https://sites.google.com/site/sameenajavaidcs
Bahria University Karachi Campus

LECTURE 3
FUNDAMENTALS OF OOP

OUTLINE

• Object Oriented Programming paradigm


• Concepts of Objects, Classes, Message Passing
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism
Bahria University Karachi Campus

PARADIGM OF OOP
• Programs are divided into entities Known as
classes of objects.
• Major emphasis is on objects rather then
procedures.
• Functions and Data Structures are required to
design characteristics of objects.
• Data hiding is an important feature.
• Objects communicate with each other through
functions.
• New data and functions can be easily added
whenever necessary.
• Follows bottom up approach in program design.
Bahria University Karachi Campus

BASIC CONCEPTS OF OOP


Bahria University Karachi Campus

Data Encapsulation
It is wrapping of
data and
associated
functions in one
single unit.
Data Abstraction
It is the act of
representing the
essential features
without including the
background details.
(Masking the
unwanted data from
user)
Bahria University Karachi Campus

Inheritance
It is the concept of
one class to inherit
properties from other
class.

Polymorphism
It is the ability for
data to be
processed in more
than one form.
Bahria University Karachi Campus

OBJECTS
• Objects are the basic run-time entities of an object
oriented system.
• They may represent a person, a place or any item
that the program must handle.
• Example

Representation of an object
Bahria University Karachi Campus

OBJECTS
Objects are made up of two "things":

• State (attributes/fields/properties + value)


• A given set of data which describes the object
• e.g., colour, size, amount, name, phoneNumber, etc...

• Behaviour (methods or functions)


• A set of operations that the object can perform
• e.g., walk, run, drive, sell, buy, transfer, computeTotal,
open, close, etc…

Identifying the state and behavior for real-world objects is a great


way to begin thinking in terms of object-oriented programming.
Bahria University Karachi Campus

EXAMPLE OF OBJECT, STATE, BEHAVIOURS


Bahria University Karachi Campus

CLASS
1. Class is a blueprint of object(s).
2. Objects created from the same class are similar but
not the same.
3. The objects are similar because each has similar
property type.
4. Each is different because the property value is
different.
5. Analogy:
a. Cars built and assembled based on the same
blueprint will definitely look similar in design.
However each car differs in serial numbers,
configuration, appearance, etc.
Car
blue
print Actual
CLASS cars
OBJECT(S)
Bahria University Karachi Campus

CLASS SYNTAX
1. We know a classname if there is keyword “class” before the
name.
2. The body of the class is enclosed in curly braces { }.
3. Hence, the syntax of a class is: <access specifier>class ClassName{}

4. An empty class is useless as it cannot be used to store any


attribute or have any behavior.
5. Hence, the body of a class should contain the following
members:
1. One or more fields (a.k.a. attributes).
public class ClassName {
2. One or more constructors.
int field1;
3. One or more methods. String field2;
ClassName(){}
void method1(){}
String method2(int arg){}
}
Bahria University Karachi Campus

APPLICATION CLASS ClassName


1. Application class is also known by other
names such as Driver , Test, Main or
Launcher class.
2. Application class is a class that contains +main(String[] args):void
the main method.
• public static void main(String [ ]
Example 1
args){ }
3. Not every class will contain the main Student
method.
4. If a program is made up of only one name: String
class, that class definitely has to
become the application class. greet():void
5. If a program is made up of many
classes, only one of the classes need to +main(String[] args):void
be the application class.
6. Application class will be the first class Example 2
loaded and executed.
Bahria University Karachi Campus

ACCESSING CLASS MEMBERS


1. Fields and methods are object members.
2. Object members are accessible using the dot notation.
3. Note that members are accessible through object
variable, not through class.
4. E.g.:
Car myCar = new Car(“AXE8888”);
myCar.engineNo = “MGH05GX100085”;
myCar.changeOwner(“Akram”);

Class Activity:
Can I use

Car.engineNo = “MGH05GX100085”;
Why and why not?
Bahria University Karachi Campus

MESSAGE PASSING
• A message for an object is a request for execution of a
procedure and therefore will invoke a function in the
receiving object that generates the desired result.
• Message passing involves specifying the name of the
object, the name of the function i.e. message and the
information to be sent.
• Objects can communicate with each other by passing
messages same as people pass messages to each
other.
• Objects can send or receive messages or information.
• Concept of message passing makes it easier to talk
about building systems that directly model or simulate
their real-world counterparts.
Bahria University Karachi Campus

MESSAGE PASSING
• For example, consider two classes Product and
Order. The object of the Product class can
communicate with the object of the Order class
by sending a request for placing order.
Bahria University Karachi Campus

MOVING TOWARDS
OBJECT-ORIENTED
PROGRAMMING
Bahria University Karachi Campus

MOVING TOWARDS OBJECTS…


public class MyClass {
public static void main(String[] • Structured
args){
code…
Programming
code… • Covered in
code…
code…
Lab1&2
code…
code…
code…
code…
code…
code…
code…
code…
}
}
Bahria University Karachi Campus

MOVING TOWARDS OBJECTS…


public class BMI { • Structured
public static void main(String[] args){
Programming
Scanner input = new Scanner(System.in);
double bmi, weight, height; • BMI Exercise
S.O.P(“Enter weight in Pounds”);
weight = input.nextDouble();
S.O.P(“Enther height in Inches”);
height = input.nextDouble();
bmi = weight/(height*height)*703;
String status;
if(bmi < 18.5)
status = “underweight”;
else if(bmi < 24.9)
status = “normal”;// so on.
S.O.P(“You BMI is “+ bmi+
“your status is” + status);
}
}
Bahria University Karachi Campus

MOVING TOWARDS OBJECTS…


public class MyClass {
public static void main(String[] args) public static void other()
{ {
code… code…
code… code…
other(); code…
code… code…
code… }
andAnother(); public static void andAnother()
code… {
code… code…
} code…
• Procedural
Programming code…
code…
• Covered in }
Lab3 (Next }
Week)
Bahria University Karachi Campus

MOVING TOWARDS OBJECTS…


public class MyClass {
public static void main(String[] args) public static double calculateBMI(double w,
{ double h) {
Scanner input = new Scanner(System.in); return w/(h*h)*703;
double bmi, weight, height; }
S.O.P(“Enter weight in Pounds”);
weight = input.nextDouble();
S.O.P(“Enther height in Inches”);
height = input.nextDouble(); public static String findStatus(double bmi)
bmi = calculateBMI(weight,height); {
S.O.P(“You BMI is “+ bmi+ String status;
“your status is” + findStatus(bmi)); if(bmi < 18.5)
status = “underweight”;
} else if(bmi < 25.0)
status = “normal”;// so on.
return status;
}
• Procedural Programming }// End MyClass
• BMI Exercise
Bahria University Karachi Campus

MOVING TOWARDS OBJECTS…


public class MyClass { public class MyOtherClass {
public static void main(String[] args) { public void aMethod() {
MyOtherClass m = new MyOtherClass(); code…
m.aMethod(); code…
AndAnotherClass a = new code…
AndAnotherClass(); code…
a.aMethod(); }
} }
}
public class AndAnotherClass
{
public void aMethod () {
code…
• Object-Oriented Programming code…
• Covered in Lab4 and onward code…
code…
}
}
Bahria University Karachi Campus

MOVING TOWARDS OBJECTS…


public class Application { public double calculateBMI() {
public static void main(String[] args) { return weight / (height*height)*703;
BMI bmi = new BMI(); }
bmi.getInput(); public String findStatus() {
bmi.printStatus(); String status=null;
} if(bmi < 18.5)
}// application class ends status = "underweight";
else if(bmi < 25.0)
public class BMI { status = "normal";// so on.
double weight, height,bmi; return status;
String status; }
public void printStatus() {
public void getInput() {
bmi = calculateBMI();
Scanner input = new
S.O.P("You BMI is "+ bmi+
Scanner(System.in); "your status is" + findStatus());
S.O.P("Enter weight in Pounds"); }
weight = input.nextDouble(); }//class BMI ends
S.O.P("Enther height in Inches");
• Object-Oriented Programming
height = input.nextDouble();
• BMI class exercise
}
Bahria University Karachi Campus

ENCAPSULATION
• Encapsulation is the principle of object-oriented
programming.
• In simple words, “Encapsulation is a process of
binding data members (variables, properties) and
member functions (methods) into a single unit”.
• The data is not accessible to the outside world,
and only those functions which are wrapped in
the class can access it.
• These functions provide the interface between the
objects data and the program. This insulation of
the data from direct access by the program is
called data hiding or information hiding.
• A Class is the best example of encapsulation.
Bahria University Karachi Campus

ABSTRACTION
• Abstraction refers to the representation of
necessary features without including details or
explanations.
• Classes use the concept of abstraction and are
defined as a list of abstract attributes and
functions to operate on these attributes.
• Data abstraction is a programming (and design)
technique that relies on the separation of
interface and implementation.
• Implementation denotes how variables are
declared, how functions are coded, etc. It is
through interface (function header/function
signature) a communication is sent to the
object as messages.
Bahria University Karachi Campus

ABSTRACTION
• When you press a key on your
keyboard the character appears on
the screen, you need to know only
this, but How exactly it works
electronically, is not needed.

• Another Example is when you use


the remote control of your TV, you
do not bother about how pressing a
key in the remote changes the
channel on the TV. You just know
that pressing the + volume button will
increase the volume.
Bahria University Karachi Campus

DIFFERENCE BETWEEN ABSTRACTION AND


ENCAPSULATION
S# Abstraction Encapsulation
1 Abstraction solves the Encapsulation solves the problem in
problem in the design level. the implementation level.
2 Abstraction is used for hidingEncapsulation means hiding the
the unwanted data and code and data into a single unit to
giving relevant data. protect the data from outside
world.
3 Abstraction lets you focus on Encapsulation means hiding the
what the object does instead internal details or mechanics of
of how it does it. how an object does something.
4 Abstraction- Outer layout, Encapsulation- Inner layout, used in
used in terms of design. terms of implementation.
For Example:- For Example:- Inner Implementation
Outer Look of a Mobile details of a Mobile Phone, how
Phone, like it has a display keypad button and Display Screen
screen and keypad buttons are connected with each other
to dial a number. using circuits.
Bahria University Karachi Campus

INHERITANCE
• Inheritance is the process by which
object of one class acquire the
properties of objects of another class.
• In OOP, the concept of inheritance
provides the idea of reusability. This
means that we can add additional
features to an existing class without
modifying it.
• This is possible by deriving a new class
from the existing one. The new class will
have combined features of both the
classes.
• Which in turn will increase the quality of
work and productivity.
Bahria University Karachi Campus

POLYMORPHISM Poly
(Many)
Morphism
(Forms)
Polymorphism
(Many Forms)

• Polymorphism is an important OOP concept.


• Polymorphism is a Greek term which means the ability to take
more than one form.
• In polymorphism an operation may show different behavior in
different instances.
• For example, + is used to make sum of two numbers as well as
it is used to combine two strings.
• This is known as operator overloading because same operator
may behave differently on different instances.
Bahria University Karachi Campus

POLYMORPHISM
• Same way functions can be overloaded.
• For example, sum()function may take two
arguments or three arguments etc.
• i.e. sum (5, 7) or sum (4, 6, 8).
• Single function Draw() draws different objects.
Bahria University Karachi Campus

You might also like