Introduction to OOP
Object-oriented programming is a programming paradigm which deals
with the concepts of object to build programs and software applications.
It is modeled around the real world.
The world we live in is full of objects. Every object has a well-defined
identity, attributes, and behavior.
The features of object-oriented programming also map closely to the
real-world features like inheritance, abstraction, encapsulation, and
polymorphism.
There are many object-oriented programming (OOP) languages, including:
1. JAVA
2. PHP
3. PYTHON
4. R
Need for Object-Oriented Programming
There were certain limitations in earlier programming approaches and to
overcome these limitations, a new programming approach was required.
1. Better Organization and Modularity:
OOP allows code to be organized into classes, making it modular. Each
class can represent a specific concept. This modularity makes code
easier to understand and maintain.
2. Code Reusability
Object-Oriented Programming (OOP) allows reusing code through
inheritance, where one class can reuse the features and functions of
other classes. This avoids repetition of the code and speeds up
development.
3. Improved Security
OOP ensures data security through encapsulation by restricting direct
access to object properties and providing controlled access via special
methods.
4. Productivity:
OOP allows programmers to construct new programs quickly by using
reusable code and multiple libraries.
Principles of Object-Oriented Languages
OOP languages follow certain principles. These principles map very
closely to the real world.
o Classes
o Objects
o Abstraction
o Inheritance
o Encapsulation
o Polymorphism
Classes:
A class is a blueprint or prototype for creating an object.
Class consists of data members and member functions, which can be
accessed and used by creating an instance of that class.
Objects:
An object is an instance of a Class.
An object is a real-word entity that has state and behaviour.
Objects have their own set of attributes (data) and methods (functions)
that describe and define their behavior.
When a class is defined, no memory is allocated but when it is
instantiated (i.e. an object is created), memory is allocated.
Abstraction:
Abstraction is a process of hiding the implementation details and
showing only functionality to the user.
Another way, it shows only essential things to the user and hides the
internal details, for example, sending SMS where we type the text and
send the message. We do not know the internal processing about the
message delivery.
Abstraction lets you focus on what the object does instead of how it
does it.
Inheritance:
Inheritance enables a class to inherit properties and actions from
another class.
When we inherit methods from an existing class, we can reuse methods
and fields of the parent class and we can add new methods and fields in
your current class also.
Encapsulation:
Encapsulation is a process of wrapping of data and methods in a single
unit is called encapsulation.
Polymorphism:
Polymorphism is considered one of the important features of Object-
Oriented Programming. Polymorphism allows us to perform a single
action in different ways.
Procedural Language vs OOP
Aspect Procedural Language Object-Oriented Programming
Focus Focuses on functions and logic. Focuses on objects, which
represent real-world entities.
Usage Used for small programs and Used for larger programs and
applications. applications.
Importance It gives importance to functions It gives importance to data over
over data. functions.
Code Linear and sequential - step-by- Modular, Organizes code into
Organization step problem-solving. classes and objects.
Security Less secure. Provides Better security through
encapsulation, access specifiers.
Approach Procedural programming Object-oriented programming
follows a top-down approach. follows a bottom-up approach.
Code Less reusable. (via functions) More reusable. (via inheritance and
reusability polymorphism)
Flexibility Less flexible. Highly flexible.
Data hiding There is not any proper way for Supports data hiding.
data hiding.
Implementation Difficult to implement change. Easier to manage and implement
of change. change.
Example C, FORTAN, Pascal JAVA, C++, VB.NET, C#.NET.
Classes and Objects:
Classes:
A class is a blueprint or prototype or template for creating an object.
Each class has its attributes and methods that can be accessed and
manipulated through the objects.
You can create multiple objects from a class.
Objects:
An object is an instance of a Class.
An object is a real-world entity that has state and behaviour.
Objects have their own set of attributes (data) and methods (functions)
that describe and define their behavior.
If we consider the real world, we can find many objects around us, cars,
dogs, humans, table, chair, fan, computer, pen, etc. All these real- world
objects have different states and behaviors.
E.g., Bikes have
o attributes - (brand, color, engine capacity, fuelCapacity),
o methods - (accelerating, , and changing gears).
If we consider a human, then its
o states(attributes)- name, age, height, weight, email, nationality
o behaviors(methods)–eating, sleeping, walking, talking
Difference Between Objects and Classes:
1. Class: A class is a blueprint or template for creating objects.
Object: An object is an instance of a class.
For Example, blueprint of a bike is not a bike.
2. There is no memory allocation done at the time of creating class,
whereas, memory will get allocated at the time of creating objects.
Why Should we Use Objects and Classes?
Modularity, information hiding, can be incorporated using objects.
Classes also provide the benefit of reusability.
For example, bike manufacturers reuse the same blueprint over and
over again to build lots of bikes. Same way programmers use the same
class repeatedly to create many objects.
Class Declaration in Java:
A class can be declared using the keyword class followed by the name of
the class.
Class declaration syntax:
[modifiers] class ClassName [extends SuperClassName] [implements InterfaceNames]
{
...
}
The items enclosed inside [] are optional.
o modifiers declare whether the class is public, protected, default, abstract or final.
o ClassName- name of the class.
o SuperClassName- name of the superclass of the class.
o InterfaceNames - list of the interfaces implemented by ClassName
Only the class keyword and the class name are mandatory. Other parameters are
optional.
Class Body
The class contains two different sections:
o variable declarations and
o method declarations.
Three types of variables:
o Local variables,
o Instance variables, and
o Class variables.
1) Local variables are defined inside a method.
2) Instance variable is defined inside the class but outside the methods.
3) Class variables are declared with the static modifier inside the class and
outside the methods.
1. Local Variables:
Declared within a method, constructor, or block
Only accessible within that method, constructor, or block
No default value; must be initialized before use
Stored in the stack memory
public class Example {
public void myMethod() {
int x = 10; // local variable
System.out.println(x);
}
}
2. Instance Variables:
Declared within a class, but outside any method or block
Each instance of the class has its own copy
Can be accessed using an instance of the class
Default values are assigned (e.g., 0 for integers, null for objects)
Stored in the heap memory
public class Car {
String color; // instance variable
int speed; // instance variable
public void accelerate() {
speed++;
}
public static void main(String[] args) {
Car myCar = new Car();
myCar.color = "Red";
myCar.speed = 60;
System.out.println(myCar.color + " " + myCar.speed);
}
}
3. Class Variables (Static Variables):
Declared with the static keyword within a class
Shared by all instances of the class
Can be accessed using the class name or an instance
Default values are assigned (e.g., 0 for integers, null for objects)
Stored in the method area of the heap memory
public class Counter {
static int count = 3; // class variable
public Counter()
{
count++;
}
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
System.out.println(Counter.count); // prints 2
}
}
Key differences:
Scope: Local variables are only accessible within a method or block, instance
variables are accessible within an instance, and class variables are accessible
using the class name or instance.
Lifespan: Local variables are created and destroyed within a method or block,
instance variables are created and destroyed with an instance, and class
variables are created when the class is loaded and destroyed when the class is
unloaded.
Memory allocation: Local variables are stored in the stack, instance variables
are stored in the heap, and class variables are stored in the method area of the
heap.
Creating Objects:
In Java, new keyword is used to create an object or instance of the class.
Syntax:
ClassName object = new ClassName();
Example:
SalesTaxCalculator obj1 = new SalesTaxCalculator();
This statement creates a new SalesTaxCalculatorobject.This
single statement declares, instantiates, and initializes the object.
Steps for creating an object:
1. Declaring an Object
2. Instantiating an Object
3. Initializing an Object
1) Declaring an Object:
Object declarations are same as variable declarations.
Generally, the declaration is as follows:
type name;
Example:
SalesTaxCalculator obj1;
where type is the type of the object (i.e., class name) and name is the
name of the reference variable used to refer the object.
2) Instantiating an Object:
After declaring a variable, an actual, physical copy of the object must be
acquired and assigned to that variable. In other words, (we need allocate
memory for the object).
The new keyword allocates memory for an object and returns a
reference, this reference is nothing but the address in the memory of
the object allocated by new.
This reference or memory address is stored in the variable declared.
SalesTaxCalculator obj1 = new SalesTaxCalculator()
In above statement, new operator creates an object obj1 by allocating
memory for its member variables.
3) Initializing an Object:
Initializing an object means assigning initial values to its attributes.