Java OOP
Java OOP
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:
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.
Class Object
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.
Class is not a real-world entity. It is just a template or blueprint, or a prototype from which
objects are created.
o Data member
o Method
o Constructor
o Nested Class
o Interface
data member;
method;
constructor;
nested class;
interface;
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.
class Student {
int id;
String n;
// creating an object of
// Student
System.out.println(s1.id);
System.out.println(s1.n);
Java 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.
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.
Constructors play a very important role, it ensures that an object is properly initialized before use.
Without constructors:
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.
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.
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.
2. User-defined Packages-These are the packages that are defined by the user.
List Set
4. Multiple null elements can be stored. 4. Null element can store only once.