A Simple Introduction To Java - Object Oriented Programming - Part 3 (Polymorphism)

otumianempire

Michael Otu

Posted on June 5, 2022

A Simple Introduction To Java - Object Oriented Programming - Part 3 (Polymorphism)

In this session, we will discuss:

There four OOP concepts Encapsulation, Inheritance, Polymorphism and Abstraction. We have discussed the first two in the said order.

Polymorphism

Have you ever written a HelloWorld program before? If you have then you would have noticed that the concept of HelloWorld is the same across (almost) all major programming languages.

For someone who, perhaps, this is your first then let's look at the HelloWorld program in Java, Python, Javascript and C++.

The HelloWorld programs

Java

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Enter fullscreen mode Exit fullscreen mode

This should look familiar.

Python

print("Hello world")

Enter fullscreen mode Exit fullscreen mode

Javascript

console.log("Hello world");
Enter fullscreen mode Exit fullscreen mode

C++

#include <iostream>

using namespace std;

int main() {
    cout << "Hello world" << endl;

    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Even though we are doing the same thing (writing a HelloWorld) in all four languages, the way they do them is different. Everyone had their way of saying "Hello world". Assume that any of the implementations above is a class and each class has its method sayHello: void. Then we can say that we have a different implementation for the same method, sayHello: void. That is the bases of Polymorphism.

Polymorphism_means having many forms. In the _sayHello: void scenario, depending on which object that is calling the sayHello: void method, we'd get a different implementation of the sayHello: void. There are two ways to "polymorph" a method in Java and this is by method overriding and method overloading.

Overriding

Let's create a base class to work with. We would create the Language class.

public class Language {

    public void sayHello() {
        System.out.println("This is the Human language!!");
    }
}

Enter fullscreen mode Exit fullscreen mode

We have a sayHello method that prints, This is the Human language!! to the screen. If we extend the Language class, we will have access to the sayHello method. The purpose of overriding is to create a polymorph of the method of interest.

Let's the class, Java

public class Java extends Language {

    public void sayHello() {
        System.out.println("This is the Java language!!");
    }
}

Enter fullscreen mode Exit fullscreen mode

and the class, Python

public class Python {

    public void sayHello() {
        System.out.println("This is the Python language!!");
    }
}

Enter fullscreen mode Exit fullscreen mode

and so on. Crate the classes for Javascript and CPlusPlus.

The idea is that, even though in Inheritance, the said classes share some common properties and methods, in Polymorphism some of the methods will have a different implementation based on the object that is calling the said method.

All the classes: Language, Java, Python, and the rest will have the same method but with different implementations specific to each class.

If you remember the Shape class, we had a default implementation for the area and perimeter. That is the super class had an implementation for area and perimeter. In the Square and Circle class, the implementation for area and perimeter may differ so we have to override them.

However, in our case where we have the super class to be the Language class which has just a method. sayHello_and we are only interested in _overriding it, it will be best that each of the sub classes will have to stand on their own without extending the Language class. We could even argue that just a class is needed with different methods like:

public class SayHello {

    public void inJava() {

    }

    public void inPython() {

    }

    public void inCPluPlus() {

    }

    public void inJavascript() {

    }

}

Enter fullscreen mode Exit fullscreen mode

Know that when we are going to override a method, the method signatures must be the same. So if sayHello in Language is public void sayHello() then sayHello in Java must be the same. For the access modifier, if it is private from the super class then we can maintain it as private or we can choose a higher one like protected or public. So we can't have a public for the super class' and have protected or private for the sub class'. We can not override static or final methods.

Overloading

In overriding a method, we have different implementations for methods by different classes. In overloading we do the overriding in the same class. So we'd have multiple implementations of one method.

Let's create our base class, Sum. Sum will have different implementations of a method, add.

In overriding a method, the method signatures must be the same

By this we meant, the access modifier, the return type, the method name and the parameters.

In overloading, we have the same method name and access modifier but the return type and parameters may differ.

public class Sum {

    public int add() {
        return 10 + 2;
    }

    public int add(int a) {
        return a + 1;
    }

    public int add(int a, int b) {
        return a + b;
    }

    public int add(int a, int b, int c) {
        return a + b + c;
    }


    public double add(double a, double b) {
        return a + b;
    }

    public double add(double a, double b, double c) {
        return a + b + c;
    }


    public String add(String a, String b) {
        return a + " " + b;
    }

    public void add(String a) {
        System.out.println("We are just adding: " + a);
    }

}
Enter fullscreen mode Exit fullscreen mode

Keep an eye on the return types and the parameters. Know that the +, plus, operator is also overloaded.

The question that asked is, which method would be called? Again, look at the return type s and parameters. The number and type of parameters you pass will determine which method to call and hence the return type.

Conclusion

Polymorphism is having several forms a method. We can override the method implementation using a subclass and we can have the same method in the same class but we have different return types based on the parameters.

Project

This project is for the sake of practice.

Given the superclass, Rectangle with methods, area and parameter a subclass, Square that overrides the methods, area and parameter. Implement both classes.

Know that a Rectangle has:

  • a length and breadth property.
  • The area is the product of the properties.
  • The perimeter is the sum of the four sides.
  • Let the types be int.

Know that a Square has a

  • one property that represents its sides. Since the sides are all the same. Let length the side be.
  • The area is the square of its side (side by side).
  • The perimeter is the sum of the four sides.
  • Let the types be int.

Source

  • Sololearn
  • DS Malik

Top

💖 💪 🙅 🚩
otumianempire
Michael Otu

Posted on June 5, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related