Java Lec 3
Java Lec 3
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.
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.
Usage Used for small programs and Used for larger programs and
applications. applications.
Code Less reusable. (via functions) More reusable. (via inheritance and
reusability polymorphism)
Data hiding There is not any proper way for Supports data hiding.
data hiding.
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).
A class can be declared using the keyword class followed by the name of
the class.
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.
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 Counter()
{
count++;
}
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:
Syntax:
Example:
1) Declaring an Object:
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:
3) Initializing an Object: