Java OOP(Object Oriented Programming) Concepts
The core idea of OOPs is to bind data and the functions that operate on it, preventing unauthorized
access from other parts of the code. Java strictly follows the DRY (Don't Repeat Yourself) Principle,
ensuring that common logic is written once (e.g., in parent classes or utility methods) and reused
throughout the application. This makes the code:
Easier to maintain: Changes are made in one place.
More organized: Follows a structured approach.
Easier to debug and understand: Reduces redundancy and improves readability.
What is OOPs and Why Do We Use it?
OOPS stands for Object-Oriented Programming System. It is a programming approach that organizes
code into objects and classes and makes it more structured and easy to manage. A class is a blueprint
that defines properties and behaviors, while an object is an instance of a class representing real-
world entities.
diagram demonstrates the Java OOPs Concepts
Difference Between Java Classes and Objects
Class Object
Class is the blueprint of an object. It is used to
An object is an instance of the class.
create objects.
No memory is allocated when a class is Memory is allocated as soon as an object is
declared. created.
An object is a real-world entity such as a book,
A class is a group of similar objects.
car, etc.
Class is a logical entity. An object is a physical entity.
Objects can be created many times as per the
A class can only be declared once.
requirement.
Objects of the class car can be BMW,
An example of class can be a car.
Mercedes, Ferrari, etc.
Java Classes
A class in Java is a set of objects that share common characteristics and common properties. It is a
user-defined blueprint or prototype from which objects are created. For example, Student is a class
while a particular student named Ravi is an object.
Properties of Java Classes
Class is not a real-world entity. It is just a template or blueprint, or a prototype from which
objects are created.
Class does not occupy memory.
A class is a group of variables of different data types and a group of methods.
A Class in Java can contain:
o Data member
o Method
o Constructor
o Nested Class
o Interface
Class Declaration in Java
access_modifier class <class_name>
data member;
method;
constructor;
nested class;
interface;
Components of Java Classes
In general, class declarations can include these components, in order:
Modifiers: A class can be public or has default access.
Class keyword: Class keyword is used to create a class.
Class name: The name should begin with an initial letter (capitalized by convention).
Superclass (if any): The name of the class's parent (superclass), if any, preceded by the
keyword extends. A class can only extend (subclass) one parent.
Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any,
preceded by the keyword implements. A class can implement more than one interface.
Body: The class body is surrounded by braces, { }.
Constructors are used for initializing new objects.
// Java Class example
class Student {
// data member (also instance variable)
int id;
// data member (also instance variable)
String n;
public static void main(String args[]) {
// creating an object of
// Student
Student s1 = new Student();
System.out.println(s1.id);
System.out.println(s1.n);
Java Objects
An object in Java is a basic unit of Object-Oriented Programming and represents real-life
entities. Objects are the instances of a class that are created to use the attributes and methods of a
class. An object consists of:
State: It is represented by attributes of an object. It also reflects the properties of an object.
Behavior: It is represented by the methods of an object. It also reflects the response of an
object with other objects.
Identity: It gives a unique name to an object and enables one object to interact with other
objects.
Example of an object: Dog
Constructors
In Java, constructors play an important role in object creation. A constructor is a special block of
code that is called when an object is created.
Characteristics of Constructors:
Same Name as the Class: A constructor has the same name as the class in which it is
defined.
No Return Type: Constructors do not have any return type, not even void. The main purpose
of a constructor is to initialize the object, not to return a value.
Automatically Called on Object Creation: When an object of a class is created, the
constructor is called automatically to initialize the object’s attributes.
Used to Set Initial Values for Object Attributes: Constructors are primarily used to set the
initial state or values of an object’s attributes when it is created.
Constructor vs Method in Java
Features Constructor Method
Constructors must have
Methods can have any
the same name as the
valid name
Name class name
Methods have the return
Constructors do not
type or void if does not
return any type
Return Type return any value.
Features Constructor Method
Constructors are called
Methods are called
automatically with new
explicitly
Invocation keyword
Constructors are used to Methods are used to
Purpose initialize objects perform operations
Why Do We Need Constructors in Java
Constructors play a very important role, it ensures that an object is properly initialized before use.
What happens when we don't use constructors:
Without constructors:
Objects might have undefined or default values.
Extra initialization methods would be required.
Risk of improper object state
Types of Constructors in Java
1. Default Constructor - A constructor that has no parameters is known as default constructor.
The default constructor can be implicit or explicit.
Implicit Default Constructor: If no constructor is defined in a class, the Java compiler
automatically provides a default constructor. This constructor doesn’t take any parameters
and initializes the object with default values, such as 0 for numbers, null for objects.
Explicit Default Constructor: If we define a constructor that takes no parameters, it's called
an explicit default constructor. This constructor replaces the one the compiler would
normally create automatically. Once you define any constructor (with or without
parameters), the compiler no longer provides the default constructor for you.
2. Parameterized Constructor - A constructor that has parameters is known as parameterized
constructor.
3. Copy Constructor - Unlike other constructors copy constructor is passed with another object
which copies the data available from the passed object to the newly created object.
Data Types in Java
Packages
Packages in Java are a mechanism that encapsulates a group of classes, sub-packages, and
interfaces.
Types of Java Packages
Built-in Packages
User-defined Packages
1. Built-in Packages-
These packages consist of a large number of classes which are a part of Java API.Some of the
commonly used built-in packages are:
java.lang: Contains language support classes(e.g classes which defines primitive data types,
math operations). This package is automatically imported.
java.io: Contains classes for supporting input / output operations.
java.util: Contains utility classes which implement data structures like Linked List, Dictionary
and support ; for Date / Time operations.
java.applet: Contains classes for creating Applets.
java.awt: Contain classes for implementing the components for graphical user interfaces (like
button , ;menus etc). 6)
java.net: Contain classes for supporting networking operations.
2. User-defined Packages-These are the packages that are defined by the user.
List Set
1. The List is an indexed sequence. 1. The Set is an non-indexed sequence.
2. List allows duplicate elements 2. Set doesn't allow duplicate elements.
3. Position access to elements is not
3. Elements by their position can be accessed.
allowed.
4. Multiple null elements can be stored. 4. Null element can store only once.
5. List implementations are ArrayList, LinkedList, 5. Set implementations are HashSet,
Vector, Stack LinkedHashSet.