Search code examples
javaoverridingcomputer-scienceoverloading

Overloaded and overridden in Java


I know how to overload a method, and how to override a method. But is that possible to overload AND override a method at the same time? If yes, please give an example.


Solution

  • Overloading and overriding are complementary things, overloading means the same method name but different parameters, and overriding means the same method name in a subclass with the same parameters. So its not possible for overloading and overriding to happen at the same time because overloading implies different parameters.

    Examples:

    class A {
        public void doSth() { /// }
    }
    
    class B extends A {
        public void doSth() { /* method overriden */ }
    
        public void doSth(String b) { /* method overloaded */ }
    
    }
    

    Cheers!