Oops
Oops
Types of inheritance:
4. What is the difference between a class and an object? Single: One child inherits from one parent.
Class: Blueprint, design, or template. It doesn’t occupy memory. Multilevel: Child inherits from parent, and that parent from
Example: Car design. another.
Object: Actual instance of the class. It occupies memory. Hierarchical: Multiple children inherit from one parent.
Example: Your red Maruti Swift car.
Multiple (C++ supports, Java doesn’t): Child inherits from
Class describes the structure and behavior, while an object is a
multiple parents.
real entity having those characteristics. You can create
multiple objects from one class, just like multiple cars from Hybrid: Combination of two or more inheritance types.
one car design.
8. What is Polymorphism?
5. Explain Encapsulation with a real-world example.
Polymorphism means "many forms." It allows a single action to behave
Encapsulation means hiding the internal data of a class and allowing differently in different situations.
access only through methods. It protects data from being modified Example: A print() function might print integers, strings, or
directly. floating numbers differently but uses the same method name.
For example, in a Bank Account, you don’t access the balance
There are two types:
directly. You use methods like deposit() and withdraw() which follow
some rules. Compile-time (Static) Polymorphism: Method overloading, operator
In programming, you make variables private and allow access overloading.
through public getter and setter methods. This way, you control how
the data is accessed and changed. Run-time (Dynamic) Polymorphism: Method overriding using
inheritance.
Inheritance means a class (child class) can reuse the properties and
methods of another class (parent class). It reduces redundancy.
10. Explain the difference between Compile-time Polymorphism and Run- A destructor is a special method that destroys or cleans up an
time Polymorphism. object when it is no longer needed.
Compile-time Polymorphism: The method call is resolved at In C++, it starts with ~ symbol and has no return type and no
compile time. Example: Method Overloading and Operator arguments. Example: ~Car() {}
Overloading.
In Java, there are no destructors, but the Garbage
Run-time Polymorphism: The method call is resolved during Collector automatically destroys unused objects.
program execution. Example: Method Overriding in inheritance.
Destructors are used to release memory or close connections when an
Compile-time is faster but less flexible, while run-time allows
object is destroyed.
dynamic method behavior based on the object type.
A constructor is a special method used to create and initialize Feature Constructor Method
objects of a class. It has the same name as the class and does not
have a return type, not even void. Defines an object's
Example: Purpose Initializes an object behavior
class Car { public: Car() { cout << "Car created"; } };
Name Same as the class name Any valid name
When you create an object of Car, the constructor is called
automatically.
Its main purpose is to assign default values to the variables of the Return
object or run setup code when an object is created. type No return type Has a return type
No, constructors cannot be inherited. 20. What is the use of friend function in C++?
Each class defines its own constructors. But a child class can call
A friend function is a function that is not a member of a class but
the parent class’s constructor using the super() keyword in Java or
can access its private and protected data.
an initializer list in C++.
Syntax:
So, while inheritance applies to properties and methods, constructors
are not inherited but can be called. class Car { friend void display(Car obj); };
2. Access the parent class’s variables and methods that are hidden 4. Multiple Inheritance (C++ supports it): One child inherits from
by child class. multiple parents.
Example in Java: Example: Class D inherits from Class A and Class B.
29. What is operator overloading in C++? Static Binding: The method call is decided at compile time.
Example: method overloading.
Operator overloading means giving special meaning to operators like
+, -, ==, etc., for user-defined objects. Dynamic Binding: The method call is decided at runtime,
Example: depending on the object type. Example: method overriding.
This allows adding two complex numbers using the + operator. Animal a = new Dog(); a.sound(); // Dynamic binding: decides at
It helps objects behave more like built-in types. runtime which sound() to call
When you call sound() on a Dog object, it prints "Bark", not "Animal Animal animal = new Dog(); animal.sound();
sound."
At runtime, it binds the Dog's sound() method, not Animal's.
This is an example of runtime polymorphism, where the method behavior
Late binding is essential for polymorphism, especially in large
depends on the object type.
systems where object types may not be known until runtime.
Here, calling Child.display() does not override but 35. What are access modifiers? List them.
hides Parent.display().
Access modifiers control where a class, variable, or method can be Abstraction is about what to hide, encapsulation is about how to
accessed from. protect data.
In Java, they are:
Public is open to all, private is strictly for the class itself, and Can have both abstract & Only abstract methods (Java
protected allows inheritance. Methods non-abstract methods 7), default/static (Java 8+)
Purpose Partial abstraction Full abstraction This lets developers add new methods to interfaces without forcing
every implementing class to override them.
When classes are closely When unrelated classes need
Usage related to share behavior
44. Can a class implement multiple interfaces?
Example: Use an abstract class when classes share common behavior, Yes, a class can implement multiple interfaces in Java. This is how
and use interfaces when different classes need to follow the same Java supports multiple inheritance of type.
contract. Example:
41. Can we instantiate an abstract class? The class must implement all the methods declared in the interfaces.
This allows adding functionalities from many different sources.
No, we cannot create an object of an abstract class because an
abstract class is incomplete. It may contain abstract methods without
a body. 45. What happens if a class does not implement all methods of an
However, we can create a reference of an abstract class and assign it interface?
to a child class object.
Example in Java: If a class does not implement all the methods of an interface, then
that class must be declared as abstract.
AbstractAnimal animal = new Dog(); Example:
Abstract classes are meant to be extended by child classes where the abstract class Animal implements Walkable { // Walkable's method is
abstract methods are implemented. not implemented }
No, interfaces cannot have constructors because they are not meant to
create objects. 46. What is an inner class?
Interfaces only declare method signatures, not implementations.
Constructors are used to initialize objects, and since you can’t An inner class is a class defined inside another class.
create an object of an interface, constructors are not allowed in it. It helps logically group classes that are only used by their outer
class.
Example:
43. What is the default method in an interface? class Outer { class Inner { void display() {
In Java 8 and later, a default method is a method in an interface System.out.println("Inner class"); } } }
that has a body (implementation). It helps in organizing code and increasing encapsulation.
It helps interfaces evolve without breaking the classes that
implement them.
Example: 47. Difference between nested class and inner class.
A nested class is any class inside another class. No, you cannot inherit a final class.
The whole purpose of declaring a class as final is to prevent other
If it's static → Static nested class.
classes from extending it.
If it's non-static → Inner class. For example, in Java, the String class is final, so no one can modify
its behavior through inheritance.
Feature Static Nested Class Inner Class
Access to outer Cannot access non-static Can access outer class An abstract class means it’s incomplete and must be extended to
class members members provide full implementation.
Runnable r = new Runnable() { public void run() { A static nested class does not require an object of the outer
System.out.println("Running"); } }; class to be created.
However, you cannot declare a top-level class static because it does
It simplifies coding when you need a short, one-time-use class. not belong to another class.
49. Explain the use of final keyword in class. 53. What is UML in OOPS?
When you declare a class as final, it cannot be inherited. UML (Unified Modeling Language) is a visual modeling language used to
Example: represent the structure and behavior of an object-oriented system.
final class Animal { ... } In OOPS, UML helps developers to:
This is useful when you want to prevent modification or extension of Design class diagrams to show classes and relationships.
a class’s behavior, ensuring security and stability. Visualize how objects interact.
54. Explain the concept of Association. Child lifecycle Can exist without parent Dies with the parent
55. Explain Aggregation with an example. Dependency is a relationship where one class depends on another to
perform its task.
Aggregation is a special type of Association where one class is Example: A Printer class depends on a Paper class. Without paper,
a part of another but can exist independently. printing doesn’t happen.
Example: It is the loosest relationship, meaning the dependent class uses the
A Library and Books – Even if the library closes, the books still other class temporarily during a method execution.
exist. Dependency is represented in UML using a dashed arrow.
In code:
Composition is a stronger form of Aggregation. If the parent object Employee e2 = (Employee) e1.clone();
is destroyed, the child object is also destroyed. Cloning is useful when you want identical objects with separate
Example: memory locations.
A House and Rooms – If the house is destroyed, the rooms no longer
exist.
In code: 60. What is shallow copy and deep copy?
class House { private Room room = new Room(); } Type Explanation Example
It tightly binds the child object’s lifecycle to the parent object.
Shallow
Copies the object's fields, but if Cloning an object
Copy
57. Difference between aggregation and composition. there are references to other that contains another
Type Explanation Example 63. Difference between stack and heap memory for objects.
Shallow copy is faster but may cause unexpected issues if nested Access
objects are modified. Deep copy is safer but takes more time and speed Faster Slower
memory.
Garbage Collection (GC) in Java is the process by which unused or In OOPS, objects are always created in the heap, while reference
unreachable objects are automatically removed from memory to free up variables can be on the stack.
space.
Java has a built-in garbage collector that runs in the background and
checks for objects that are no longer referenced by any part of the 64. Write a program to show inheritance in C++.
program. #include <iostream> using namespace std; class Animal { public: void
This makes Java a memory-managed language, preventing memory leaks. eat() { cout << "Eating..." << endl; } }; class Dog : public Animal {
Example: If an object is no longer used in your code, the garbage public: void bark() { cout << "Barking..." << endl; } }; int main() {
collector removes it from the heap memory automatically. Dog d; d.eat(); // Inherited from Animal d.bark(); // Own method
return 0; }
The finalize() method is a protected method of the Object class that 65. Write a program for method overriding.
Java calls before garbage collecting an object. Example in Java:
You can override finalize() to perform cleanup tasks like closing
files or releasing resources. class Animal { void sound() { System.out.println("Animal makes
Example: sound"); } } class Dog extends Animal { void sound() {
System.out.println("Dog barks"); } } public class Main { public
protected void finalize() throws Throwable { static void main(String[] args) { Animal a = new Dog(); a.sound(); //
System.out.println("Object is being destroyed"); } Output: Dog barks } }
Note: As of Java 9+, the use of finalize() is discouraged because
it can cause performance problems. Instead, use try-with-resources or
explicit cleanup. 66. Create a program using abstract class and interface.
Classes represent business models like Customer, Account, or 72. Difference between method hiding and overriding.
Order.
Feature Method Overriding Method Hiding
Inheritance allows reusing code efficiently.
Polymorphism lets objects behave differently based on their Method type Instance methods Static methods
types.
class Car { Car() { } Car(String color) { } Car(String color, int Usage Complex data and functions Simple data grouping
speed) { } }
It provides flexibility in creating objects in different ways. In modern C++, structures can also have functions, but traditionally,
they were used only to store data.
Feature Overloaded Constructors Overloaded Methods Yes, in modern C++, you can define member functions inside a
structure, just like in a class.
Example:
Initialize objects in Perform different tasks using
Purpose different ways same method name struct Point { int x, y; void display() { cout << x << "," << y; } };
Type casting is converting one data type or object type into another.
Strong "has- In OOPS, it mainly refers to converting parent class references to
Composition House and Rooms a" Rooms die with house child class references (downcasting) or vice versa (upcasting).
It helps in writing flexible and reusable code but should be done
Association is general linking, aggregation is weak ownership, and carefully to avoid errors.
composition is strong ownership.
Multilevel inheritance means a class inherits from a class, which Class type Concrete or abstract Abstract
itself inherits from another class.
Example:
class Base { public: virtual void show() { cout << "Base"; } }; 91. What are class templates in C++?
Used for runtime polymorphism. Class templates allow you to create a single class that works with
any data type.
Example:
88. What is pure virtual function in C++? template <class T> class Box { T data; };
A pure virtual function has no body in the base class and must Instead of creating separate classes for int, float, etc., you use
be overridden by derived classes. templates.
It is written like this: They promote code reusability and type safety.
virtual void show() = 0;
If a class contains a pure virtual function, the class 92. Difference between function templates and class templates.
becomes abstract and cannot be instantiated.
Feature Function Template Class Template
89. What is the difference between virtual function and pure virtual Purpose Works for functions Works for classes
function?
Feature Virtual Function Pure Virtual Function Syntax template <typename T> template <typename T>
Feature Function Template Class Template class Singleton { private static Singleton instance = new
Singleton(); private Singleton() { } public static Singleton
getInstance() { return instance; } }
Example add<int>(a, b) Box<int> myBox;
Both support generic programming, but one is for functions, the other 96. What is an immutable class?
for entire classes.
An immutable class means its objects cannot be changed once created.
Example: Java's String class is immutable.
Key features:
93. Explain friend class in C++.
Class marked final
A friend class is allowed to access private and protected members of
another class. Private fields
Example:
No setter methods
class B; class A { friend class B; };
Values set through the constructor
Used when two classes are closely related and need to share internal
data.
It breaks strict encapsulation, so use carefully. 97. Difference between mutable and immutable objects.
In C++/Java, private constructors are used to control object Example ArrayList, StringBuilder String, Integer
creation, such as in Singleton design patterns.
Example in Java:
Use case Frequent modifications Security, thread safety
private MyClass() { }
The class then controls when and how objects are created.