Skip to content

Commit 8050004

Browse files
Experiment 2.4
1 parent bbe2881 commit 8050004

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

OverloadingAndOverriding.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.company;
2+
class Overloading{
3+
int multiply(int a, int b) {
4+
return a * b;
5+
}
6+
7+
int multiply(int a, int b, int c) {
8+
return a * b * c;
9+
}
10+
11+
}
12+
public class OverloadingAndOverriding extends Overloading {
13+
14+
int multiply(int a, int b) {
15+
return a * b + 10; // Overriding the method
16+
}
17+
public static void main(String[] args) {
18+
19+
Overloading obj1 = new Overloading();
20+
Overloading obj2 = new OverloadingAndOverriding();
21+
22+
// Overloading (compile time)
23+
System.out.println(obj1.multiply(2, 3)); // 6
24+
System.out.println(obj1.multiply(2, 3, 4)); // 24
25+
26+
// Overriding (runtime)
27+
System.out.println(obj2.multiply(2, 3)); // 16
28+
}
29+
30+
}

0 commit comments

Comments
 (0)