Polymorphism and Static Methods

I faced the same issue while working with Inheritance. What I have learned is if the method being called is Static then it will be called from the class to which the reference variable belongs and not from the class by which it is instantiated.

 public class ParentExamp
    {                   
     public static void Displayer()
     {
      System.out.println("This is the display of the PARENT class");
     }
    }

     class ChildExamp extends ParentExamp
    {
        public static void main(String[] args)
        {
          ParentExamp a  = new ParentExamp();
          ParentExamp b  = new ChildExamp();
          ChildExamp  c  = new ChildExamp();

          a.Displayer(); //Works exactly like ParentExamp.Displayer() and Will 
                        call the Displayer method of the ParentExamp Class

          b.Displayer();//Works exactly like ParentExamp.Displayer() and Will 
                        call the Displayer method of the ParentExamp Class

          c.Displayer();//Works exactly like ChildExamp.Displayer() and Will 
                        call the Displayer method of the ChildtExamp Class
        }               
        public static void Displayer()
        {
         System.out.println("This is the display of the CHILD class");
        }   
    }

enter image description here


Because c calls the m1 method, but it's static, so it can't override and it calls the method in class Mini instead of Car.

That's exactly backwards.

c is declared as Car, so static method calls made through c will call methods defined by Car.
The compiler compiles c.m1() directly to Car.m1(), without being aware that c actually holds a Mini.

This is why you should never call static methods through instance like that.