Bytes
rocket

Your Success, Our Mission!

6000+ Careers Transformed.

Classes and Objects

Last Updated: 22nd March, 2026

3.1 Classes and Objects

3.1.1 Explanation

At the heart of Java lies Object-Oriented Programming (OOP) — a paradigm that models software around real-world entities and their interactions. Java implements OOP through classes (blueprints) and objects (instances).

class defines properties (variables) and behaviors (methods) of entities. It serves as a logical template from which multiple objects can be created. Each object contains its own data but shares the same structure defined by its class.

Key principles of OOP include abstraction (hiding complexity), encapsulation (bundling data and behavior), inheritance (reusability), and polymorphism (flexible behavior). These make Java modular, maintainable, and scalable.

Classes in Java can have fields, constructors, methods, and nested classes. They can also use access modifiers (public, private, protected, default) to control visibility, ensuring secure and organized code structure.

Objects are created using the new keyword, and memory is allocated dynamically on the heap. The JVM’s garbage collector later reclaims this memory, making Java memory-safe.

3.1.2 Code

public class Car {

// Fields (attributes)

String brand;

int year;

double price;

// Method (behavior)

void startEngine() {

System.out.println(brand + " engine started!");

}

// Main method

public static void main(String[] args) {

Car myCar = new Car(); // Object creation

myCar.brand = "Tesla";

myCar.year = 2025;

myCar.price = 75000.0;

System.out.println("Brand: " + myCar.brand);

System.out.println("Year: " + myCar.year);

System.out.println("Price: $" + myCar.price);

myCar.startEngine();

}

}

3.1.3 Example

Output:

Brand: Tesla

Year: 2025

Price: $75000.0

Tesla engine started!

Explanation:
The Car class defines attributes (brand, year, price) and a method startEngine().
The main() method creates an object myCar and initializes its attributes. When the method is called, it outputs a message representing the car’s behavior.

3.1.4 Use Cases

  • Representing real-world entities such as bank accounts, employees, or products.
  • Creating modular systems using class-based design.
  • Organizing code into logical units to enhance reusability.
  • Implementing data abstraction through methods and attributes.

3.1.5 Relevant Table

Concept

Description

Example

Class

Blueprint of objects

class Car {}

Object

Instance of a class

Car myCar = new Car();

Method

Defines behavior

startEngine()

Field

Holds object data

String brand;

Access Modifier

Controls visibility

publicprivate

3.2 Constructors and Inheritance

3.2.1 Explanation

constructor is a special method in Java used to initialize new objects. It has the same name as the class and no return type. Constructors can be:

  • Default Constructor: Automatically provided when no explicit constructor exists.
  • Parameterized Constructor: Accepts arguments to initialize fields.
  • Copy Constructor: Initializes an object with another object’s values.

Constructors help in ensuring consistency of object state. Java also supports constructor chaining — using this() to call another constructor within the same class or super() to call a superclass constructor.

Inheritance allows one class (subclass) to derive properties and behaviors from another (superclass), promoting code reusability. The extends keyword enables inheritance. Child classes can override parent methods, achieving runtime polymorphism.

Java supports single inheritance (one direct superclass), but multiple inheritance is achieved through interfaces.

3.2.2 Code

class Vehicle {

String type;

Vehicle(String type) {

this.type = type;

}

void displayType() {

System.out.println("Type: " + type);

}

}

class Car extends Vehicle {

String brand;

Car(String type, String brand) {

super(type); // Calling superclass constructor

this.brand = brand;

}

void displayDetails() {

System.out.println("Brand: " + brand);

displayType();

}

public static void main(String[] args) {

Car c1 = new Car("Electric", "Tesla");

c1.displayDetails();

}

}

3.2.3 Example

Output:

Brand: Tesla

Type: Electric

Explanation:
The subclass Car extends Vehicle, invoking the parent constructor using super(type).
This demonstrates inheritance and constructor chaining, ensuring all class properties are properly initialized.

3.2.4 Use Cases

  • Establishing parent-child relationships (e.g., Employee → Manager).
  • Promoting reusability through shared logic in base classes.
  • Defining specialized behavior by extending generic classes.

3.2.5 Relevant Table

Constructor Type

Description

Example

Default

Created automatically if none defined

Car() {}

Parameterized

Accepts arguments

Car(String brand)

Copy

Creates duplicate object

Car(Car other)

Inheritance Type

Description

Example

Single

One parent class

class B extends A

Multilevel

Chain of inheritance

A → B → C

Hierarchical

One parent, multiple children

A → B, C

3.3 Polymorphism, Encapsulation, and Abstraction

3.3.1 Explanation

Polymorphism means “many forms.” In Java, it allows a single interface or method to behave differently based on context.

  • Compile-time Polymorphism (Method Overloading): Multiple methods with the same name but different parameters.

  • Runtime Polymorphism (Method Overriding): Child class provides its own implementation of a method defined in the parent.

     

Encapsulation is the concept of binding data and methods together while hiding implementation details. It uses private fields with public getters and setters to protect internal state.

Abstraction focuses on exposing only essential features while hiding internal complexities. It is achieved through abstract classes and interfaces. These define contracts for implementation, ensuring consistency across multiple classes.

Together, these principles make Java systems modular, extensible, and secure — aligning perfectly with modern enterprise software development.

3.3.2 Code

abstract class Shape {

abstract double area();

}

class Circle extends Shape {

double radius;

Circle(double r) { radius = r; }

double area() { return Math.PI * radius * radius; }

}

class Rectangle extends Shape {

double length, width;

Rectangle(double l, double w) {

length = l; width = w;

}

double area() { return length * width; }

}

public class OOPDemo {

public static void main(String[] args) {

Shape s1 = new Circle(5);

Shape s2 = new Rectangle(4, 6);

System.out.println("Circle Area: " + s1.area());

System.out.println("Rectangle Area: " + s2.area());

}

}

3.3.3 Example

Output:

Circle Area: 78.53981633974483

Rectangle Area: 24.0

Explanation:
The Shape abstract class defines a contract (area()), while Circle and Rectangle provide specific implementations.
This demonstrates runtime polymorphism — the method call area() behaves differently depending on the object type referenced.

3.3.4 Use Cases

  • Framework design (e.g., GUI components with abstract base classes).
  • Implementing APIs where subclasses provide specific implementations.
  • Enforcing security and data protection via encapsulation.
  • Designing flexible systems supporting dynamic behavior at runtime.

3.3.5 Relevant Table

Concept

Description

Keyword / Mechanism

Example

Polymorphism

One interface, multiple behaviors

Overriding / Overloading

area() behaves differently

Encapsulation

Protect data using methods

privategetter/setter

getName(), setName()

Abstraction

Hide implementation details

abstractinterface

abstract class Shape

Module 3: Object-Oriented Programming in JavaClasses and Objects

Top Tutorials

Logo
Computer Science

CNN in Deep Learning 2026

A beginner-friendly guide to CNNs: understand deep learning essentials, create Python-based models, and explore advanced applications.

4 Modules12 Lessons151 Learners
Start Learning
Logo
Computer Science

Breaking The Limits: Scaling Databases with MySQL Partitioning

Learn MySQL partitioning with examples. Improve query performance, scalability, and data management using RANGE, LIST, HASH, KEY, and composite techniques.

7 Modules11 Lessons71 Learners
Start Learning
Logo

ML in Action: Hands-On Guide to Deploying and Serving Models

Learn model deployment and serving—from concepts to real-world architectures, tools, APIs, containers, and cloud workflows for production-ready ML.

4 Modules10 Lessons78 Learners
Start Learning
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2026 AlmaBetter