Your Success, Our Mission!
6000+ Careers Transformed.
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).
A 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.
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();
}
}
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.
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 | public, private |
A 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:
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.
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();
}
}
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.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 |
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.
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());
}
}
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.
Concept | Description | Keyword / Mechanism | Example |
Polymorphism | One interface, multiple behaviors | Overriding / Overloading | area() behaves differently |
Encapsulation | Protect data using methods | private, getter/setter | getName(), setName() |
Abstraction | Hide implementation details | abstract, interface | abstract class Shape |
Top Tutorials
CNN in Deep Learning 2026
A beginner-friendly guide to CNNs: understand deep learning essentials, create Python-based models, and explore advanced applications.
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.
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.
All Courses (6)
Master's Degree (2)
Fellowship (2)
Certifications (2)