12. When is a method said to be overloaded and when is a method said to be overridden?


Method Overloading

Method Overriding

Overloading deals with multiple methods in the same class with the same name but different method signatures.


class MyClass {

public void getInvestAmount(int rate) {…}

public void getInvestAmount(int rate, long principal)

{ … }

}


Both the above methods have the same method names but different method signatures, which mean the methods are overloaded.


Overriding deals with two methods, one in the parent class and the other one in the child class and has the same name and signatures.


class BaseClass{

public void getInvestAmount(int rate) {…}

}


class MyClass extends BaseClass {

public void getInvestAmount(int rate) { …}

}


Both the above methods have the same method names and the signatures but the method in the subclass MyClass overrides the method in the superclass BaseClass.


Overloading lets you define the same operation in

different ways for different data.


Overriding lets you define the same operation in different

ways for different object types.





No comments:

Post a Comment