0% found this document useful (0 votes)
29 views5 pages

OOP - Class and Object

Classes and Objects are core components of object-oriented programming, where a class serves as a blueprint for creating objects, which are instances of that class. Classes define the structure (fields) and behaviors (methods) of objects, allowing for the modeling of real-world entities and promoting code reusability and encapsulation. Java categorizes classes into types such as Top-Level, Abstract, and Nested classes, each serving specific roles in application development.

Uploaded by

SachinJadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views5 pages

OOP - Class and Object

Classes and Objects are core components of object-oriented programming, where a class serves as a blueprint for creating objects, which are instances of that class. Classes define the structure (fields) and behaviors (methods) of objects, allowing for the modeling of real-world entities and promoting code reusability and encapsulation. Java categorizes classes into types such as Top-Level, Abstract, and Nested classes, each serving specific roles in application development.

Uploaded by

SachinJadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

2.

OOP — Class and Object


Introduction to Class and Object
Classes and Objects are fundamental building blocks of object-oriented programming.
A class is a blueprint (or template) for creating objects, and an object is an
instance of a class. In simple words, a class acts as a blueprint from which one or
multiple objects (also called instances) can be created. For example, an architect
creates a blueprint of a house before the builder constructs it. Similarly, a car
company designs a blueprint of a car before manufacturing its models. In both
examples, the blueprint represents the class, which defines the overall structure,
and the actual buildings or cars represent objects, which are created from that
class.
Classes and Objects allow us to model or represent real-world entities in code. In
simple terms, we can represent anything from the real world in code using a class,
and then create objects based on that class. It's important to understand that the
class itself is not a real-world entity (or the actual representation of real-world
entities in code); rather, it defines the structure to represent real-world
entities in code; and the objects created from the class are the actual
representations of those real-world entities. For example, a Car class defines the
structure of a car, but it does not represent actual real-world cars; instead, the
objects created from this class represent actual cars in code. The Car class is
just a blueprint used to create those cars in code.
A class defines the structure of an object. This means that within a class, we
specify what kind of data an object will store and what actions (or behaviors) it
will be able to perform. This structure is defined using the object's properties
(also known as attributes or characteristics) — which define what data the object
should hold, and behaviors (also known as actions or functionalities) — which
define what actions the object should be able to perform. Any object created from a
class will have the same properties and behaviors as defined by the class. For
example, if a car company creates a Car class (a blueprint of a car) with
properties like brand name, model name, car color, car price, year of manufacture,
engine type, number of gears, top speed etc., and with behaviors like starting the
engine, stopping the engine, accelerating, braking, honking, turning left, turning
right etc., then all car objects created from this class will have the same
properties and behaviors defined in the class.
It is important to note that, the data (the properties of an object) is stored in
objects, not in the class itself. An object is an instance of a class: while it is
created using a class, it is the object — not the class — that actually holds the
data. In simple words, a class serves as a blueprint that defines the structure of
objects, but it does not store any data itself. The actual data is stored in
objects created from the class. Furthermore, all objects are separate and
independent; each object stores its own data, so any changes made to one object do
not impact the others.
In Java, memory is not allocated when a class is defined; instead, memory is
allocated in the heap when an object is created.

Introduction to Fields and Methods


