Core Concept on Java polymorphism

Following are some important points for method overriding and static methods in Java.
1) For class (or static) methpolymorphismods, the method according to the type of reference is called, not according to the object being referred, which means method call is decided at compile time.
2) For instance (or non-static) methods, the method is called according to the type of object being referred, not according to the type of reference, which means method calls is decided at run time.
3) An instance method cannot override a static method, and a static method cannot hide an instance method. For example, the following program has two compiler errors.
4) In a subclass (or Derived Class), we can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass methods — they are new methods, unique to the subclass.

1. Can we override the static method?

No, a static method cannot be overridden. It can be proved by the runtime, so we will learn it later.

2. Why can we not override static method?
It is because the static method is bound with class whereas the instance method is bound with an object. Static belongs to the class area, and an instance belongs to the heap area.

3. Can we override the java main method?

No, because the main is a static method.






4. Can we overload methods that differ only by static keyword?

We cannot overload two methods in Java if they differ only by static keyword (number of parameters and types of parameters is the same). See the following Java program for example. This behaviour is the same in C++ (See point 2 of this).
filter_none
edit
play_arrow
brightness_4

// filename Test.java
public class Test {
    public static void foo() {
        System.out.println("Test.foo() called ");
    }
    public void foo() { // Compiler Error: cannot redefine foo()
        System.out.println("Test.foo(int) called ");
    }
    public static void main(String args[]) { 
        Test.foo();
    }
}
Output: Compiler Error, cannot redefine foo()







5. What is compile time and run time polymorphism?
overriding - Runtime polymorphism
         A subclass (or derived class) provides a specific implementation of a method in superclass (or base class).
The implementation to be executed is decided at run-time and decision is made according to the object used for call. Note that signatures of both methods must be same. Refer Overriding in Java for details.



overloading  - compile time polymorphism
     This feature allows different methods to have same name, but different signatures, especially number of input parameters and type of input paramaters. 
------------------------------------------------------------------------------------------------------------

Can we overload static methods?

The answer is ‘Yes’. We can have two ore more static methods with same name, but differences in input parameters. For example, consider the following Java program.

// filename Test.java 
public class Test { 
public static void foo() { 
System.out.println("Test.foo() called "); 
public static void foo(int a) { 
System.out.println("Test.foo(int) called "); 
public static void main(String args[]) 
Test.foo(); 
Test.foo(10); 

Output:
Test.foo() called 
Test.foo(int) called 
(source:- geeksforgeeks)



Can we overload methods that differ only by static keyword?
----------------------------------------------------------------------------------------------------------------------
We cannot overload two methods in Java if they differ only by static keyword (number of parameters and types of parameters is same). See following Java program for example. 
// filename Test.java 
public class Test { 
public static void foo() { 
System.out.println("Test.foo() called "); 
public void foo() { // Compiler Error: cannot redefine foo() 
System.out.println("Test.foo(int) called "); 
public static void main(String args[]) { 
Test.foo(); 

Output: Compiler Error, cannot redefine foo()
(source:- geeksforgeeks)



----------------------------------------------------------







Can we Override static methods in java?

We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism. Hence the answer is ‘No’.

If a derived class defines a static method with same signature as a static method in base class, the method in the derived class hides the method in the base class.



/* Java program to show that if static method is redefined by 

a derived class, then it is not overriding. */

// Superclass 
class Base { 
// Static method in base class which will be hidden in subclass 
public static void display() { 
System.out.println("Static or class method from Base"); 
// Non-static method which will be overridden in derived class 
public void print() { 
System.out.println("Non-static or Instance method from Base"); 

// Subclass 
class Derived extends Base { 
// This method hides display() in Base 
public static void display() { 
System.out.println("Static or class method from Derived"); 
// This method overrides print() in Base 
public void print() { 
System.out.println("Non-static or Instance method from Derived"); 

// Driver class 
public class Test { 
public static void main(String args[ ]) { 
Base obj1 = new Derived(); 
// As per overriding rules this should call to class Derive's static 
// overridden method. Since static method can not be overridden, it 
// calls Base's display() 
obj1.display(); 
// Here overriding works and Derive's print() is called 
obj1.print();  


output
-----------------------
Static or class method from base
Non-Static or Instance method from Deriv






What is Dynamic method dispatch ?
----------------------------------------------------------------------------------------------

Runtime Polymorphism or Dynamic method dispatch

Dynamic method dispatch is a mechanism by which a call to an overridden method is resolved at run-time. This is how java implements run-time polymorphism. When an overridden method is called by a reference, java determines which version of that method to execute based on the type of object it refers to. In simple words, the type of object which it referred, determines which version of the overridden method will be called.
 *We can hold the child class object in the reference of parent class*
upcasting in java





Covariant Return Type

The covariant return type specifies that the return type may vary in the same direction as the subclass.
Before Java5, it was not possible to override any method by changing the return type. But now, since Java5, it is possible to override method by changing the return type if subclass overrides any method whose return type is Non-Primitive but it changes its return type to subclass type. Let's take a simple example:

Simple example of Covariant Return Type

  1. class A{  
  2. A get(){return this;}  
  3. }  
  4.   
  5. class B1 extends A{  
  6. B1 get(){return this;}  
  7. void message(){System.out.println("welcome to covariant return type");}  
  8.   
  9. public static void main(String args[]){  
  10. new B1().get().message();  
  11. }  
  12. }  

Output:welcome to covariant return typ

As you can see in the above example, the return type of the get() method of A class is A but the return type of the get() method of B class is B. Both methods have different return type but it is method overriding. This is known as covariant return type.



















#java interview questions
#java polymorphism
#java runtime polymorphism
#java compile time polymorphism
#java moc interview
#corejava interview question answer on polymorphism
#java
#Core Concept Java polymorphism
#Dynamic method dispatch in java







Comments

Popular posts from this blog

Git Commands With Output Log

Java Interview Preparation (conceptual)

Java 8 Function Interface