OOP L6-L7 OOP Principles and Intro To Class Concepts
OOP L6-L7 OOP Principles and Intro To Class Concepts
(ICT 1271)
Object-Oriented Programming
• 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 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.
The new class will have the combined features of both the classes.
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.
The behavior depends upon the types of data used in the operation.
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
• 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.
class Car {
...description of a car goes here...
}
• 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
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; }
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; }