Java overloading vs overriding

后端 未结 3 893
鱼传尺愫
鱼传尺愫 2020-12-05 08:11

Hi I just want to make sure I have these concepts right. Overloading in java means that you can have a constructor or a method with different number of arguments or differe

3条回答
  •  隐瞒了意图╮
    2020-12-05 08:57

    Correct; overloading is providing multiple signatures for the same method.

    Overriding, which is what I think you mean by "overwriting" is the act of providing a different implementation of a method inherited from a base type, and is basically the point of polymorphism by inheritance, i.e.

    public class Bicycle implements Vehicle {
        public void drive() { ... }
    }
    
    public class Motorcycle extends Bicycle {
        public void drive() {
            // Do motorcycle-specific driving here, overriding Bicycle.drive()
            // (we can still call the base method if it's useful to us here)
        }
    }
    

提交回复
热议问题