Core Java - Polymorphism
Tags: java
Published: 2023-02-15 16:34:12

**Polymorphism** Poly means many.
Morphic means Shapes.

It means a object can behave differently while communicating with different objects.

Two types of polymorphism 
   Compile time or static binding
   Runtime or dynamic binding


**Compile time** which is also static binding we can achieve compile time polymorphism by method overloading.
Method overloading means same method name with different signature arguments or different number of arguments.

```

public class CompileTime {
	void add(int a, int b) {
		int result = a + b;
		System.out.println("Result is : " + result);
	}

	void add(float a, float b) {
		float result = a + b;
		System.out.println("Result is : " + result);
	}

	void add(int a, int b, int c) {
		int result = a + b + c;
		System.out.println("Result is : " + result);
	}

	public static void main(String[] args) {
		CompileTime ct = new CompileTime();
		ct.add(10, 20);
		ct.add(10.1f, 20.5f);
	}
}
```
**Output :**
Result is : 30
Result is : 30.6

**Runtime Polymorphism**

```
public interface Runtime {
		void start();
		void shutDown();
}
```

```
public class MacBook implements Runtime {
	@Override
	public void start() {
		System.out.println("Inside macbook start");
	}
	@Override
	public void shutDown() {
		System.out.println("Inside macbook shutdown");
	}
}
```

```
public class MacBookAir implements Runtime{
	@Override
	public void start() {
		System.out.println("Inside MacbookAir Start");
	}
	@Override
	public void shutDown() {
		System.out.println("Inside MacBookAir shut down");
	}
}
```

```
public class RuntimePoly {
	public static void main(String[] args) {
			Runtime mac = new MacBook();
			Runtime air = new MacBookAir();
			mac.start();
			air.start();
	}
}
```

**Output** 
  Inside macbook start
  Inside MacbookAir Start

**Object Casting**
We can convert child class object to parent vise versa.

Upcasting is when we create a object for child from parent.
  Runtime mac = new MacBook();

DownCasting is when we create a object for parent from child.
MacBook m1 = (MacBook)mac;

AutoTypeCasting -- The compiler will automatically promote to next available type if the exact type not found.

```
public class CompileTime {
	void add(float a) {
		System.out.println("Result is : " + a);
	}

	public static void main(String[] args) {
		CompileTime ct = new CompileTime();
		ct.add(10);
		ct.add(10.1f);
	}
}
```

Output -  Result is : 10.0
Result is : 10.1


**Overriding** -- The static method in the parent cannot be override as non static methods. Vise versa non static method cannot be converted to static method in child.

If we use parent class to create a object for child and call the variable it will call the parent variable.

```
public interface Runtime {
		String s ="Hello";
}
```

```
public class MacBook implements Runtime {
	String s = "Hello MacBook";
}
```

```
public class RuntimePoly {
	public static void main(String[] args) {
                Runtime mac = new MacBook();
		System.out.println(mac.s);
	}
}
```
**Output**  Hello


Overloading of main method.

```
public static void main(String[] args) {
		System.out.println("String[] args");
		main(10);
	}
	public static void main(int args) {
		System.out.println("int args overloaded main method");
	}
```

Even though we overload main method JVM will take method with String[] args. We need to call the method with overloaded method in main method then only jvm will print the output of the method.