Introduction to Object-Oriented Programming -2 Inheritance & Polymorphism

In this article, we’ll continue to look at Object-Oriented Programming- abbr. OOP-. The topics are Inheritance and Polymorphism. These topics are two of the primary characteristics of object-oriented programming.

  • Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes.
  • Polymorphism is a Greek word that means “many-shaped”. And it occurs when we have many classes that are related to each other by inheritance.

Inheritance

Inheritance is a concept in which you define parent classes and child classes. So, It is the mechanism by which one class is allowed to inherit the features (fields and methods) of another class. Inheritance gives us reusability. Reusability could be described as creating a new class by reusing the properties of the existing class.

We define the “inheritance concept” into two categories:

  • Derived Class (child) — the class that inherits from another class
  • Base Class (parent) — the class being inherited from

C#

class BaseClass
{
public string model { get; set; }
public string color { get; set; }
public void TurnOn()
{
Console.WriteLine("Device turned on.");
}
public void TurnOff()
{
Console.WriteLine("Device turned off.");
}
}
class Laptop : BaseClass
{
public string display { get; set; }
public string storage { get; set; }
public void OpenTerminal() {
Console.WriteLine("Terminal Opened");
}
}
class Phone : BaseClass
{
public string capacity { get; set; }
public string display { get; set; }
public void Call(string phoneNumber)
{
Console.WriteLine(phoneNumber + " Calling ");
}
}
Laptop laptop= new Laptop();
laptop.model = "MacBook Pro";
laptop.color = "Space Gray";
laptop.display = "13";
laptop.storage = "1 TB";
laptop.TurnOn(); // Device turned on.
Phone phone = new Phone();
phone.model = "Iphone 12";
phone.color = "Pasific Blue";
phone.capacity = "256 GB";
phone.display = "6.1";
phone.TurnOn(); // Device turned on.
phone.Call("+90 242 242 2424");// "+90 242 242 2424 Calling"

We use a colon to inherit from another class and the parent class name to call the parent class.

JavaScript

class BaseClass {
constructor(model, color) {
this.model = model;
this.color = color;
}
turnOn() {
return "Device turned on.";
}
turnOff() {
return "Device turned off.";
}
}
class Laptop extends BaseClass{
constructor(model,color, display, storage) {
super(model,color);
this.display = display;
this.storage = storage;
}
openTerminal() {
return "Terminal Opened";
}
}
class Phone extends BaseClass{
constructor(model,color, capacity, display) {
super(model,color);
this.capacity = capacity;
this.display = display;
}
call(phoneNumber) {
return phoneNumber + " Calling ";
}
}
let laptop= new Laptop();
laptop.model = "MacBook Pro";
laptop.color = "Space Gray";
laptop.display = "13";
laptop.storage = "1 TB";
console.log(laptop.turnOn()); // Device turned on.
let phone = new Phone();
phone.model = "Iphone 12";
phone.color = "Pasific Blue";
phone.capacity = "256 GB";
phone.display = "6.1";
console.log(phone.turnOn()); // Device turned on.
console.log(phone.call("+90 242 242 2424"));// "+90 242 242 2424 Calling"

We use extends to inherit from another class and the super keyword to call the parent class (function).

Most of the advantage of inheritance is reusability and it leads to less development and maintenance costs.

Polymorphism

The term polymorphism simply means ‘one function, multiple forms’. Inheritance lets us inherit fields and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.

Types of Polymorphism are:

  • Compile-time polymorphism (Method overloading)
  • Run-time polymorphism (Method Overriding)

Let’s examine with the examples.

  1. Compile-time polymorphism (Method overloading)

C#

class BaseClass 
{
public void CalculateArea(int num1)
{
Console.WriteLine(6 * num1* num1);
}
public void CalculateArea(int num1, int num2)
{
Console.WriteLine(num1 * num2 );
}
}
class Cube : BaseClass
{
}
class Rectangle : BaseClass
{
}
BaseClass cube = new BaseClass();
BaseClass rectangle = new BaseClass();

// Compile-time polymorphism
cube.CalculateArea(2); // 24
rectangle.CalculateArea(2,2); // 4

JavaScript

In JavaScript, overloading is slightly different because we cannot produce two different functions with the same name. (The Factory Pattern uses this type of Polymorphism).

function rectangularArea (length, height) {
return length * height;
}
function cubeArea (length) {
// 6k^2
return rectangularArea(length*length, 6);
}
// Overloading happens here
function calculateArea (...args) {
if (args.length === 1) return cubeArea(...args)
return rectangularArea(...args)
}
console.log(calculateArea(2)); // 24
console.log(calculateArea(2,2)); // 4

2. Run-time polymorphism (Method Overriding)

C#

class BaseClass
{
public void TurnOn()
{
Console.WriteLine("Device turned on.");
}
public void TurnOff()
{
Console.WriteLine("Device turned off.");
}
}
class Laptop : BaseClass
{
public void TurnOn()
{
Console.WriteLine("Laptop turned on.");
}
public void TurnOff()
{
Console.WriteLine("Laptop turned off.");
}
}
class Phone : BaseClass
{
public void TurnOn()
{
Console.WriteLine("Phone turned on.");
}
public void TurnOff()
{
Console.WriteLine("Phone turned off.");
}
}
BaseClass mydevice = new BaseClass();
BaseClass laptop = new Laptop();
BaseClass phone = new Phone();
mydevice.TurnOn(); // Device turned on.
laptop.TurnOn(); // Device turned on.
phone.TurnOn(); // Device turned on.
Laptop laptopTwo = new Laptop();
laptopTwo.TurnOn(); // Laptop turned on.
Phone phoneTwo = new Phone();
phoneTwo.TurnOn(); // Phone turned on.

JavaScript

class BaseClass {
turnOn() {
return "Device turned on.";
}
turnOff() {
return "Device turned off.";
}
}
class Laptop extends BaseClass{
turnOn() {
return "Laptop turned on.";
}
}
class Phone extends BaseClass{
turnOn() {
return "Phone turned on.";
}
turnOff() {
return "Phone turned off.";
}
}
let laptop = new Laptop();
console.log(laptop.turnOn()); // Laptop turned on.
console.log(laptop.turnOff()); // Device turned off.
let phone = new Phone();
console.log(phone.turnOn()); // Phone turned on.
console.log(phone.turnOff()); // Phone turned off.

As you can see, the features of polymorphism mainly depend on the programming languages.

Conclusion

Inheritance is generating the child classes that take the properties and methods of the parent class. The child class can then also define its own methods.

Polymorphism allows the object to decide which form of the function to implement at compile-time (overloading) as well as run-time (overriding). It is applied to functions or methods.

Consequently, inheritance and polymorphism are interrelated concepts.

Full Stack Developer👨🏻‍💻 Camper 🏕️

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store