Skip to content

Commit 082f330

Browse files
author
Ram swaroop
committed
overloading almost done
1 parent 22bd1d6 commit 082f330

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

_posts/2015-06-02-overloading.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,122 @@ Reusing the same method name in the same class or subclass but with different ar
88

99
There are certain __rules for overloading__, the below code points out all of the rules:
1010

11+
{% highlight java linenos %}
12+
13+
public class Foo {
14+
public void doStuff(int y, String s) { }
15+
16+
public void doStuff(int x) { } // Valid overload as DIFFERENT ARGUMENT LIST
17+
// (and methods can be overloaded
18+
// in the same class or in subclass)
19+
}
20+
21+
class Bar extends Foo {
22+
23+
private void doStuff(long a) { // Valid overload as DIFFERENT ARGUMENT LIST
24+
// (access modifier CAN be same or different)
25+
}
26+
27+
public String doStuff(int x, int y) { // Valid overload as DIFFERENT ARGUMENT LIST
28+
return "s"; // (return type CAN be same or different)
29+
}
30+
31+
public void doStuff(int y, long s) throws IOException { } // Valid method overload as DIFFERENT ARGUMENT LIST
32+
// (overloaded methods CAN declare new or broader
33+
// checked exceptions)
34+
35+
public String doStuff(int y, String s) { // Invalid overload, MUST change method's
36+
return "s"; // argument list (compiler error)
37+
}
38+
}
39+
40+
{% endhighlight %}
41+
42+
In short, the only rule you __MUST__ obey is to __change the argument list__ of the overloaded method, the rest
43+
are all optional.
44+
45+
### Invoking Overloaded Methods
46+
47+
{% highlight java linenos %}
48+
49+
class Animal {
50+
}
51+
52+
class Horse extends Animal {
53+
}
54+
55+
class UseAnimals {
56+
public void doStuff(Animal a) {
57+
System.out.println("In the Animal version");
58+
}
59+
60+
public void doStuff(Horse h) {
61+
System.out.println("In the Horse version");
62+
}
63+
64+
public static void main(String[] args) {
65+
UseAnimals ua = new UseAnimals();
66+
Animal animalObj = new Animal();
67+
Horse horseObj = new Horse();
68+
Animal animalRefToHorse = new Horse();
69+
ua.doStuff(animalObj);
70+
ua.doStuff(horseObj);
71+
ua.doStuff(animalRefToHorse);
72+
}
73+
}
74+
75+
{% endhighlight %}
76+
77+
The output of the above program is:
78+
79+
In the Animal version
80+
In the Horse version
81+
In the Animal version
82+
83+
Notice the call to `doStuff(animalRefToHorse)`, here the `Animal` version of `doStuff()` is called despite the actual
84+
object being passed is of a `Horse`. The reference type (not the object type) determines which overloaded method is
85+
invoked.
86+
87+
To summarize, __which overridden version__ of the method to call (in other words, from which class in the
88+
inheritance tree) is decided __at runtime based on object type__, but __which overloaded version__ of the method to
89+
call is based on the __reference type of the argument passed at compile time__.
90+
91+
---------
92+
93+
### Q&A
94+
95+
__Q1.__ Consider the below program in which a method is both overridden and overloaded.
96+
97+
{% highlight java linenos %}
98+
99+
public class Animal {
100+
public void eat() {
101+
System.out.println("Animal eating");
102+
}
103+
}
104+
105+
public class Horse extends Animal {
106+
public void eat() {
107+
System.out.println("Horse eating hay ");
108+
}
109+
110+
public void eat(String s) {
111+
System.out.println("Horse eating " + s);
112+
}
113+
}
114+
115+
{% endhighlight %}
116+
117+
Figure out which version of `eat()` will run on each of the invocation made?
118+
119+
1. Animal ah = new Horse();
120+
ah.eat();
121+
2. Horse he = new Horse();
122+
he.eat("Apples");
123+
3. Animal a2 = new Animal();
124+
a2.eat("treats");
125+
4. Animal ah2 = new Horse();
126+
ah2.eat("Carrots");
127+
128+
__Q2.__
11129

0 commit comments

Comments
 (0)