OOPS Cheat Sheet
OOPS Cheat Sheet
1. Compile-time polymorphism (method overloading): Methods have the same name but different
parameters.
2. Runtime polymorphism (method overriding): A subclass provides a specific implementation of a
method that is already defined in its superclass.
What are access specifiers? Explain the difference between private, protected, and public.
Ans : Access specifiers define the visibility and accessibility of classes, methods, and attributes:
What is method overloading and method overriding? How are they different?
Ans : Method overloading is defining multiple methods with the same name but different parameters
in the same class. Method overriding is when a subclass provides a specific implementation of a
method that is already defined in its superclass.
What are abstract classes? How are they different from interfaces?\
Ans : An abstract class can have both abstract methods (without implementation) and concrete
methods. An interface only has abstract methods (with some exceptions like default methods in Java).
A class can inherit only one abstract class but can implement multiple interfaces.
What is a static method and how does it differ from an instance method?
Ans : A static method belongs to the class rather than any object of the class and can be called
without creating an instance of the class. An instance method requires an object of the class to be
invoked and can access instance variables.
What is the difference between composition and inheritance? When would you use one over the
other?
Ans : Inheritance is a "is-a" relationship (e.g., a Dog is an Animal), while composition is a "has-a"
relationship (e.g., a Car has an Engine). Composition is often preferred when you want to use
functionalities from different classes without the tight coupling of inheritance.
What are the differences between abstract classes and interfaces in [specific language]?
Ans : In Java, an abstract class can have a mixture of abstract and concrete methods, whereas an
interface (prior to Java 8) only had abstract methods. Interfaces are used to implement multiple
inheritance, whereas a class can extend only one abstract class.
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
Animal myDog = new Dog();
myDog.sound(); // Outputs: Bark
What is the role of a destructor in OOP, and how is it implemented in [specific language]?
Ans : In C++, a destructor is a special method that is automatically called when an object goes out of
scope or is deleted. It is used to release resources like memory or file handles. In Java, destructors are
not used; instead, the garbage collector handles memory management.