AMCAT OOP Concepts 2 — Questions and Answers
Question 1: Which OOP principle allows a class to have multiple methods with the same name but different parameters?
- Encapsulation
- Inheritance
- Method Overloading (Correct answer)
- Abstraction
Correct answer: Method Overloading
Method overloading is compile-time polymorphism where multiple methods share the same name but differ in parameter list.
Method overloading: same method name, different parameter lists. Example: add(int a, int b) and add(double a, double b) coexist. The compiler selects the correct version based on argument types.
Question 2: In Java, when you call an overridden method through a base-class reference, which method is executed?
- Base class method
- Derived class method (Correct answer)
- Compilation error occurs
- Runtime error occurs
Correct answer: Derived class method
Java uses dynamic dispatch — the actual object type determines which method runs at runtime.
Dynamic polymorphism (late binding): Animal a = new Dog(); a.sound(); calls Dog's sound() method. JVM resolves the method at runtime based on the actual object type, not the reference type.
Question 3: Which access modifier makes a class member accessible only within the same class?
- public
- protected
- default
- private (Correct answer)
Correct answer: private
private restricts access to within the declaring class only.
Access levels: private (class only) < default/package-private (same package) < protected (same package + subclasses) < public (everywhere). private enforces strong encapsulation.
Question 4: In Java, what is the purpose of the 'super' keyword?
- To create a new object
- To call the parent class constructor or method (Correct answer)
- To declare an abstract method
- To implement an interface
Correct answer: To call the parent class constructor or method
'super' refers to the immediate parent class. It calls the parent's constructor (super()) or accesses the parent's method (super.method()).
super() in a constructor calls the parent class constructor. super.methodName() calls the parent's version of an overridden method. super() must be the first statement when calling parent constructor.
Question 5: What is an abstract class?
- A class with no methods
- A class that cannot be instantiated and may have abstract methods (Correct answer)
- A class with only static methods
- A class that implements all interface methods
Correct answer: A class that cannot be instantiated and may have abstract methods
An abstract class cannot be instantiated. It may have abstract methods (no body) that subclasses must implement.
Abstract classes serve as templates. They can have abstract methods that subclasses must override, as well as concrete methods and fields. You cannot do: AbstractClass obj = new AbstractClass().
Question 6: What is the key difference between an interface and an abstract class in Java?
- Interfaces can have constructors
- A class can implement multiple interfaces but extend only one abstract class (Correct answer)
- Abstract classes cannot have concrete methods
- Interfaces can have instance variables
Correct answer: A class can implement multiple interfaces but extend only one abstract class
Java supports multiple interface implementation but single class inheritance.
Key difference: A class can implement multiple interfaces but can only extend one class. Interfaces (pre-Java 8) had only abstract methods; abstract classes can have both. Interfaces have no constructors.
Which OOP principle allows a class to have multiple methods with the same name but different parameters?