-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverloadDemo.java
More file actions
44 lines (36 loc) · 1.41 KB
/
OverloadDemo.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
class Parent {
// printing with a String param
public void printStuff(String str) {
System.out.println("In parent class: " + str);
}
}
// child class inheriting from parent
class Child extends Parent {
// overloaded parent method
public void printStuff(String str) {
System.out.println("In child class: " + str);
}
// print method with different signature
public void printStuff(int n) {
System.out.println("In child class: " + n);
}
}
class OverloadDemo {
public static void main(String[] args) {
// create some variables
String str = "Hello there!";
int i = 100;
// create a parent and a child object
Parent p = new Parent();
Child c = new Child();
Parent pc = new Child(); // 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
}
}