OOP concepts interview questions

Classes, objects, the four pillars, overloading versus overriding, abstract classes and interfaces.

1. What is object-oriented programming?

A way of writing programs around objects, which bundle data (fields) and behaviour (methods). It helps model real things, reuse code and keep large programs organised. Java, C++ and Python all support it.

Asked in: Wipro Elite NTH

2. What is the difference between a class and an object?

A class is a blueprint that describes fields and methods; an object is an instance created from that blueprint, with its own values. One Car class can create many car objects.

3. What are the four pillars of OOP?

Encapsulation (keeping data and the methods that use it together, and hiding the data), abstraction (showing only what is needed and hiding details), inheritance (a class reusing another class's members) and polymorphism (one name, many forms, such as overloading and overriding). Be ready to give a small code example of each.

Asked in: TCS NQT, Infosys, Cognizant GenC, Capgemini, HCLTech, LTIMindtree

4. What is the difference between encapsulation and abstraction?

Encapsulation is about hiding data inside a class and controlling access through methods (private fields with getters and setters). Abstraction is about hiding complexity and exposing only the essential operations (an interface or abstract class). Encapsulation is how; abstraction is what.

Asked in: Wipro Elite NTH

5. What is polymorphism? Overloading versus overriding?

Polymorphism lets the same call behave differently. Overloading (compile-time) means several methods with the same name but different parameters in one class. Overriding (run-time) means a subclass gives its own version of a parent method with the same signature, chosen based on the actual object.

Asked in: Deloitte

6. What is inheritance and what types are there?

Inheritance lets a class acquire the fields and methods of another. Types: single, multilevel, hierarchical, multiple and hybrid. Java does not allow multiple inheritance of classes (it uses interfaces instead); C++ does.

7. What is the difference between an abstract class and an interface?

An abstract class can have fields, constructors and both abstract and normal methods, and a class can extend only one. An interface defines a contract of methods (in Java it can also have default and static methods but no instance state), and a class can implement many. Use an interface for a capability and an abstract class for shared base code.

8. What is a constructor? Can it be overloaded?

A special method with the class name that runs when an object is created, used to initialise fields. It has no return type. Yes, constructors can be overloaded with different parameter lists.

9. What are access modifiers?

Keywords that control visibility: public (everywhere), private (only inside the class), protected (the class and its subclasses, plus the package in Java) and default/package-private in Java (only the same package).

Asked in: Tech Mahindra

10. What is a singleton class?

A class that allows only one object. Make the constructor private, keep a static instance and return it from a static method such as getInstance(). Used for things like a configuration manager or a logger.

Asked in: Tech Mahindra

11. What do this and super mean?

this refers to the current object (to access its fields or call another constructor). super refers to the parent class (to call the parent constructor or an overridden parent method).

All interview preparation