Method Overloading
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)
{ … }
}
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.
Overloading lets you define the same operation in different ways for different data.
Method Overriding
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) { …}
}
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.
Overriding lets you define the same operation in different ways for different object types.
No comments:
Post a Comment