-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCompileTimePolyMorphism.java
More file actions
62 lines (46 loc) · 1.84 KB
/
CompileTimePolyMorphism.java
File metadata and controls
62 lines (46 loc) · 1.84 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
45
46
47
48
49
50
51
52
53
54
55
56
class MethodOverloadingExample {
String defaultName = "User";
// printName with empty parameter
public void printName() {
System.out.println("My name is:"+ " " + defaultName);
}
// printName with one parameter
public void printName(String name){
System.out.println("My name is: " + name);
}
// printName functions with int data type with one parameter
public void printPrice(int price){
System.out.println("My name is: " + price);
}
// printName functions with float data type with one parameter
public void printPrice(float price){
System.out.println("My name is: " + price);
}
// printName with two parameter
public void printName(String name , String lastName) {
System.out.println("My name is: " + name+ " " + lastName);
}
// printName with two parameter
public void printName(String name , String middleName ,String lastName) {
System.out.println("My name is: " + name+ " " + middleName+ " " +lastName);
}
MethodOverloadingExample() {
System.out.println("I am constructor");
}
MethodOverloadingExample(String name) {
this.defaultName = name;
}
}
public class Polymorphism {
public static void main(String[] args) {
// PolyMorphismh
// MethodOverloadingExample overloading1 = new MethodOverloadingExample();
MethodOverloadingExample overloading2 = new MethodOverloadingExample("Anonymous");
overloading2.printName();
overloading2.printName("Vishal");
overloading2.printName("vishal" , "sharma");
overloading2.printName("vishal" ,"joshi", "sharama");
}
}
// There should be atleast two method with same name to encounter method overloading => static polyporphism
// there should be different parameter( number of parameter or type of parameter)