Explain OOP concepts.
Tier: EssentialDifficulty: Easy
Object-oriented programming is a way of structuring code around objects that bundle data and behavior together, and it rests on four pillars.
- Encapsulation. An object hides its internal state behind a public interface. In Kotlin that's a class exposing methods or properties while keeping its fields
private, so callers can't reach in and put it into an invalid state directly. - Abstraction. You expose what an object does without exposing how it does it. A
Repositoryinterface with agetUser()method hides whether that data comes from a network call, a database, or a cache. - Inheritance. A class can extend another and reuse its fields and behavior, adding or overriding what's specific to it.
class SavingsAccount : Account()gets everythingAccountalready does. - Polymorphism. The same call behaves differently depending on the actual object behind it. If
Accounthas anopen fun withdraw()andSavingsAccountoverrides it, callingwithdraw()on anAccountreference at runtime runsSavingsAccount's version. That's runtime polymorphism. Overloading a function with different parameter types is compile time polymorphism.
The reason interviewers ask this is to see if you can connect the theory to real code, not recite definitions. In an Android app, encapsulation is why you expose a StateFlow from a ViewModel instead of a mutable one. Abstraction is why you code against a Repository interface instead of a concrete RetrofitRepository. Inheritance and polymorphism show up constantly in the Android framework itself, every Activity you write overrides onCreate(), which is polymorphism in action.