🧱Java OOP Programs

Object-oriented programming is at the heart of Java, and interviewers expect you to explain each pillar with a small example. These programs show encapsulation, inheritance, polymorphism and abstraction in working code you can run and modify.52 programs · each with full code, output and a step-by-step explanation

Most Asked in Interviews

🔍
Beginner
01beginner
Abstract Class
Abstraction

Defines a class that mixes an unimplemented method with a ready-to-use one.

View Program →
02beginner
Abstract Method
Abstraction

Declares a method with no body that every concrete subclass is forced to implement.

View Program →
03beginner
Aggregation
Relationships

Models a Library that holds Book objects created independently, outside of it.

View Program →
04beginner
Association
Relationships

Models a Student that references an assigned Teacher, with neither owning the other.

View Program →
05beginner
Composition
Relationships

Models a Library that creates and owns its own Book objects internally.

View Program →
06beginner
Constructor Overloading
Constructors

Defines two constructors with different parameter lists for the same class.

View Program →
07beginner
Constructors
Constructors

Introduces constructors as the special method that initializes a newly created object.

View Program →
08beginner
Create Class and Object
Classes & Objects

Defines a simple class and creates an object from it.

View Program →
09beginner
Default Constructor
Constructors

Shows the no-argument constructor Java supplies automatically when a class declares none of its own.

View Program →
10beginner
Dependency
Relationships

Models a Printer that uses a Document only for the duration of a single method call.

View Program →
11beginner
Encapsulation
Encapsulation

A class with a private field that validates every value before storing it.

View Program →
12beginner
Enum Class
Enums

Defines a fixed set of constants as an enum, each carrying its own data.

View Program →
13beginner
Final Class
Modifiers

Marks a class so it can never be extended by any subclass.

View Program →
14beginner
Final Method
Modifiers

Marks a single method so it can never be overridden, while the rest of the class stays open to subclassing.

View Program →
15beginner
Final Variable
Modifiers

A final variable assigned once and never reassigned.

View Program →
16beginner
Getter and Setter
Encapsulation

A class with private fields accessed only through matching getter and setter methods.

View Program →
17beginner
Instance Methods
Classes & Objects

Defines a method that reads an object's own fields to produce a per-object result.

View Program →
18beginner
Instance Variables
Classes & Objects

Shows that every object gets its own independent copy of a class's fields.

View Program →
19beginner
instanceof Example
Polymorphism

Checks an object's actual type at runtime before acting on it differently.

View Program →
20beginner
Interface
Interfaces

A single interface implemented by a single class.

View Program →
21beginner
Method Overloading
Polymorphism

Defines the same method name multiple times with different parameter lists.

View Program →
22beginner
Multiple Interfaces
Interfaces

A single class implementing two unrelated interfaces at once.

View Program →
23beginner
Nested Class
Nested Classes

Introduces nested classes with the simplest case — a static nested class.

View Program →
24beginner
Object Class Methods
Object Class

Shows the default toString(), equals(), and getClass() behavior every class inherits from Object, before any overriding.

View Program →
25beginner
Parameterized Constructor
Constructors

Initializes an object's fields at creation time using constructor arguments.

View Program →
26beginner
Single Inheritance
Inheritance

One subclass extends one parent class and inherits its members directly.

View Program →
27beginner
Static Methods
Classes & Objects

Defines a utility method callable directly on the class, without creating an object.

View Program →
28beginner
Static Variables
Classes & Objects

Uses a static field to track a count shared across every object of a class.

View Program →
29beginner
super Keyword
Inheritance

Uses super to call a parent class's constructor and its overridden method.

View Program →
30beginner
this Keyword
Classes & Objects

Uses this to distinguish a constructor parameter from a field of the same name.

View Program →
31beginner
toString() Override
Object Class

Overrides toString() to return a custom, readable string instead of Java's default format.

View Program →
32beginner
Upcasting
Inheritance

Assigns a subclass object to a variable typed as its parent class.

View Program →
Intermediate
33intermediate
Anonymous Inner Class
Nested Classes

Implements an interface inline, without ever declaring a separate named class.

View Program →
34intermediate
Cloneable Example
Object Class

Implements Cloneable to create a duplicate object using Java's built-in clone() mechanism.

View Program →
35intermediate
Copy Constructor
Constructors

Builds a new object by copying another object's field values.

View Program →
36intermediate
Create a Generic Stack Class
Generics

Writes a type-safe Stack<T> class once and reuses it for strings and integers without any casts.

View Program →
37intermediate
Downcasting
Inheritance

Converts a parent-typed reference back to its actual subclass type.

View Program →
38intermediate
equals() Override
Object Class

Overrides equals() to compare two objects by their field values instead of reference identity.

View Program →
39intermediate
Functional Interface
Interfaces

An interface with a single abstract method, implemented inline with a lambda.

View Program →
40intermediate
hashCode() Override
Object Class

Overrides hashCode() so it stays consistent with an equals() override.

View Program →
41intermediate
Hierarchical Inheritance
Inheritance

One parent class extended separately by multiple sibling subclasses.

View Program →
42intermediate
Immutable Class
Encapsulation

A class whose objects can never be modified after they're constructed.

View Program →
43intermediate
Inheritance & Method Overriding
Inheritance

A base class extended by a subclass with overridden behavior.

View Program →
44intermediate
Inner Class
Nested Classes

Demonstrates a non-static inner class that needs an outer instance and can read the outer object's fields directly.

View Program →
45intermediate
Interface Inheritance
Interfaces

An interface extending another interface, adding its own extra method.

View Program →
46intermediate
Interfaces & Polymorphism
Interfaces

Multiple classes implementing a shared interface.

View Program →
47intermediate
Local Inner Class
Nested Classes

Defines a named class entirely inside a method body, scoped to that one method.

View Program →
48intermediate
Method Hiding
Inheritance

A static method in a subclass hides the parent's static method of the same signature.

View Program →
49intermediate
Multilevel Inheritance
Inheritance

A three-class chain where each class extends the one before it.

View Program →
50intermediate
Static Interface Method
Interfaces

A static utility method defined directly on an interface, called through the interface name.

View Program →
51intermediate
Static Nested Class
Nested Classes

Uses a static nested Builder class to construct instances of the outer class step by step.

View Program →