In programming, properties are referred to as “fields”, and behaviors are referred
to as “methods”. Fields are data used to represent the properties or attributes
that an object has. These fields are also known as member variables (or data
members). Methods are sets of instructions written in blocks of code that define
the behaviors or actions the object can perform. Methods operate on the values
stored in an object's fields. They can access, modify, or use these values to
perform specific actions or calculations or other operations. Methods are also
known as member functions. A class can contain zero or more fields or methods,
depending on its design. In object-oriented programming, fields and methods are
essential for defining an object's structure and behavior. Fields store the data of
the object, while methods define the actions the object can perform using that
data. This combination of fields and methods inside a class allows us to create
meaningful, interactive models of real-world entities in code.
Note that, each object has its own individual set of fields. This means that while
all objects created from a class have the same set of fields defined in the class,
but each object maintains its own separate copy of those fields. On the other hand,
methods are defined once in the class and are shared among all objects of that
class. Unlike fields, methods are not copied for each object — every object uses
the same set of method definitions provided in the class.
For example, Imagine a Car class that has fields like brand, model, color, and
price, and methods like accelerate, brake, and honk. Now, if we create two car
objects from this class, each car object will have its own separate copy of the
fields and values stored in these fields can be different for each object. That
means one car object can have the brand “Toyota”, model “Corolla”, color “Silver”
and price “12,00,000”, and another car object can have brand “BMW”, model “X5”,
color “Black” and price “88,00,000”. However, the methods like accelerate, brake,
and honk — are not copied for every object. These methods are defined once in the
class and are shared by all the objects. This means both car objects will use the
same method definition from the class to perform operations.

Why Classes and Objects Are Needed


Understanding the importance of classes and objects is essential. Here are the main
reasons why classes and objects are needed in programming:
To Model Real-World Entities: In real life, we interact with entities like Person,
Car, Bank, College, and many others — each having its own specific characteristics
and behaviors. In programming, classes allow us to represent these real-world
entities or concepts in code by defining their characteristics and actions using
fields and methods. For example, if you want to represent a car in code, you create
a class that specifies the characteristics and behaviors of a car. In this way, you
can model real-world entities in code.
To Promote Code Reusability: Once a class is defined in a program, you can reuse it
to create multiple objects without rewriting the same code, thereby promoting code
reusability. Without the concept of classes and objects, you would need to create
separate variables for each object's data manually, leading to repetitive and less
maintainable code. For example, Suppose we need handle the data like brand, model,
year, color, type and other details for 100 cars. Following class and objects
approach, we will define the class with

To Encapsulate Data and Behavior Together: In procedural programming like C, data


and functions are kept separately, which makes it harder to manage and scale the
application as it becomes larger and more complex. Classes help solve this problem
by combining data (called fields) and the operations on that data (called methods)
into a single unit.

Need to Recorrect

It is important to understand how and where a class can be defined within a Java
program. Based on where a class is defined, Java categorizes classes into two
primary types:
Top-Level Class: A top-level class is a class that is declared directly inside a
“.java” file and not enclosed within any other class.
Nested Class: A nested class is a class that is defined inside another class.

Top-Level Class
A Top-Level Class is a class that is not defined inside any other class or method
and It is written directly inside a java source code file (.java file). A top-level
class is the outermost structure of any Java file, and Java allows only one
“public” top-level class per source file. Top-level classes are used very often in
Java and usually form the main structure of an application or its parts.
Use of Top-Level Class
Defines the Main Parts of an Application: Top-level classes are used to create the
main parts or features of a program. These classes often represent key components
that control the main logic and behavior of the application. For example, classes
like Student, Course, and Faculty in a college application, or BankAccount,
FundDeposit, and DebitCard in a banking application, are typically written as top-
level classes because they represent the main logic and structure of the
application.
Acts as the Entry Point of the Program: In most Java programs, the “main()” method
— which is the starting point of the application — is written inside a top-level
class. This class serves as the entry point for the entire application. For
example, MainApp is a top-level class containing the main() method. When you run
the program, Java looks for this main() method and starts executing the code from
here.
Helps Organize Code Clearly: Top-level classes make it easier to organize large
applications by breaking them into separate, manageable parts, where each class is
designed to handle a particular role or operation within the application, which
keeps the code focused and easier to understand. By following the principle of
single responsibility, each class contributes to a cleaner, more maintainable
codebase. For example, you could have a Student class to manage student
information, a Course class to handle course-related data, and a MainApp class to
control the overall execution of the program.
Allows Reuse Across the Application: Once a top-level class is defined, it can be
reused in different parts of the application. Since top-level classes can be
reused, their logic or functionality only needs to be written once and can then be
used wherever needed. This helps keep the code modular and avoids repetition or
duplicating code. For example, you create a top-level class named Student in a file
called Student.java. You can then import and use this class in other parts of the
application, such as in a CourseRegistration class or a ReportGenerator class. This
allows you to reuse the same logic without rewriting the Student class again.

