- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverloadDemo.java
More file actions
Latest commit
44 lines (36 loc) · 1.41 KB
/
Copy pathOverloadDemo.java
File metadata and controls
44 lines (36 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// method overloading demo based on an article from http://www.25hoursaday.com/CsharpVsJava.html#same
// parent class implementing a print method
classParent {
// printing with a String param
publicvoidprintStuff(Stringstr) {
System.out.println("In parent class: " + str);
}
}
// child class inheriting from parent
classChildextendsParent {
// overloaded parent method
publicvoidprintStuff(Stringstr) {
System.out.println("In child class: " + str);
}
// print method with different signature
publicvoidprintStuff(intn) {
System.out.println("In child class: " + n);
}
}
classOverloadDemo {
publicstaticvoidmain(String[] args) {
// create some variables
Stringstr = "Hello there!";
inti = 100;
// create a parent and a child object
Parentp = newParent();
Childc = newChild();
Parentpc = newChild(); // allowed - Child-declared method will be inaccessible
// printing items
p.printStuff(str); // allowed - Parent object calls Parent method
c.printStuff(str); // allowed - Child calls overloaded method
c.printStuff(i); // allowed - Child calls Child-specific method
pc.printStuff(str); // allowed - Parent object calls Child method
//pc.printStuff(i); // not allowed - parent does not have this method
}
}