Method Overloading and Overriding Flashcards
What is the other name of Method
Overloading?
Method Overloading is also known as Static Polymorphism
What kinds of argument variations are allowed in Method Overloading?
Method Overloading allows two methods with same name to differ in: 1. Number of parameters 2. Data type of parameters 3. Sequence of data type of parameters
. Is it allowed to overload main()
method in Java?
Yes, Java allows users to create many methods with same name ‘main’. But only public static void main(String[] args) method is used for execution.
Why Java does not allow overriding a static method?
To override a method, you need an instance of a class. Static method is not associated with any instance of the class. So the concept of
overriding does not apply here.
What is the other name for method overriding?
Runtime Polymorphism
What is the difference between method overloading and method overriding in Java?
Method overloading occurs within the same class. Method overriding happens in two classes with hierarchy relationship.
Parameters must be different in method overloading. Parameters must be same in method overriding.
Method overloading is a compile time concept. Method overriding is a runtime concept.
What is virtual function?
A virtual function or virtual method in an OOP language is a function or method used to override the behavior of the function in an inherited class with the same signature to achieve the polymorphism
What is meant by covariant return
type in Java?
Let say class B is child of class A. There is a get() method in classA as well as class B. get() method of class A can return an instance of A, and get() method of class B return an instance of B. Here class B overrides get() method, but the return type is different. Before Java 5, any method that overrides the method of parent class would have same return type.
From Java 5 onwards, a child class can override a method of parent class and the child class method can return an object that is child of object return by parent class method.