2. OOP ITRODUCTION
• What is object oriented programming?
Object-oriented programming (OOP) is a way to
organize and conceptualize a program as a set
of interacting objects.
• The programmer defines the types of objects
that will exist.
• The programmer creates object instances as
they are needed.
3. OOP CONT………….
• The programmer specifies how these various
object will communicate and interact with each
other.
• Object-oriented programming is a methodology
that gives programmers tools to make this
modeling process easier.
• Simply we can say Object-oriented
programming, or OOP, is an approach to problem
solving where all computations are carried out
using objects.
4. TEMINOLOGIES USED IN OOP
• Object
• Class
• Instance
• Inheritance
• Encapsulation
• Abstraction
• Polymorphism
• Overloading
5. Object
• An object is a component of a program that
knows how to perform certain actions and
how to interact with other elements of the
program.
• An object contains both data and methods
that manipulate that data
– An object is active, not passive; (it does things)
– An object is responsible for its own data
6. Object
• Code in object-oriented programming is organized
around objects. Once you have your objects, they can
interact with each other to make something happen
• Let's say you want to have a program where a person
gets into a car and drives it from A to B. You would
start by describing the objects, such as a person and
car. That includes methods: a person knows how to
drive a car and a car knows what it is like to be driven.
Once you have your objects, you bring them together
so the person can get into the car and drive.
7. Class
• A class is a blueprint or template or set of
instructions to build a specific type of object.
or
• class is the generic definition for a set of
similar objects
• A class can be thought of as a template used
to create a set of objects.
• A class is a static definition; a piece of code
written in a programming language
8. Class
• So, let's say you want to use a person in your
program. You want to be able to describe the
person and have the person do something. A
class called 'person' would provide a blueprint
for what a person looks like and what a person
can do. To actually use a person in your
program you need to create an object. You use
the person class to create an object of the
type 'person.' Now you can describe this
person and have it do something.
9. Class
A class is a statement
class ClassVariable
attr
AttrName1
:
AttrNameN
meth Pattern1 Statement end
:
meth PatternN Statement end
end
10. Instance
• An instance is a realization of a particular item of a class. In
other words, an instance is an instantiation of a class. All
the instances of a class have similar properties, as
described in the class definition. For example, you can
define a class called "Student" and create three instances
of the class "Student" for "Peter", "Paul" and "Pauline".
• The term "object" usually refers to instance. But it is often
used loosely, and may refer to a class or an instance.
• instance is a concrete occurrence of any object, existing
usually during the runtime of a computer program
11. Example of instance
For examples, suppose that we have a class called Circle, we can create
instances of Circle as follows:
// Declare 3 instances of the class Circle, c1, c2, and c3
Circle c1, c2, c3; // They hold a special value called null
// Construct the instances via new operator
c1 = new Circle();
c2 = new Circle(2.0);
c3 = new Circle(3.0, "red");
// You can Declare and Construct in the same statement Circle
c4 = new Circle();
12. Inheritance
• Inheritance
• Inheritance is the process of forming a new
class from an existing class that is from the
existing class called as base class, new class is
formed called as derived class.
• This is a very important concept of object-
oriented programming since this feature helps
to reduce the code size.
13. Example
// A class to display the attributes of the vehicle
class Vehicle {
String color;
int speed;
int size;
void attributes() {
System.out.println("Color : " + color);
System.out.println("Speed : " + speed);
System.out.println("Size : " + size);
}
}
// A subclass which extends for vehicle class
Car extends Vehicle {
int CC;
int gears;
void attributescar() {
14. Example cont……
// The subclass refers to the members of the superclass
System.out.println("Color of Car : " + color);
System.out.println("Speed of Car : " + speed);
System.out.println("Size of Car : " + size);
System.out.println("CC of Car : " + CC);
System.out.println("No of gears of Car : " + gears);
}
}
public class Test {
public static void main(String args[]) {
Car b1 = new Car();
b1.color = "Blue";
b1.speed = 200 ;
b1.size = 22;
b1.CC = 1000;
b1.gears = 5;
b1.attributescar();
}
}
15. Encapsulation
• Encapsulation is placing the data and the
functions that work on that data in the same
place. While working with procedural
languages, it is not always clear which
functions work on which variables but object-
oriented programming provides you
framework to place the data and the relevant
functions together in the same object.
16. Benefits of Encapsulation
• Makes it easy to model real-world entities –
hence easy to understand and maintain
• Control the way data is accessed or modified
• Makes the class easy to use for clients
• Increase reusability
• Aids to the flexibility of design e.g. It is
possible to add accelerationConfiguration field
in the Car. This will enable you to have
different acceleration behaviour of each car.
17. Abstraction
• Polymorphism is the capability of a method to
do different things based on the object that it
is acting upon. In other words, polymorphism
allows you define one interface and have
multiple implementations
18. Overloading
• Overloading is ability of one function to
perform different tasks, i.e,it allows creating
several methods with the same name which
differ from each other in the type of the input
and the output of the function.
19. Private and public
• Public, means all the class members declared under public will be
available to everyone. The data members and member functions
declared public can be accessed by other classes too. Hence there
are chances that they might change them. So the key members
must not be declared public.
example
class PublicAccess
{
public:
// public access specifier
int x;
// Data Member Declaration
void display();
// Member Function decaration
}
20. Private
• Private , means that no one can access the
class members declared private outside that
class. If someone tries to access the private
member, they will get a compile time error. By
default class variables and member functions
are private.
21.
22. Methods and properties of the class
• A method in object-oriented programming
(OOP) is a procedure associated with a
message and an object. An object is made up
of data and behavior, which form the interface
that an object presents to the outside world.
Data is represented as properties of the object
and behavior as methods. For example, a
Window object would have methods such as
open and close, while its state (whether it is
opened or closed) would be a property.
23. Class methods
• Class methods are methods that are called on
a class rather than an instance. They are
typically used as part of an object meta-
model. I.e, for each class defined an instance
of the class object in the meta-model is
created.
24. property
• property is a member of a class that plays an
intermediary role to a field of the class. For example, if
you have a field of class and that member represents
the salary of an employee, a property can be the
"door" that other classes that need the salary must
present their requests to. As such, these external
classes cannot just change the salary or retrieve it as
they wish. A property can be used to validate their
request, to reject or to accept them.
• A property is used to "filter" access to a field of a class.
Therefore, you start by declaring a (private (if you don't
make it private, you may be deceiving the purpose of
creating a property)) field
25. property
• A property is used to "filter" access to a field
of a class. Therefore, you start by declaring a
(private (if you don't make it private, you may
be deceiving the purpose of creating a
property)) field