Java overloading vs overriding

后端 未结 3 896
鱼传尺愫
鱼传尺愫 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:54

    what you have described is correct.

    For more clarification take a look at polymorphism concept. The Wikipedia has a good article

    http://en.wikipedia.org/wiki/Polymorphism#Computing

    http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

    0 讨论(0)
  • 2020-12-05 08:57

    You are basically correct. Overloading is having multiple methods in a single class where the method has the same name. However, the return value is not seen as part of the signature of the method. Thus, you cannot overload a method by changing only the return value. You cannot have the following code, from your example:

    public void setValue() {
       this.value = 0;
    }
    
    public int setValue() {
       return this.value;
    }
    

    This will fail to compile.

    As Rob identified, I believe you mean overriding, and you have that correct. Note with overriding, you cannot change the return type. As of Java 5, you can return a derived type of what the base class method returned. Before Java 5, it must be the identical type. That is, you cannot do the below until Java 5 and later:

    public class AnimalNoise {}
    public class Miaw extends AnimalNoise {}
    
    public class Animal {
        public AnimalNoise makeNoise() {
            return new AnimalNoise();
        }
    }
    
    public class Cat extends Animal {
        public Miaw makeNoise() {
            return new Miaw ();
        }
    }
    

    However, even in Java 5 and later, you cannot do the following:

    public class Animal {
        public String makeNoise() {
            return "silence";
        }
    }
    
    public class Cat extends Animal {
        public Miaw makeNoise() {
            return new Miaw ();
        }
    }
    public class Miaw {}
    

    Finally, a big difference between overloading and overriding that is often overlooked is that overloading is decided at compile time and overriding is decided at runtime. This catches many people by surprise when they expect overloading to be decided at runtime.

    0 讨论(0)
  • 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)
        }
    }
    
    0 讨论(0)
提交回复
热议问题