🧱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
Defines a class that mixes an unimplemented method with a ready-to-use one.
View Program →Declares a method with no body that every concrete subclass is forced to implement.
View Program →Models a Library that holds Book objects created independently, outside of it.
View Program →Models a Student that references an assigned Teacher, with neither owning the other.
View Program →Models a Library that creates and owns its own Book objects internally.
View Program →Defines two constructors with different parameter lists for the same class.
View Program →Introduces constructors as the special method that initializes a newly created object.
View Program →Defines a simple class and creates an object from it.
View Program →Shows the no-argument constructor Java supplies automatically when a class declares none of its own.
View Program →Models a Printer that uses a Document only for the duration of a single method call.
View Program →A class with a private field that validates every value before storing it.
View Program →Defines a fixed set of constants as an enum, each carrying its own data.
View Program →Marks a class so it can never be extended by any subclass.
View Program →Marks a single method so it can never be overridden, while the rest of the class stays open to subclassing.
View Program →A final variable assigned once and never reassigned.
View Program →A class with private fields accessed only through matching getter and setter methods.
View Program →Defines a method that reads an object's own fields to produce a per-object result.
View Program →Shows that every object gets its own independent copy of a class's fields.
View Program →Checks an object's actual type at runtime before acting on it differently.
View Program →A single interface implemented by a single class.
View Program →Defines the same method name multiple times with different parameter lists.
View Program →A single class implementing two unrelated interfaces at once.
View Program →Introduces nested classes with the simplest case — a static nested class.
View Program →Shows the default toString(), equals(), and getClass() behavior every class inherits from Object, before any overriding.
View Program →Initializes an object's fields at creation time using constructor arguments.
View Program →One subclass extends one parent class and inherits its members directly.
View Program →Defines a utility method callable directly on the class, without creating an object.
View Program →Uses a static field to track a count shared across every object of a class.
View Program →Uses super to call a parent class's constructor and its overridden method.
View Program →Uses this to distinguish a constructor parameter from a field of the same name.
View Program →Overrides toString() to return a custom, readable string instead of Java's default format.
View Program →Assigns a subclass object to a variable typed as its parent class.
View Program →Implements an interface inline, without ever declaring a separate named class.
View Program →Implements Cloneable to create a duplicate object using Java's built-in clone() mechanism.
View Program →Builds a new object by copying another object's field values.
View Program →Writes a type-safe Stack<T> class once and reuses it for strings and integers without any casts.
View Program →Converts a parent-typed reference back to its actual subclass type.
View Program →Overrides equals() to compare two objects by their field values instead of reference identity.
View Program →An interface with a single abstract method, implemented inline with a lambda.
View Program →Overrides hashCode() so it stays consistent with an equals() override.
View Program →One parent class extended separately by multiple sibling subclasses.
View Program →A class whose objects can never be modified after they're constructed.
View Program →A base class extended by a subclass with overridden behavior.
View Program →Demonstrates a non-static inner class that needs an outer instance and can read the outer object's fields directly.
View Program →An interface extending another interface, adding its own extra method.
View Program →Multiple classes implementing a shared interface.
View Program →Defines a named class entirely inside a method body, scoped to that one method.
View Program →A static method in a subclass hides the parent's static method of the same signature.
View Program →A three-class chain where each class extends the one before it.
View Program →A static utility method defined directly on an interface, called through the interface name.
View Program →Uses a static nested Builder class to construct instances of the outer class step by step.
View Program →