Interview Q&A / Object-Oriented Programming
Object-Oriented Programming interview questions
The four pillars, interfaces, abstract classes and design basics.
Prefer rapid revision? Practise Object-Oriented Programming short interview answers →
- 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 - What is the difference between composition and inheritance, and when would you prefer composition?
Inheritance is an "is-a" relationship where a class extends another and reuses its code. Composition is a "has-a" relationship where a class holds other objects and delegates work to them. Prefer composition by default because it keeps classes loosely coupled and lets you swap behaviour, and use inheritance only for a true, stable "is-a" relationship.
IntermediateIn-depth3 min read - 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 - 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 - 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 - 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 - 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 - Why does Square extending Rectangle violate the Liskov Substitution Principle?
A Square must keep width and height equal, so setting one changes the other. Code written for any Rectangle, expecting independent sides, gets wrong results when given a Square.
IntermediateQuick answer1 min read - What is the method resolution order (MRO) in Python?
MRO is the order in which Python searches a class and its parents when you call a method or read an attribute. Python builds it with the C3 rule, and you can see it in ClassName.__mro__.
AdvancedQuick answer1 min read