Want to learn Fundamental of Instance Method?
In Java, an instance method in a subclass with the same signature and return type as an instance method in the superclass overrides the superclass’s method.
Overriding and Hiding Methods
The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is close enough
and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method it overrides. Basically it’s the definition of method hiding in Java.
An overriding method can also return a subtype of the type returned by the overridden method.
This is called a covariant return
type.
When overriding a method, you might want to use the @Override
annotation that instructs the compiler that you intend to override a method in the superclass. If, for some reason, the compiler detects that the method does not exist in one of the superclasses, it will generate an error.
If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides
the one in the superclass.
The distinction between hiding and overriding
has important implications. The version of the overridden method that gets invoked is the one in the subclass. The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass.
Can we override static method in Java?
No, you cannot override static method in Java
because method overriding is based upon dynamic binding at runtime. Usually static methods are bonded using static binding at compile time before even program runs.
Basically, keyword static
modifies the lifecycle of variable and method. If you specify static method or variable then those are created at the time of class is loaded and not at runtime. Nonstatic
variables and methods are only available during runtime.
Let’s look at below Java Example that contains two classes. The first is Company
, which contains one instance method and one class method. The second class, a subclass of Company
, is called CrunchifyComapny
:
Kindly take a look at inline comment descriptions for some more information.
package com.crunchify.tutorials; /** * @author Crunchify.com */ public class CrunchifyHideStaticMethod { public static void main(String args[]) { Company cmp = new CrunchifyCompany(); // if we can override static, this should call method from Child class cmp.staticMethod(); // Eclipse should show warning: The static method // staticMethod() from the type Company should be // accessed in a static way cmp.nonStaticMethod(); } } // Comapny: Super-Parent Class class Company { // public static method which can not be overridden in Java public static void staticMethod() { System.out.println("Company: Static Instance method"); } // non static method public void nonStaticMethod() { System.out.println("Company: non-Static method"); } } // CrunchifyCompany: Sub-Child Class class CrunchifyCompany extends Company { /* * static method of same name and method signature as existed in super * class, this is not method overriding instead this is called method hiding * in Java */ public static void staticMethod() { System.err.println("CrunchifyCompany: Overridden Static Instance method"); } // non static method public void nonStaticMethod() { System.out.println("CrunchifyCompany: non-Static method"); } }
Eclipse Console Output:
Just run above Java program in Eclipse and you will see result as below.
Company: Static Instance method CrunchifyCompany: non-Static method
Let me know if you face any issue running above program.