OOPS In Java
[Link]
Introduction to OOPS in Java
Object-Oriented Programming (OOPS)
is a programming paradigm that
allows us to model real-world objects
in our code.
Java is an object-oriented
programming language that fully
supports the principles of OOPS.
OOPS in Java provides a modular and
organized approach to coding, making
it easier to understand and maintain.
1
Four Pillars of OOPS
Encapsulation: It is the process of
hiding the internal details of an object
and providing a public interface for
accessing and manipulating it.
Inheritance: It allows one class to
inherit the properties and methods of
another class, creating a hierarchical
relationship between classes.
Polymorphism: It allows objects of
different classes to be treated as
objects of a common superclass,
providing flexibility and extensibility in
code.
2
Classes and Objects
A class is a blueprint or template for
creating objects that define their
attributes and behaviors.
An object is an instance of a class that
has its own state and behavior.
Objects are created using the "new"
keyword and can be assigned to
variables of their respective class
types.
3
Constructors and Destructors
Constructors are special methods that
are called when an object is created
and are used to initialize its state.
In Java, constructors have the same
name as the class and can be
overloaded to create objects with
different initial states.
Java does not have explicit
destructors, but it has a garbage
collector that automatically frees the
memory of objects that are no longer
referenced.
4
Access Modifiers
Access modifiers control the visibility
and accessibility of classes, methods,
and variables.
The four access modifiers in Java are
public, private, protected, and default
(no modifier).
Public allows access from anywhere,
private restricts access to within the
class, protected allows access within
the package and subclasses, and
default allows access within the
package.
5
Inheritance and Polymorphism
Inheritance allows a class to inherit
properties and methods from another
class, promoting code reuse and
creating a hierarchical structure.
Polymorphism allows objects of
different classes to be treated as
objects of a common superclass,
enabling flexibility and extensibility in
code.
In Java, inheritance is achieved using
the "extends" keyword, and
polymorphism is achieved through
method overriding and method
overloading. 6
Method Overriding and Overloading
Method Overriding is the process of
providing a different implementation
of a method in the subclass that is
already defined in its superclass.
Method Overloading is the process of
defining multiple methods with the
same name but different parameters
in the same class or subclass.
Both method overriding and
overloading are examples of
polymorphism and allow for code
reuse and flexibility.
7
Abstract Classes and Interfaces
An abstract class is a class that
cannot be instantiated and serves as
a blueprint for subclasses.
Abstract classes can have abstract
methods, which are declared but not
defined in the abstract class and must
be implemented in the subclasses.
Interfaces are similar to abstract
classes but can only have abstract
methods and cannot contain any
implementation.
8
Encapsulation and Data Hiding
Encapsulation is the process of
bundling data and methods together
into a single unit, known as a class,
and hiding the internal details from
the outside world.
Data hiding is achieved by declaring
variables as private and providing
public methods (getters and setters)
to access and modify the data.
Encapsulation protects the data from
unauthorized access and ensures data
integrity.
9
Benefits of OOPS in Java
Reusability: OOPS allows for code
reuse through inheritance and
polymorphism, reducing development
time and effort.
Modularity: OOPS promotes modular
design, making it easier to
understand, maintain, and update
code.
Flexibility and Extensibility: OOPS
provides flexibility and extensibility
through polymorphism, allowing for
future enhancements and
modifications without affecting
existing code. 10