Sunday 9 September 2018

Can we Override static methods in Java

            The process of implementing the super class method in sub class is called as method overriding. As per Java principle, classes are closed for modification. so if we want to modify a method in any class, changing existing method is not a good idea instead of that we should extend that class and override method in the child class.If we try to override the method which is static, compiler won't give any error but we will get the output that does not satisfy the definition of overriding.
For example, we have two classes Animal and Cat, in Animal class, we have declared two methods, one is static and another is not static and in Cat class, we have overridden the Animal class methods.
From the output of the below programs, we can say that static methods can not be overridden.































Output :
Animal class : The static method in Animal
Cat Class : The instance method in Cat

No comments:

Post a Comment