Skip to content
C

OOP Programs

Practicing small OOP-based programs helps solidify concepts like inheritance, polymorphism, and encapsulation in a practical, hands-on way.


Why practice these?

Practicing small OOP-based programs helps solidify concepts like inheritance, polymorphism, and encapsulation in a practical, hands-on way.

Program 1: Simple Inheritance Example

java
class Employee { String name = "Employee"; void work() { System.out.println(name + " is working"); } } class Manager extends Employee { Manager() { name = "Manager"; } } public class InheritancePractice { public static void main(String[] args) { Manager m = new Manager(); m.work(); } }

Output:

Manager is working

Program 2: Runtime Polymorphism Example

java
class Shape { void area() { System.out.println("Calculating area of a shape"); } } class Circle extends Shape { void area() { System.out.println("Calculating area of a circle"); } } public class PolymorphismPractice { public static void main(String[] args) { Shape shape = new Circle(); shape.area(); } }

Output:

Calculating area of a circle

Program 3: Encapsulation with Getters and Setters

java
class BankAccount { private double balance = 0; void deposit(double amount) { if (amount > 0) balance += amount; } double getBalance() { return balance; } } public class EncapsulationPractice { public static void main(String[] args) { BankAccount account = new BankAccount(); account.deposit(1500); System.out.println("Balance: " + account.getBalance()); } }

Output:

Balance: 1500.0

Key Points to Remember

  • Practicing small OOP programs is the best way to internalize how classes, objects, and inheritance actually behave.
  • Try modifying these programs (adding new subclasses or fields) to deepen your understanding further.
  • OOP-based coding questions are extremely common in interviews for freshers and experienced developers alike.