Govt. Exams
Entrance Exams
Method overloading requires methods to have the same name but different parameter lists (different types, number, or order of parameters). Return type alone cannot distinguish overloaded methods.
Abstract classes cannot be instantiated but can contain both abstract methods (without implementation) and concrete methods (with implementation). This allows providing default behavior while enforcing certain methods to be implemented by subclasses.
Polymorphism allows objects to be treated as instances of their parent class and to call methods that are overridden in child classes. It includes method overloading and method overriding.
'this' is a reference to the current object instance, while 'super' is a reference to the parent class object. They are used to access members of current and parent classes respectively.
Constructor overloading is allowed in Java. A class can have multiple constructors with different parameter lists. Constructors don't have return types and can have any access modifier.
The 'instanceof' operator is used to test if an object is an instance of a specified class, subclass, or interface. It returns a boolean value.
Method overriding occurs when a child class provides a specific implementation of a method already defined in the parent class with the same signature. The @Override annotation is optional but recommended.
Java supports single inheritance for classes but a class can implement multiple interfaces. An interface can extend multiple interfaces through multiple inheritance.
The 'instanceof' operator checks whether an object is an instance of a specific class or implements a specific interface.
class A {
A() {
System.out.println("A");
}
}
class B extends A {
B() {
super();
System.out.println("B");
}
}
public class Test {
public static void main(String[] args) {
new B();
}
}
super() calls parent constructor first. Parent constructor A prints 'A', then child constructor B prints 'B'.