Can you write one small program that demonstrates all four pillars of object-oriented programming at the same time?
Yes, and the interesting part is showing where each pillar lives rather than naming them. Abstraction is the Storage interface: it states what a report can do with a store without saying how the store works. Encapsulation is the private list inside MemoryStorage: the outside world can only call save and count. Inheritance is DailyReport extending Report to reuse the storage handling. Polymorphism is the line where a Storage reference holds a MemoryStorage object and the runtime picks the right implementation.
public class Main {
public static void main(String[] args) {
Storage store = new MemoryStorage();
DailyReport report = new DailyReport(store);
report.saveAll("monday", "tuesday");
System.out.println("saved = " + report.savedCount());
}
}
interface Storage {
void save(String text);
int count();
}
class MemoryStorage implements Storage {
private final java.util.List<String> items = new java.util.ArrayList<>();
public void save(String text) { items.add(text); }
public int count() { return items.size(); }
}
class Report {
protected final Storage storage;
Report(Storage storage) { this.storage = storage; }
void save(String text) { storage.save(text); }
int savedCount() { return storage.count(); }
}
class DailyReport extends Report {
DailyReport(Storage storage) { super(storage); }
void saveAll(String... texts) { for (String t : texts) { save(t); } }
}Notice the two-way benefit. The abstract interface lets the report be written against any store, including a database-backed one, with no change to the report class. The encapsulation lets the storage swap its internal list for a map without touching the report. Inheritance gives DailyReport the storage plumbing for free, and polymorphism means the program never has to ask which report implementation it is holding.
One honest caveat: this example is compact, not realistic. In production code you would not extend Report to add a daily flavour, you would compose. The four pillars are still worth demonstrating because interviewers want to see you know how the mechanisms line up, and because abstraction plus polymorphism are what actually pay the bills at scale.