Harshit Gupta
Harshit Gupta

Reputation: 739

Method overloading in different classes

Is it possible to have perform method overloading in different classes.

class Parent{
    // Private method
    private void method1(){
        System.out.println("In private method of Parent class");
    }
    void method2(){
    }
    void method3(){
    }
}

class Child extends Parent{
    void method3(int i){
    }
}

To perform overloading it is necessary to have two methods of same name and with different signature in the class. but in inheritance how does it work. In inheritance is it true that copy of non private method is created in the child class? In this example overloading is performed or not?

Upvotes: 2

Views: 5225

Answers (2)

Vasu
Vasu

Reputation: 22384

Overriding - Redefining the methods in the Sub Class with out disturbing the signature. This is also called as Dynamic Binding, which will be decided during Runtime based upon the object being passed.

Overloading - Redefining the methods with in the same Class by changing the method signatures. This is also called as Static Binding, which will be decided during compile time.

Here, in your particular example, we SHOULD NOT say that the method3() is overloaded as we did not re-define method3() more than one time with in the same class.

Upvotes: 0

Akash Thakare
Akash Thakare

Reputation: 22974

Overloading means methods with same name but different signature but not override equivalent for particular class. It's subject of class and not related to it's parent or child. Moreover, if parent has overloaded methods than child may or may not have the same behavior. Moreover, if any interface contains the overloaded signatures your class ultimately have the overloaded methods.

Note here that you have not overloaded method3(int i) with method() of parent, even more method of Child is not related to method of it's parent in your case. You can only override non-private and non-static methods of parent but you can not overload them, there is no meaning of overloading them.

Upvotes: 3

Related Questions