Why doesn't Java allow overriding of static methods?

后端 未结 22 2771
谎友^
谎友^ 2020-11-21 05:49

Why is it not possible to override static methods?

If possible, please use an example.

22条回答
  •  时光取名叫无心
    2020-11-21 06:19

    Actually we were wrong.
    Despite Java doesn't allow you to override static methods by default, if you look thoroughly through documentation of Class and Method classes in Java, you can still find a way to emulate static methods overriding by following workaround:

    import java.lang.reflect.InvocationTargetException;
    import java.math.BigDecimal;
    
    class RegularEmployee {
    
        private BigDecimal salary = BigDecimal.ONE;
    
        public void setSalary(BigDecimal salary) {
            this.salary = salary;
        }
        public static BigDecimal getBonusMultiplier() {
            return new BigDecimal(".02");
        }
        public BigDecimal calculateBonus() {
            return salary.multiply(this.getBonusMultiplier());
        }
        public BigDecimal calculateOverridenBonus() {
            try {
                // System.out.println(this.getClass().getDeclaredMethod(
                // "getBonusMultiplier").toString());
                try {
                    return salary.multiply((BigDecimal) this.getClass()
                        .getDeclaredMethod("getBonusMultiplier").invoke(this));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            }
            return null;
        }
        // ... presumably lots of other code ...
    }
    
    final class SpecialEmployee extends RegularEmployee {
    
        public static BigDecimal getBonusMultiplier() {
            return new BigDecimal(".03");
        }
    }
    
    public class StaticTestCoolMain {
    
        static public void main(String[] args) {
            RegularEmployee Alan = new RegularEmployee();
            System.out.println(Alan.calculateBonus());
            System.out.println(Alan.calculateOverridenBonus());
            SpecialEmployee Bob = new SpecialEmployee();
            System.out.println(Bob.calculateBonus());
            System.out.println(Bob.calculateOverridenBonus());
        }
    }
    

    Resulting output:

    0.02
    0.02
    0.02
    0.03
    

    what we were trying to achieve :)

    Even if we declare third variable Carl as RegularEmployee and assign to it instance of SpecialEmployee, we will still have call of RegularEmployee method in first case and call of SpecialEmployee method in second case

    RegularEmployee Carl = new SpecialEmployee();
    
    System.out.println(Carl.calculateBonus());
    System.out.println(Carl.calculateOverridenBonus());
    

    just look at output console:

    0.02
    0.03
    

    ;)

提交回复
热议问题