BSCS 208 AOOP Lecture 1 OOP Concepts
BSCS 208 AOOP Lecture 1 OOP Concepts
Object Oriented
Programming
Lecture 1 - Overview of
OOP Concepts
1
Objects and classes
A class is a template or a blueprint for an
object
When you create an instance of a class, you
get an object of that class
A class specifies the properties, features and
behavior of its objects in the same way the
DNA specifies the properties of the class of
living things.
In OOP, data are hidden and are accessible
only through the methods of that class.
2
Objects and classes – cont’d
Defining a class
• A class has 3 parts as shown below.
class ClassName
variables
methods
3
Objects and classes – cont’d
class ClassName
{
variables declaration;
methods declaration;
}
NB:
• Everything inside the curly brackets is optional, hence
class Empty{} is a valid java class.
• It can compile and even be instantiated into objects
but it will not do anything since it is empty.
4
Objects and classes – cont’d
Declaring variables and methods
class Rectangle
{
int length;
int width;
void method1(int x, int y)
{
}
int method2()
{
}
}
5
Abstraction and encapsulation
Encapsulation
• Wrapping of data and methods into a class
• Data are not accessible except through
methods
• This insulation of data from direct access is
called data hiding.
Abstraction
• Representing only the essential features and
hiding details
6
Inheritance
• Inheritance is the technique by which objects
of a class acquire properties of another class
• In Java, we use extends keyword for single
inheritance and implements for interface
(multiple) inheritance.
7
Inheritance – cont’d
There are 4 types of inheritance
• Single inheritance – class A extends B
• Multiple inheritance - class A extends B implements C
• Hierarchical inheritance -
• Multilevel inheritance –
The first two types are most important, and the
last two are their extensions
Therefore, we focus on single and multiple
inheritances in this class.
8
Inheritance – cont’d
9
Inheritance – cont’d
10
Polymorphism
Polymorphism is the ability to take more than
one form i.e. exhibit different behavior
We can implement polymorphism using
operators e.g.
• a + b = ab [string concatenation] and
• 1 + 2 = 3 [math operation]
This type of polymorphism is called operator
overloading.
11
Polymorphism – cont’d
We can also implement polymorphism
using methods
This type of polymorphism is called
method overriding
For example
• we can create an abstract class Shape and
then subclass it into Polygon and Rectangle
12
Polymorphism – cont’d
public abstract class Shape
{
private double area;
public abstract double getArea();
}
NB:
• The subclasses must override the getArea()
method to produce desired effects.
13
Message passing
Communication between objects
14
Dynamic binding
Involves linking a method call to the code
to be executed in response to the call.
The code to be executed is not known
until runtime
Dynamic binding is associated with
polymorphism and inheritance.
15
Benefits of OOP
Inheritance – increases reusability of code
Data hiding – increases security of code
Classes – enable the partitioning of large
systems into smaller modules
Easy to upgrade small OOP programs into
larger programs
Makes message communication easier
Easier to manage software complexity
16
Applications of OOP
Real time systems
Simulation and modeling
Object Oriented databases
Web applications
AI & expert systems
Parallel programming
Decision support and office automation
Graphics applications
17
Lab exercise
Write a program that uses the concepts
of inheritance and polymorphism to
calculate the areas of a rectangle and a
square.
18