Java OOPs Interview Questions and Answers
Ques 16. Explain the concept of method overloading in Java.
Method overloading allows a class to have multiple methods with the same name but different parameters. It improves code readability and flexibility.
Example:
int add(int a, int b) {
/* method implementation */
}
double add(double a, double b) { /* method implementation */ }
Ques 17. What is the 'instanceof' operator used for?
The 'instanceof' operator in Java is used to test if an object is an instance of a particular class or interface. It returns true if the object is an instance; otherwise, it returns false.
Example:
if (obj instanceof MyClass) { /* do something */ }
Ques 18. Explain the concept of method overriding in Java.
Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. It allows for runtime polymorphism.
Example:
class Animal { void makeSound() { /* method implementation */ } }
class Dog extends Animal { void makeSound() { /* overridden method implementation */ } }
Ques 19. What is a constructor in Java?
A constructor in Java is a special method used to initialize objects. It has the same name as the class and is called when an object is created.
Example:
class Car { Car() { /* constructor code */ } }
Ques 20. Explain the 'super()' statement in a constructor.
The 'super()' statement is used to invoke the constructor of the superclass. It must be the first statement in the subclass constructor if used.
Example:
class SubClass extends SuperClass { SubClass() { super(); /* other constructor code */ } }
Most helpful rated by users: