Professional Documents
Culture Documents
BASIC CONCEPT OF
OBJECT-ORIENTED
PROGRAMMING
OBJECT ORIENTED PROGRAMMING
Object-Oriented Programming (OOP) refers to a type of
computer programming (software design) in
which programmers define the data type of a data structure,
and also the types of operations (functions) that can be
applied to the data structure.
In this way, the data structure becomes an object that
includes both data and functions. In addition, programmers
can create relationships between one object and another. For
example, objects can inherit characteristics from other
objects.
CONCEPT OF OOPs
■ It is necessary to understand some of the concepts used
extensively in object-oriented programming. These include:
Representing an Object:
CONCEPT OF OOPs
OBJECTS
Programming problem is analyzed in term of objects and
the nature of communication between them. Program objects
should be chosen such that they match closely with the real-
world objects. (Entity)
Example #1:
Object: House
State: Address, Color, Area
Behavior: Open door, close door
CONCEPT OF OOPs
CLASSES
The entire set of data and code of an object can be made
a user-defined data type with the help of class. In fact, objects
are variables of the type class. Once a class has been defined,
we can create any number of objects belonging to that class.
Example #2:
Object: Car
State: Color, Brand, Weight, Model
Behavior: Break, Accelerate, Slow Down, Gear change.
CONCEPT OF OOPs
A class can be considered as a blueprint using which you
can create as many objects as you like. For example, here we
have a class Website that has two data members (also known
as fields, instance variables and object states).
This is just a blueprint, it does not represent any website,
however using this we can create Website objects (or
instances) that represents the websites. We have created two
objects, while creating objects we provided separate
properties to the objects using constructor.
CONCEPT OF OOPs
CONCEPT OF OOPs
INHERITANCE
✓ Inheritance enables new classes to receive—or inherit—the
properties and methods of existing classes.
✓ Inheritance is a way to express a relationship between
blueprints (classes). It's a way of saying: I want to build a
new object that is similar to one that already exists, and
instead of creating the new class from scratch, I want to
reference the existing class and simply indicate what's
different.
INHERITANCE
In object-oriented programming, inheritance enables new
objects to take on the properties of existing objects. A class
that is used as the basis for inheritance is called
a superclass or base class.
A class that inherits from a superclass is called
a subclass or derived class. The terms parent class and child
class are also acceptable terms to use respectively. A child
inherits visible properties and methods from its parent while
adding additional properties and methods of its own.
INHERITANCE
TWO OBJECTS OF INHERITANCE
✓ Subclassing – making a new class based on a previous
one.
✓ Overriding – changing how a previous class works
String s = “hello”;
int index = s.indexOf(‘1’, 2);
CALLING METHODS
• To illustrate how to call methods, let’s use the
String class as an example.
• You can use the Java API documentation to see
all the available methods in String class.
CALLING INSTANCE METHODS
• 2 sample methods
public char charAt(int index)
char x = str1.charAt(0);