Java Basics 4
Is multiple inheritance possible in java and why?
In Java, multiple inheritance is not directly possible because a class cannot inherit from more than one class. This is because a class can only have one direct superclass, and any additional inheritance would result in ambiguity when trying to resolve method calls or field access.
However, Java does provide a way to achieve similar functionality through the use of interfaces. A class can implement multiple interfaces, and each interface can define its own set of methods and fields. This allows a class to inherit the behavior of multiple interfaces, but it does not have the same issues of ambiguity that multiple class inheritance would have.
This design decision was taken to solve the problem of Diamond problem which is a common problem of multiple inheritance in other languages. The diamond problem occurs when a class inherits from two classes that have a common superclass, leading to an ambiguity of which superclass to inherit from.
In summary, multiple inheritance is not possible in Java through classes, but it is possible through interfaces, which allows a class to inherit the behavior of multiple interfaces and avoid the problem of ambiguity.
Can we implement multiple interface?
Yes. A class can implement any number of interfaces. In this case, there is no ambiguity even though both interfaces are having the same method. Why? Because methods in an interface are always abstract by default, which doesn’t let them give their implementation (or method definition ) in the interface itself.
Explain Method overloading and overriding with example.
Method overloading and overriding are two related concepts in object-oriented programming that allow a class to provide multiple methods with the same name but with different signatures or implementations.
Method overloading is the ability of a class to have two or more methods with the same name but with different parameters. The number of parameters or the type of parameters can be different. The compiler differentiates these methods by the number, types and order of the parameters.
For example:
class MyClass {
void myMethod(int a) {
// implementation
}
void myMethod(int a, int b) {
// implementation
}
}Method overriding, on the other hand, is the ability of a subclass to provide a new implementation for a method that is already provided by its superclass. The method in the subclass must have the same name, return type and parameters as the method in the superclass.
For example:
class Vehicle {
void startEngine() {
// implementation
}
}
class Car extends Vehicle {
@Override
void startEngine() {
// new implementation
}
}It is important to note that the overriding method cannot have a more restrictive access modifier than the method being overridden. Also, a method declared as final or static cannot be overridden. Additionally, when a method is overridden, the overriding method should use the @Override annotation to indicate that it is intended to override a method from the superclass.

In summary, Method overloading allows a class to have multiple methods with the same name but different parameters, while method overriding allows a subclass to provide a new implementation for a method that is already provided by its superclass. Both concepts allow for more flexibility and code reuse.