0% found this document useful (0 votes)
10 views41 pages

OOP L6-L7 OOP Principles and Intro To Class Concepts

The document provides an overview of Object-Oriented Programming (OOP) principles, including key concepts such as encapsulation, inheritance, and polymorphism. It explains how OOP organizes programs around data (objects) and their interactions, contrasting it with process-oriented programming. Additionally, it discusses classes and objects in Java, illustrating how to define and use them in code.

Uploaded by

sumedhkrishna12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views41 pages

OOP L6-L7 OOP Principles and Intro To Class Concepts

The document provides an overview of Object-Oriented Programming (OOP) principles, including key concepts such as encapsulation, inheritance, and polymorphism. It explains how OOP organizes programs around data (objects) and their interactions, contrasting it with process-oriented programming. Additionally, it discusses classes and objects in Java, illustrating how to define and use them in code.

Uploaded by

sumedhkrishna12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

OBJECT ORIENTED PROGRAMMING

(ICT 1271)
Object-Oriented Programming

• All computer programs consist of two elements: code and data.

• Program can be conceptually organized around its code or around its data.

• These are the two paradigms that govern how a program is constructed.
 process-oriented.
 Object Oriented
Process-oriented programming.
• This approach characterizes a program as a series of linear steps (that is, code).
• The process-oriented model can be thought of as code acting on data.
• Problems with this approach appear as programs grow larger and more complex.

Object-oriented programming
• Object-oriented programming organizes a program around its data (that is,
objects) and a set of well-defined interfaces to that data.
• An object-oriented program can be characterized as data controlling access to
code.
Data Abstraction

The act of representing essential features without including the


background details or explanations.
Encapsulation:

• In Object Oriented Programming, Encapsulation is defined as


binding together the data and the functions that manipulate
them.
Encapsulation
 Wrapping up of data and methods into a single unit (called class).

 The data is not accessible to the outside world and only those methods,
which are wrapped in the class, can access it.

 These methods provide the interface between the object's data and the
program.

 This insulation of the data from direct access by 'the program is called
datahiding.

 Encapsulation makes it possible for objects to be treated like 'black boxes‘.


Inheritance
 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 the combined features of both the classes.

 Subclass defines only those features that are unique to it.

 Without the use of inheritance, each class would have to explicitly include
all of its features.
Inheritance
Polymorphism
 Polymorphism means the ability to take more than one form.

 For example, an operation may exhibit different behavior in different


instances.

 The behavior depends upon the types of data used in the operation.

 For example, consider the operation of addition.

 For two numbers, the operation will generate a sum. If the operands are
strings, then the operation would produce a third string by concatenation.
Polymorphism
Object Oriented Approach - Benefits

• Through inheritance, we can eliminate redundant code and extend


the use of existing classes .

• We can build programs from the standard working modules that


communicate with one another, rather than having to start writing the
code from scratch.

• Principle of data hiding helps the programmer to build secure


programs.

• Reduces the rick factor in building large systems as they are built
incrementally from subsystems which are stable.
CLASSES AND OBJECTS
• Any concept we wish to represent is encapsulated in a class.

• Java class includes instance variables and methods.

• Class defines the shape and behavior of an object

• Class is a template for an object.

• Object is an instance of a class


• A Java program consists of one or more classes
• A class is an abstract description of objects

class Car {
...description of a car goes here...
}

• some objects of class:


• Here is another example of a class:

class Window { ... }

• Some examples of Windows:


Basic Terminology
• A class defines a kind of objects:
specifies the kinds of attributes (data) an object can have.
provides methods specifying the actions an object can take.

• An object is an instance of the class.

• Person is a class
Alice and Bob are objects of the Person class.
What does a class have?
• Members of a class:
Attributes (instance variables)
For each instance of the class (object), values of attributes can vary.
Methods

• Person class
Attributes: name, address, phone number
Methods: change address, change phone number
• Alice object
• Name is Alice, address is …
• Bob object
• Name is Bob, address is …
class classname
{ type instance-variable1;
// ...
type instance-variableN;

type methodname1(parameter-list)
{ // body of method
}
// ...
type methodnameN(parameter-list)
{ // body of method
}
}
class Box
{
double width;
double height;
double depth;
}
class Box
{
double width;
double height;
double depth;
}
Box mybox = new Box(); // create a Box object
mybox.width = 100;
class Box
{ double width;
double height;
double depth;
}
class BoxDemo
{ public static void main(String args[])
{ Box mybox = new Box();
double vol;
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
} }
class BoxDemo2
{ public static void main(String args[])
{ Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
vol = mybox1.width * mybox1.height * mybox1.depth;
System.out.println("Volume is " + vol);
vol = mybox2.width * mybox2.height * mybox2.depth;
System.out.println("Volume is " + vol);
} }
Declaring Objects

Box mybox = new Box();

This statement combines the two steps.

Box mybox; // declare reference to object


mybox = new Box(); // allocate a Box object
Assigning Object Reference Variables

Box b1 = new Box();


Box b2 = b1;
Box b1 = new Box();
Box b2 = b1;
// ...
b1 = null;
b1 has been set to null, but b2 still points to the original object.
Introducing Methods

type name(parameter-list)
{
// body of method
}
Adding a Method to the Box Class
class Box
{
double width;
double height;
double depth;

void volume()
{ System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
class BoxDemo3
{ public static void main(String args[])
{ Box mybox1 = new Box();
Box mybox2 = new Box();

mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;

mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;

mybox1.volume();
mybox2.volume();
}}
Returning value
class Box
{
double width;
double height;
double depth;

double volume()
{
return width * height * depth;
}
}
class BoxDemo4
{ public static void main(String args[])
{ Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15;
mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9;

vol = mybox1.volume();
System.out.println("Volume is " + vol);
vol = mybox2.volume();
System.out.println("Volume is " + vol);
} }
// parameterized method.
class Box
{ double width; double height; double depth;

double volume()
{ return width * height * depth; }

void setDim(double w, double h, double d)


{
width = w;
height = h;
depth = d;
}
}
class BoxDemo5
{ public static void main(String args[])
{
Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
mybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);

vol = mybox1.volume();
System.out.println("Volume is " + vol);

vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
// parameterized method.
class Box
{ double width; double height; double depth;

double volume()
{ return width * height * depth; }

void setDim(double w, double h, double d)


{
width = w;
height = h;
depth = d;
}
}

You might also like