Java OOPs Interview Questions and Answers
Ques 11. What is the purpose of the 'static' keyword in Java?
The 'static' keyword is used to create class-level variables and methods. These belong to the class rather than instances of the class.
Example:
static int count = 0;
static void increment() { count++; }
Ques 12. Explain the concept of composition in Java.
Composition involves creating complex objects by combining simpler objects. It allows for better code organization and reusability.
Example:
class Car {
Engine engine;
// other properties and methods
}
Ques 13. What is the purpose of the 'interface' in Java?
An interface in Java is a collection of abstract methods that defines a contract for classes to implement. It supports multiple inheritances and is often used for achieving abstraction.
Example:
interface Printable { void print(); }
Ques 14. Explain the 'diamond problem' and how Java solves it.
The 'diamond problem' occurs in multiple inheritance when a class inherits from two classes that have a common ancestor. In Java, this is avoided by allowing a class to implement multiple interfaces.
Ques 15. What is the purpose of the 'final' method in Java?
A 'final' method in Java cannot be overridden by subclasses. It is used to prevent modification of a method in a subclass.
Example:
class Parent {
final void display() { /* method implementation */ }
}
Most helpful rated by users: