How to call a super method (ie: toString()) from outside a derived class

后端 未结 5 1330
無奈伤痛
無奈伤痛 2020-12-11 01:58

existantial question

if i have a class hierarchy like:

public class TestSuper {
    public static class A {
        @Override
        public String t         


        
5条回答
  •  生来不讨喜
    2020-12-11 02:19

    You can't, and very deliberately so: it would break encapsulation.

    Suppose you had a class which used a method to validate input by some business rules, and then call the superclass method. If the caller could just ignore the override, it would make the class pretty much pointless.

    If you find yourself needing to do this, revisit your design.

提交回复
热议问题