Oop Questions
Oop Questions
def drive(self):
return f"The {self.make} {self.model} is driving"
// Abstraction
public abstract class Vehicle {
// Abstract method - implementation details hidden
public abstract void accelerate();
// Concrete method
public void stop() {
System.out.println("Vehicle is stopping");
}
}
Question 5: Explain Inheritance?
Inheritance is a mechanism where a new class (subclass/derived class) can reuse,
extend, or modify the attributes and behaviors of an existing class
(superclass/base class). It promotes code reuse and establishes an "is-a"
relationship.
Example:
java// Parent/Base class
class Animal {
protected String name;
// Usage
Dog dog = new Dog("Rex");
dog.eat(); // Inherited method
dog.bark(); // Dog-specific method
Question 6: Explain virtual keyword?
The virtual keyword is used primarily in languages like C++ and C# to indicate that
a method can be overridden in derived classes. It enables runtime polymorphism
through method overriding.
Example (C#):
csharpclass Base {
public virtual void Display() {
Console.WriteLine("Display method from Base class");
}
}
class Derived : Base {
public override void Display() {
Console.WriteLine("Display method from Derived class");
}
}
// Usage
Base obj1 = new Base();
Base obj2 = new Derived();
Overriding:
Example:
javaclass Shape {
public void draw() {
System.out.println("Drawing a shape");
}
}
// Polymorphic usage
Shape shape1 = new Circle();
Shape shape2 = new Rectangle();
Resolved at runtime
Method overriding is an example
More flexible but slightly slower
Enables more extensible code
Example:
javaclass Calculator {
// Static polymorphism (overloading)
public int add(int a, int b) {
return a + b;
}
class Animal {
public void makeSound() {
System.out.println("Animal makes a sound");
}
}
// Overload + operator
Complex operator+(const Complex& obj) {
Complex result;
result.real = this->real + obj.real;
result.imag = this->imag + obj.imag;
return result;
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
// Usage
Complex c1(3.0, 2.0);
Complex c2(1.0, 4.0);
Complex c3 = c1 + c2; // Using overloaded + operator
c3.display(); // Output: 4 + 6i
Question 14: How to do custom operator overloading?
The approach varies by language. Here's how to overload operators in Python and C+
+:
Example (Python):
pythonclass Vector:
def __init__(self, x, y):
self.x = x
self.y = y
# Overload + operator
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
# String representation
def __str__(self):
return f"Vector({self.x}, {self.y})"
# Usage
v1 = Vector(2, 3)
v2 = Vector(5, 1)
v3 = v1 + v2
v4 = v1 * 3
Example:
javaabstract class DatabaseConnection {
// Common functionality
public void connect() {
System.out.println("Establishing connection...");
}
@Override
public void drive() {
System.out.println("Driving " + brand + " car");
}
}
// This is valid:
Vehicle v = new Car("Toyota");
v.drive(); // Output: Driving Toyota car
Question 18: Is it compulsory to implement Abstract methods?
Yes, it is compulsory for non-abstract subclasses to implement all abstract methods
inherited from their abstract parent classes or interfaces. If a subclass doesn't
implement all abstract methods, it must also be declared abstract.
Example:
javaabstract class Animal {
public abstract void makeSound();
public abstract void move();
}