Skip to content

Commit 370336d

Browse files
committed
added polymorphism
1 parent 05e5b22 commit 370336d

File tree

6 files changed

+181
-2
lines changed

6 files changed

+181
-2
lines changed

.idea/workspace.xml

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"files.exclude": {
3+
"**/.classpath": true,
4+
"**/.project": true,
5+
"**/.settings": true,
6+
"**/.factorypath": true
7+
}
8+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ It is used to develop rich internet applications. It uses a light-weight user in
102102
|[Static KeyWord](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/OopsConcept/Static.md)|
103103
|[this keyword](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/OopsConcept/this.md)|
104104
|[Inheritance](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Inheritance/Inheritance.md)|
105-
|[]()|
105+
|[Polymorphism]()|
106106
|[]()|
107107
|[]()|
108108
|[]()|

src/Polymorphism/MethodOveriding.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
### Method Overriding in Java
2+
3+
-------
4+
5+
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.
6+
7+
In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.
8+
9+
##### Usage of Java Method Overriding
10+
11+
- Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
12+
- Method overriding is used for runtime polymorphism
13+
14+
##### Rules for Java Method Overriding
15+
16+
17+
1. The method must have the same name as in the parent class
18+
2. The method must have the same parameter as in the parent class.
19+
3. There must be an IS-A relationship (inheritance).
20+
21+
![](https://static.javatpoint.com/images/java-rules-for-method-overriding.png)
22+
23+
24+
-----
25+
26+
//Java Program to demonstrate why we need method overriding
27+
//Here, we are calling the method of parent class with child
28+
//class object.
29+
//Creating a parent class
30+
```java
31+
class Vehicle{
32+
void run(){System.out.println("Vehicle is running");}
33+
}
34+
//Creating a child class
35+
class Bike extends Vehicle{
36+
public static void main(String args[]){
37+
//creating an instance of child class
38+
Bike obj = new Bike();
39+
//calling the method with child class instance
40+
obj.run();
41+
}
42+
}
43+
```

src/Polymorphism/MethodOverloading.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
### Method Overloading in Java
2+
3+
-----
4+
5+
If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.
6+
7+
If we have to perform only one operation, having same name of the methods increases the readability of the program.
8+
9+
Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs.
10+
11+
So, we perform method overloading to figure out the program quickly.
12+
13+
Advantage of method overloading
14+
- Method overloading increases the readability of the program.
15+
16+
17+
There are two ways to overload the method in java
18+
19+
- By changing number of arguments
20+
- By changing the data type
21+
22+
23+
------
24+
25+
26+
#### 1) Method Overloading: changing no. of arguments
27+
28+
In this example, we have created two methods, first add() method performs addition of two numbers and second add method performs addition of three numbers.
29+
30+
In this example, we are creating static methods so that we don't need to create instance for calling methods.
31+
32+
```java
33+
class Adder{
34+
static int add(int a,int b){return a+b;}
35+
static int add(int a,int b,int c){return a+b+c;}
36+
}
37+
class TestOverloading1{
38+
public static void main(String[] args){
39+
System.out.println(Adder.add(11,11));
40+
System.out.println(Adder.add(11,11,11));
41+
}
42+
}
43+
```
44+
Output
45+
```
46+
22
47+
33
48+
```
49+
50+
------
51+
52+
##### 2) Method Overloading: changing data type of arguments
53+
54+
55+
56+
In this example, we have created two methods that differs in data type. The first add method receives two integer arguments and second add method receives two double arguments.
57+
58+
```
59+
class Adder{
60+
static int add(int a, int b){return a+b;}
61+
static double add(double a, double b){return a+b;}
62+
}
63+
class TestOverloading2{
64+
public static void main(String[] args){
65+
System.out.println(Adder.add(11,11));
66+
System.out.println(Adder.add(12.3,12.6));
67+
}
68+
}
69+
```
70+
Output
71+
```
72+
22
73+
24.9
74+
```
75+
76+
77+
-------
78+
79+
Q) Why Method Overloading is not possible by changing the return type of method only?
80+
81+
In java, method overloading is not possible by changing the return type of the method only because of ambiguity. Let's see how ambiguity may occur:
82+
83+
```java
84+
class Adder{
85+
static int add(int a,int b){return a+b;}
86+
static double add(int a,int b){return a+b;}
87+
}
88+
class TestOverloading3{
89+
public static void main(String[] args){
90+
System.out.println(Adder.add(11,11));//ambiguity
91+
}
92+
}
93+
```
94+
Output
95+
```
96+
Compile Time Error: method add(int,int) is already defined in class Adder
97+
System.out.println(Adder.add(11,11)); //Here, how can java determine which sum() method should be called?
98+
```
99+
100+
-------
101+
102+
#### Can we overload java main() method?
103+
Yes, by method overloading. You can have any number of main methods in a class by method overloading. But JVM calls main() method which receives string array as arguments only. Let's see the simple example:
104+
```java
105+
class TestOverloading4{
106+
public static void main(String[] args){System.out.println("main with String[]");}
107+
public static void main(String args){System.out.println("main with String");}
108+
public static void main(){System.out.println("main without args");}
109+
}
110+
```
111+
Output
112+
```
113+
main with String[]
114+
```
115+

src/Polymorphism/Polymorphism.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
1- Method Overloading
4+
2- Method Overriding
5+
3-

0 commit comments

Comments
 (0)