Understanding method overloading and overriding in java
I know method overloading means defining a method in a subclass with the same name as in parent class but with different parameters
Incorrect. A method is overloaded if there is another method with the same name but different signatures in the same class.
Or more specifically I miss understanding or should I say a minimal set of rules which can dictate what will be the output in all examples.
The rules that the compiler and runtime applies when figuring out which method to call is described in section 15.12 of the Java Language Specification. The most relevant subsections to this question are 15.12.1, 15.12.2 and 15.12.4. This answer will essentially be a simplification of what is said there.
There are basically 3 things that the compiler and the runtime needs to decide:
- Which class should I search in? (compile time)
- Which overload should I call? (compile time)
- Which implementation should I call? (runtime)
Step 1 is decided based on the compile time type of the object on which you are calling the method. The class to search is the compile time type of that object.
Step 2 is decided by looking at all the matching overloads found in the class from Step 1, and picking the most specific one.
Step 3 depends on the runtime type.
Let's apply these rules to Example 3, 4 and 5. In all three examples, the class to search is A
.
Example 3
Two overloads of method
are declared in A
. method(A)
is overridden in B
a.method(b);
Both
method(A)
andmethod(B)
are applicable here, butmethod(B)
is more specific, somethod(B)
is chosen in Step 2.method(B)
is not overridden inB
, so in Step 3 the implementation inA
is chosen.a.method(a);
Only
method(A)
is applicable somethod(A)
is chosen at Step 2, because the compiler doesn't know the runtime type ofa
. Note that Step 2 is carried out at compile time.method(A)
is overridden inB
, so the implementation inB
is chosen in Step 3.
Example 4
Same as Example 3, except that method(B)
is also overridden in B
, so a.method(b);
calls the implementation in B
.
Example 5
Only one overload of method
is declared in A
. B
declares 2 overloads. One of them overrides method(A)
in A
.
Unlike the other examples, in resolving a.method(b);
, the compiler can't find a method(B)
in A
anymore, so the best applicable overload is method(A)
in Step 2. And then the implementation in B
is chosen in Step 3.
Resolving a.method(b);
is similar to Example 4.
Overloaded and overridden in Java
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!
How does JVM differentiates between method overloading and method overriding internally?
The JVM only deals with method overriding. A method is overridden by adding a method with the same signature in a derived class (the only allowed difference is in the return type, which is allowed to be more specific). The signature encodes the method's name, as well as the types of the parameters and the return type.
Method overloading means having multiple methods with the same "simple name" but different signatures. At compile time, the javac
compiler chooses one of the same-named methods based on the types of the arguments and places its signature in the compiled .class
file. A method invocation in compiled Java bytecode must specify the signature of the callee.
Overloading overridden method am I overloading parent or sub-class method
You override something that is inherited, so B.a() overrides A.a(). Overriding means to redefine.
Overloading is when your class have more than one definition of the same method name (each with different argument types). In B, the name a is overloaded. There is B.a() and B.a(int x).
Some of the definitions might be inherited. So if you remove B.a(), the class B would still have a method a() since it inherits it from A. And the method name a would still be overloaded in B.
Why should we override a method?
If you will give another name to the method in derived class, you cant invoke it with same interface. You can always invoke it through base class pointer.
i.e.
Base p = new Derived();
p.overrideMethod();
If Derived class is derived from Base then it will automatically call the derived version and not of Base. In case of different name, it is not possible. It is called code against interfaces and not implementations.
Related Topics
Why Does Java Code with an Inner Class Generates a Third Someclass$1.Class File
Using Variables Outside of an If-Statement
What Java Ftp Client Library Should I Use
Java: Set Timeout on a Certain Block of Code
Best Way to Create a Hashmap of Arraylist
Find Maximum, Minimum, Sum and Average of a List in Java 8
Get Key Press Without Pressing Enter in Console
Java: Split String When an Uppercase Letter Is Found
Modifier Static Is Only Allowed in Constant Variable Declarations
Centering Text in a Jtextarea or Jtextpane - Horizontal Text Alignment
Why Are Wait() and Notify() Declared in Java's Object Class
Java: CSV File Easy Read/Write
Sum All the Elements Java Arraylist
Override "Private" Method in Java
Differencebetween the 'Shell' Channel and the 'Exec' Channel in Jsch
How Are Servlet Url Mappings in Web.Xml Used