OOP is a programming paradigm that uses objects - instances of classes - to design applications and software. These objects represent real-world entities, having both properties (attributes) and behaviors (methods), and interact with one another to create robust systems. During tech interviews, questions on OOP evaluate a candidate’s grasp of concepts like encapsulation, inheritance, polymorphism, and abstraction, and their ability to effectively design and structure a software system using these principles.
OOP Fundamentals
- 1.
What is Object-Oriented Programming (OOP)?
Answer:Object-Oriented Programming is a programming paradigm that organizes code into self-contained objects.
Each object combines data and the methods that operate on that data, resulting in more modular, flexible, and reusable code.
Core Concepts
Classes and Objects
A class serves as a blueprint for creating objects, defining their attributes and methods.
An object represents an instance of a class. It contains both data (attributes of the class) and the methods designed to manipulate that data.
Encapsulation
Encapsulation refers to the bundling of data and related methods within a class, keeping them hidden to protect their integrity.
External code accesses them through public methods, often referred to as getters (to read the attributes) and setters (to change them).
Inheritance
Inheritance allows for the creation of new classes that inherit properties and methods of existing classes. It promotes code reuse and establishes a hierarchical relationship.
The class being inherited from is the base class or superclass, while the class inheriting is the derived class or subclass.
Polymorphism
Polymorphism allows objects of derived classes (subclasses) to be treated as objects of their base class (the superclass) but still maintain their unique behaviors.
This enables objects to respond uniquely to the same method call, especially when various classes share a method name but have differing implementations.
- Compile-Time Polymorphism: Determined before the code runs, typically using method overloading.
- Run-Time Polymorphism: Determined while the code is running, typically through method overriding.
Abstraction
Abstraction is all about creating a simple interface that exposes only relevant and necessary functionalities.
- Abstract Classes: Generalized classes, therefore not instantiated directly. They serve as base classes for derived ones.
- Interfaces: Set guidelines for classes, specifying which methods they must implement.
Associations
Objects often have relationships with other objects, such as one object using or being composed of others. Such relationships are defined as associations.
- Aggregation: One object owns or contains the other, indicated by a “has-a” relationship.
- Composition: A stronger form of aggregation, where the child cannot exist without the parent.
Benefits of OOP
- Modularity: Objects are organized into standalone entities and communicate through public interfaces, promoting code separation and easier maintenance.
- Reusability: Objects and class hierarchies can be reused in different projects. Additionally, inheritance allows classes to acquire attributes and behaviors from parent classes.
- Extensibility: New features can be incorporated through class inheritance and interface implementation.
- Flexibility: Polymorphism enables objects to adapt their behavior based on context.
Code Example: OOP
Here is the Java code:
// Abstract base class abstract class Animal { private String name; public Animal(String name) { this.name = name; } public String getName() { return name; } // Abstract method public abstract void makeSound(); public void sleep() { System.out.println(name + " is sleeping."); } } // Derived class class Lion extends Animal { public Lion(String name) { super(name); } @Override public void makeSound() { System.out.println("Roar!"); } } // Derived class class Parrot extends Animal { private String color; public Parrot(String name, String color) { super(name); this.color = color; } public String getColor() { return color; } @Override public void makeSound() { System.out.println("Squawk!"); } } public class Zoo { public static void main(String[] args) { Lion simba = new Lion("Simba"); Parrot rio = new Parrot("Rio", "Blue"); System.out.println(simba.getName() + " is a Lion."); simba.makeSound(); simba.sleep(); System.out.println(rio.getName() + " is a " + rio.getColor() + " Parrot."); rio.makeSound(); rio.sleep(); } }In the example:
- Classes and Objects:
Animalis a class, whilesimbaandrioare objects. - Encapsulation: Animal’s
nameattribute and Parrot’scolorattribute are encapsulated with private visibility, and public getter methods are provided. - Inheritance: Both
LionandParrotclasses inherit from theAnimalclass. - Polymorphism: Both
LionandParrotprovide their own implementation of themakeSound()method, even though they are treated asAnimalobjects. - Abstraction: The
Animalclass contains the abstract methodmakeSound(), ensuring derived classes provide their own implementation.
- 2.
What is the difference between procedural and Object-Oriented programming?
Answer: - 3.
What is encapsulation?
Answer: - 4.
What is polymorphism? Explain overriding and overloading.
Answer: - 5.
What is inheritance? Name some types of inheritance.
Answer: - 6.
What is an abstraction? Name some abstraction techniques.
Answer: - 7.
What is a class in OOP?
Answer: - 8.
What is an object in OOP?
Answer: - 9.
How do access specifiers work and what are they typically?
Answer: - 10.
Name some ways to overload a method.
Answer: - 11.
What is cohesion in OOP?
Answer: - 12.
What is coupling in OOP?
Answer:
Class Relationships and Design
- 13.
What is a constructor and how is it used?
Answer: - 14.
Describe the concept of destructor or finalizer in OOP.
Answer: - 15.
Compare inheritance vs. mixin vs. composition.
Answer: