Introduction To OOP Concepts
Introduction To OOP Concepts
Introduction To OOP Concepts
OUTLINE
• Object Oriented Programming paradigm
• Concepts of Objects, Classes, Message Passing
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism
PARADIGM OF OOP
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":
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
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
bluepri
nt
Actual cars
CLASS
OBJECT(S)
SYNTAX
class <class_name>{
field;
method;
}
/Defining a Student class.
class Student{
//defining fields
int id;//field or data member or instance variable
String name;
//creating main method inside the Student class
public static void main(String args[]){
//Creating an object or instance
Student s1=new Student();//creating an object of Student
//Printing values of the object
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}
}
Bahria University Karachi Campus
6. Application class will be the first class loaded and +main(String[] args):void
executed.
Example 2
HOW OBJECTS ARE CREATED
Vehicle minivan = new Vehicle();
Vehicle truck = new Vehicle();
Class Activity:
Can I use
Car.engineNo = “MGH05GX100085”;
Why and why not?
class Student{
int rollno;
String name;
void insertRecord(int r, String n){
rollno=r;
name=n;
}
void displayInformation(){System.out.println(rollno+" "+name);}
}
class TestStudent4{
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.insertRecord(111,"Karan");
s2.insertRecord(222,"Aryan");
s1.displayInformation();
s2.displayInformation();
}
}
EXAMPLE
create a class of student . Think of its properties and behaviors. Write it down.
Create an object s1 of student class. Save your name , enrolment num, age, address in
it . And call method of markattendance() for s1.
// assign values to fields in minivan
minivan.passengers = 7;
minivan.fuelcap = 16;
EXAMPLE minivan.mpg = 21;
}
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.
ABSTRACTION 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.
Consider a real-life example of a man driving a car. The man only
knows that pressing the accelerators will increase the speed of car or
applying brakes will stop the car but he does not know about how
on pressing the accelerator the speed is actually increasing, he does
not know about the inner mechanism of the car or the
implementation of accelerator, brakes etc in the car. This is what
abstraction is.
In java, abstraction is achieved by interfaces and abstract classes.
We can achieve 100% abstraction using interfaces.
Bahria University Karachi Campus
DIFFERENCE
BETWEEN ABSTRACTION AND
ENCAPSULATION
S# Abstraction Encapsulation
1 Abstraction solves the problem in Encapsulation solves the problem in the
the design level. implementation level.
2 Abstraction is used for hiding the Encapsulation means hiding the code and
unwanted data and giving relevant data into a single unit to protect the data
data. from outside world.
3 Abstraction lets you focus on what Encapsulation means hiding the internal
the object does instead of how it details or mechanics of how an object does
does it. something.
4 Abstraction- Outer layout, used in Encapsulation- Inner layout, used in terms
terms of design. of implementation.
For Example:- For Example:- Inner Implementation
Outer Look of a Mobile Phone, like details of a Mobile Phone, how keypad
it has a display screen and keypad button and Display Screen are connected
buttons to dial a number. with each other using circuits.
Bahria University Karachi Campus
POLYMORPHISM
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