Interview Q&A / Object-Oriented Programming

INTERVIEW Q&A · 69 QUESTIONS

Object-Oriented Programming interview questions

The four pillars, interfaces, abstract classes and design basics.

  1. What is a constructor, and what is constructor chaining?

    A constructor is a special method with the class's name and no return type that runs when an object is created, to set its starting state. Constructor chaining is one constructor calling another, either in the same class with this(...) or in the parent class with super(...), so setup code is written once.

    BeginnerIn-depth3 min read
  2. What is encapsulation, and how do access modifiers help achieve it?

    Encapsulation means keeping an object's data and the methods that change it together, and hiding the data so outside code can only change it through controlled methods. In Java, access modifiers (private, default, protected, public) decide who can see each member, and making fields private with validated methods is the usual way to encapsulate.

    BeginnerIn-depth3 min read
  3. What is the difference between method overloading and method overriding?

    Overloading means several methods in the same class share a name but take different parameters, and the compiler picks one. Overriding means a subclass gives its own version of a method it inherited, with the same signature, and the JVM picks the version at runtime based on the actual object.

    BeginnerIn-depth3 min read
  4. What is the difference between an abstract class and an interface?

    An abstract class is a partially built parent that can hold state, constructors and shared code, and a class can extend only one. An interface is a contract of methods that a class promises to provide, and a class can implement many.

    IntermediateIn-depth2 min read
  5. What is the difference between compile-time and runtime polymorphism?

    Compile-time (static) polymorphism is resolved by the compiler using the declared types, mainly through method overloading. Runtime (dynamic) polymorphism is resolved by the JVM using the actual object, through method overriding and dynamic dispatch. Fields, static methods and private methods are never resolved at runtime.

    AdvancedIn-depth4 min read
  6. Explain the Interface Segregation Principle with a Worker interface example.

    No class should be forced to implement methods it does not use. Split one big interface into small, focused ones so each class picks only what applies.

    BeginnerQuick answer1 min read
  7. Explain the Open/Closed Principle with a payment example.

    Classes should be open for extension but closed for modification. To add a new payment method, write a new class that implements an interface instead of editing the checkout code.

    BeginnerQuick answer1 min read
  8. How do you achieve abstraction in Java?

    Use abstract classes (partial implementations with abstract methods) or interfaces (pure contracts). Callers depend on the abstract type and never see the concrete class behind it.

    BeginnerQuick answer1 min read
  9. What are constructor injection and setter injection, and which should you prefer?

    Constructor injection passes dependencies when the object is created, so it is never half-built. Setter injection sets them afterwards. Prefer the constructor for required dependencies and setters only for optional ones.

    BeginnerQuick answer1 min read
  10. What are the types of inheritance, and which does Java support through classes?

    Single, multilevel, hierarchical, multiple and hybrid. Java supports the first three with classes; multiple inheritance of classes is not allowed, but a class can implement many interfaces.

    BeginnerQuick answer1 min read
  11. What are the uses of the super keyword?

    super refers to the parent part of the current object. It calls the parent constructor, calls a parent method that the child has overridden, or reaches a parent field hidden by a child field.

    BeginnerQuick answer1 min read
  12. What does the this keyword mean in Java, and where do you use it?

    this is a reference to the current object, the one whose method or constructor is running. It is used to separate fields from parameters of the same name, to call another constructor, and to pass the current object along.

    BeginnerQuick answer1 min read
  13. What is the difference between == and equals() in Java?

    == compares references for objects (are they the same object?) and values for primitives. equals() compares content, but only if the class overrides it; the default version behaves like ==.

    BeginnerQuick answer1 min read
  14. What is the difference between instance and static members of a class?

    An instance member belongs to each object and has a separate copy per object. A static member belongs to the class itself, exists once, and is shared by every object.

    BeginnerQuick answer1 min read
  15. What is the Observer pattern? Explain with a cricket score example.

    A subject keeps a list of observers and tells all of them when its state changes. Observers subscribe and unsubscribe at runtime, and the subject does not need to know what they do with the news.

    BeginnerQuick answer1 min read
  16. What is the Singleton pattern and how do you implement it?

    Singleton ensures a class has exactly one instance and gives a global access point to it. Make the constructor private, keep the instance in a static field, and return it from a static method.

    BeginnerQuick answer1 min read
  17. When does Java provide a default constructor, and when does it not?

    If you write no constructor at all, the compiler adds a public no-argument one. The moment you write any constructor yourself, the compiler stops adding it.

    BeginnerQuick answer1 min read
  18. Why use getters and setters when we could make fields public?

    Methods let a class validate input, compute values, log changes, and change its internal storage later without breaking callers. A public field gives up all of that control.

    BeginnerQuick answer1 min read
  19. Are constructors and private members inherited in Java?

    Constructors are never inherited; a child must define its own and call the parent's with super(). Private members exist inside the child object but cannot be accessed by name from the child.

    IntermediateQuick answer1 min read
  20. Can an abstract class have a constructor? Why would it need one?

    Yes. It cannot be instantiated directly, but a subclass constructor calls it with super() so the parent's fields get initialised.

    IntermediateQuick answer1 min read
  21. Can static methods be overridden in Java?

    No. A static method in a child with the same signature hides the parent's one. The call is resolved at compile time from the reference type, so there is no runtime polymorphism.

    IntermediateQuick answer1 min read
  22. How do you create an immutable class in Java?

    Make the class final, all fields private and final, set them only in the constructor, add no setters, and copy any mutable object on the way in and on the way out.

    IntermediateQuick answer1 min read
  23. How do you make a Singleton thread-safe in Java?

    Use eager initialisation, the holder class idiom, or an enum. If you need lazy creation with double-checked locking, the field must be volatile.

    IntermediateQuick answer1 min read
  24. What happens in memory when you write new Student("Asha") in Java?

    new allocates memory for the object on the heap, sets fields to default values, runs the constructor, and returns a reference. The variable on the stack holds only that reference.

    IntermediateQuick answer1 min read
  25. What is dynamic method dispatch in Java?

    It is the way Java chooses which overridden method to run at runtime, based on the actual object type and not on the type of the reference variable.

    IntermediateQuick answer1 min read
  26. What is the difference between protected and default (package-private) access in Java?

    Default access lets only classes in the same package use the member. Protected allows the same package plus subclasses in any package, but through inheritance.

    IntermediateQuick answer1 min read
  27. When does an object become eligible for garbage collection in Java?

    An object becomes eligible when no live thread can reach it through any chain of references. The JVM then frees its memory at a time of its own choosing.

    IntermediateQuick answer1 min read
  28. Why does Java not allow a class to extend two classes?

    To avoid the diamond problem: if two parents define the same method and a child inherits both, it is unclear which version should run. Java blocks it and lets you combine interfaces instead.

    IntermediateQuick answer1 min read
  29. Predict the output: a parent reference holds a child object that hides the parent's field. Which value do you get?

    Methods are chosen by the object's real type, but fields are chosen by the reference type. So p.name gives the parent's value while p.get() runs the child's method.

    AdvancedQuick answer1 min read
  30. What is a covariant return type in method overriding?

    An overriding method may return a more specific type (a subclass) than the parent's method declared. Callers using the child type then avoid casting.

    AdvancedQuick answer1 min read