3
votes

Well this type of questions have been answered already but this confused me. I have spent lot of time going through lots of answer so please go through my question before making it duplicate.

case 1 -

static polymorphism - function overloading dynamic polymorphism - function overriding

links - https://stackoverflow.com/a/12894211/3181738 https://stackoverflow.com/a/20783390/3181738 and many others.

My confusion is that in all the examples dynamic polymorphism is shown using upcasting. What if I don't upcast.

class A{
   public void show(){
       System.out.print("A");
   }
}

class B extends A{
   public void show(){
        System.out.print("B");
   }

   public static void main(String[] s){
      A a = new B();
      a.show(); // upcasting. It is dynamic polymorphism.
      B b = new B();
      b.show(); // Now java compiler can decide so is it still dynamic polymorphism?
   }
}

case 2 -

static polymorphism is achieved via overloading if method is private , static or final.

So what about overloading of public and default methods?

3
The static members aren't involved in runtime polymorphism. You can't override the static members in a derived class, but you can redefine themOPK
but you can overload static method and yes I am saying that overloading of static methods is example of static polymorphism. My question is what about public and defaultIshan

3 Answers

0
votes

Q1 upcasting: Upcasting and treating a subclass object as if it were a parent class object is the essence of polymorphism. So your question, "What if I don't upcast?" Then you are not using polymorphism

Q2 static polymorphism - method overridding: This is what it's saying:

  • static: static methods are overwritable, since they are associated with the class, not the object. So for instance, if you have a class with a main method, and then you subclass that, and add a main method to the subclass, when you execute the main method in the subclass, it is statically overridden

    • private: private methods are not inherited and cannot be called in the subclass, unless that are statically overridden

    • final: final methods are not overriddable. Therefore, redefining them in a subclass creates a statically overridden method

1
votes

Static methods can only be overloaded.

Non-static methods can be both overridden and overloaded.

public/protected/private doesn't affect this directly, except that since private methods can't be seen from subclasses, they can't be overridden either. The exact rules are defined here

And if you want to prevent the override of a non-static method, you can declare it final. (private methods are therefore implicitly final.)

0
votes

Case 1
At compile time, the compiler will identify the method signature that matches ie. it will be show() with no parameters in your example. Whether it is A's or B's method that gets invoked though is still runtime even though in your example you are sure it is a B.

Case 2
I think that the visibility rules of methods maybe causing some confusion for you.

The decision of which overloaded method that will be called at runtime will be decided at compile time ie. overloaded method is statically decided - this is regardless of whether it is public, package-private (default), or private. If the object, in which the method call occurs, does not have visibility of the method it fails at compile time.

As others have said - overriding does not apply to static methods since it is part of the class not the instance.