Three characteristics of OOP: Polymorphism
Tags: java, computerscience, beginners, internship
Published: 2022-01-28 02:31:03



###Polymorphism:


Finally, I begin to write about POLYMORPHISM section. This section needs me get much more familier with the core knowledge of *Three Main Features Of Java*.

Inheritance maybe not the most popular in code, but the most important part of programming (at least, I think so XD).


**1. What is Polymorphism:**

**Polymorphism is the implementation of an object having different manifestations.**

For example, pressing the same button "ENTER" in different applications, computer will show up different reactions, like acting 
 "Line Feed" function in Word application, acting "Send" function after you type the text in chat box, etc. Just like invoking the same function, pressing "ENTER" button plays an different roles in different applications. This is also kind of Polymorphism.


**2. Significance of Polymorphism :**

1. Remove coupling between classes
2. Replaceability
3. Scalability
4. Working as interface (like a real `interface` in Java)
5. Flexibility
6. Simplicity


Before describing all of its significance, there are three necessary conditions for the existence of polymorphisms that you should know:

1. The implementance of inheritance: So you can see, the implementance of polymorphsm is also the implementance of 
2. Override
3. An reference of father class points to an object of a sub class: `Parent p = new Child();`

Now we have classes in our program:
![2384ed46c6540cea9eb1415086bf8c89_2DAC601E-70D8-4B3C-86CC-7E4972FC2466](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6env33zyq3ir8dfw7p1q.jpg)
 

And we have codes for these:
```java
class Shape {
    void draw() {}// is overridden
}

class Circle extends Shape {
    void draw() {//override function draw() of class Shape
        System.out.println("Circle.draw()");
    }
}

class Square extends Shape {
    void draw() {//override function draw() of class Shape
        System.out.println("Square.draw()");
    }
}

class Triangle extends Shape {
    void draw() {//override function draw() of class Shape
        System.out.println("Triangle.draw()");
    }
}
```

When a method is called in polymorphic mode, it is first checked to see if the method exists in the parent class. If it does not, a compile error occurs. If so, call the method of the same name in the subclass. The phenomenon of calling a function in a subclass with the same name as the one of father class, but showing different results is called Override.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/krn1ru5f51j9apo77z74.png)

The best way to demonstrate these 6 advantages is to look at the following code:
```java
abstract class Animal {// Abstraction
    abstract void eat();
}

class Cat extends Animal {
    public void eat() {// Interface
        System.out.println("Eat fish.");
    }
    public void work() {
        System.out.println("Catch mouse.");
    }
}

class Dog extends Animal {
    public void eat() {
        System.out.println("Eat bone.");
    }
    public void work() {
        System.out.println("Guard.");
    }
}

public class Test {

    public static void main(String[] args) {
        show(new Cat());  // Invoke the show() through the object of class Cat
        show(new Dog());  // Invoke the show() through the object of class Cat

        Animal a = new Cat();  // Upcasting
        a.eat();               // Invoke eat() of class Cat
        Cat c = (Cat)a;        // Downcasting
        c.work();        // Invoke work() of class Cat
    }

    public static void show(Animal a)  {
        a.eat();
        // Judging the class type
        if (a instanceof Cat)  {  // What a cat does
            Cat c = (Cat)a;
            c.work();
        } else if (a instanceof Dog) { // What a dog does
            Dog c = (Dog)a;
            c.work();
        }
    }
}
```

And you can see the output:

>Eat fish.
>Catch mouse.
>Eat bone.
>Guard.
>Eat fish.
>Catch mouse.



**3. How to implement Polymorphism:**


- 1. Override: 

**Override exists for polymorphisms.**


We've already discussed method overrides, which are methods that can be overridden by a sub class.

When a subclass object calls an overridden method, the method in the subclass is called, not the overridden method in the superclass.

To call an overridden method in a parent class, you must use the `super` keyword.


- 2. Interface

Interfaces in Java are similar to interfaces in life, which are collections of method characteristics, but no method implementation. See the [Java Interfaces](https://www.tutorialspoint.com/java/java_interfaces.htm) section for details.


- 3. Abstract classes and abstract functions (vitural function)

**Virtual functions also exist for polymorphisms.**


Of course, the implementation of polymorphism is not only about the interation among classes. It also can be implemented by `abstract function()` and `abstract class {} `(like the vitural func() in c++). 

___

###Last but not least


Those concepts of `abstract` and `override` could be written in another blogs. However, this blog is the first blog of mine, I hope I could get some feedbacks and advice from our members of DEV community, and then move forward again.

Your advice and comments are the power of my moving forward. Thank you for your reading.


***Never know how good you will be if you don't push yourself.***
Ricky Ruan:

E-mail: yruan@umass.edu