prog8am
prog8am
Why,how
1 post
Don't wanna be here? Send us removal request.
prog8am ยท 2 months ago
Text
Tumblr media
What is Method overriding in Java?
Method overriding allows to make /take different parameters of the same method(name).In a situation you have to calculate x of different things with different number or types of values.(taking different parameters of data types)
Method overloading in Java is a feature that allows multiple methods in the same class to have the same name but different parameters (number, type, or both). It helps improve code readability and reusability.
Rules for Method Overloading:
Different Number of Parameters:
class MathUtils { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } }
Different Parameter Types:
class MathUtils { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }
Different Order of Parameters:
class MathUtils { void printInfo(String name, int age) { System.out.println(name + " is " + age + " years old."); } void printInfo(int age, String name) { System.out.println(name + " is " + age + " years old."); } }
Important Notes:
Method overloading is resolved at compile time (static binding).
The return type is not considered for overloading (i.e., methods cannot be overloaded by only changing the return type).
0 notes