Key Characteristics of a Top-Level Class


Declared at the File Level: A top-level class is written directly inside a “.java”
file, not nested inside any other class or method.
Access Modifiers: A top-level class can be either a public or package-private; It
cannot be private or protected.

Types of Classes
In Java, there are multiple types of classes available, depending on how you design
or define them. These classes can be categorized based on their usage, structure,
and purpose. Each type of class serves a specific role in application development,
and knowing when and how to use each one is important. Understanding the different
types of classes is essential for designing modular, efficient, and well-structured
object-oriented programs.
The following is a list of the different types of classes that can be created in
Java:
Top-Level Class
Concrete Class
Abstract Class
Final Class
Static Nested Class
Non-static (Inner) Class
Local Inner Class
Anonymous Inner Class
POJO (Plain Old Java Object)
Utility Class
Wrapper Classes

🔹 1. Top-Level Classes
These are classes that are not defined within any other class.
Concrete Class: A regular class with full implementation (can be instantiated).
Abstract Class: Cannot be instantiated; may contain abstract and non-abstract
methods.
Final Class: Cannot be extended (subclassed).
Enum Class: Special class for fixed constants.
Record Class (Java 14+): Immutable data-carrier classes.
POJO (Plain Old Java Object): A simple Java object without special restrictions.
Utility Class: Contains only static methods (e.g., java.util.Collections).
Singleton Class: Only one instance exists throughout the application.
Marker Class: Empty class used to convey metadata using type (e.g., Serializable).
Immutable Class: Custom-designed with final fields, no setters (like String)
🔹 2. Nested & Inner Classes
Classes declared inside another class.
Static Nested Class: Nested class with static keyword. Doesn’t need outer class
instance.
Non-static Inner Class (Member Class): Instance-level inner class.
Local Inner Class: Defined inside a method or block.
Anonymous Inner Class: A class without a name, declared and instantiated in one
expression.
🔹 3. Special Purpose Classes
These are not keywords or formal types but commonly used design patterns or
architectures.
Wrapper Classes: Java’s boxed types like Integer, Double, etc. (wrap primitives
into objects).
Factory Class: Used to create and return instances (usually follows the Factory
design pattern).
Builder Class: Used to construct complex objects step by step.
DAO (Data Access Object) Class: Manages database operations for an entity.
DTO (Data Transfer Object) Class: Used for transferring data between processes.
Service Class: Contains business logic (commonly used in layered architecture).
Controller Class: Manages input, output, and user interaction (commonly in MVC).
Test Class: Used in unit testing (like with JUnit or TestNG frameworks).
🔹 4. Access-Level Variants (for Top-Level Classes)
Not types but important to mention:
Public Class: Accessible from any other class in any package.
Package-Private Class (default): Accessible only within the same package.
The object has four important parts:
Identity: Identity represents the unique name or reference of the object. It helps
to identify and differentiate each object from others in a program.
Fields: Fields are data used to represent the properties or attributes that an
object has. These fields are also known as member variables (or data members). A
class can consist of zero or more fields.
State: Each object has one or more fields, and each field stores one or more values
depending on its type. The combined values of all the fields are referred to as the
state of the object. This state represents the current data stored in the object.
Methods: Methods are sets of instructions written in blocks of code that define the
behaviors or actions the object can perform. Methods operate on the values stored
in an object's fields. They can access, modify, or use these values to perform
specific actions or calculations or any operations. Methods are also known as
member functions. A class can consist of zero or more methods.

Class level data and Object level data


Memory Allocation of Class, Object, Fields and Methods
Need to add picture - representation of class & object
Difference between class and object
introduction to all parts of oop like access-modifers and all other before starting
classes

You might also like