Abstract Class

Abstract classes in programming serve as blueprints for other classes but cannot be instantiated themselves. They may contain abstract methods, which are declared but not implemented, as well as concrete methods with defined behavior. Abstract classes are useful for defining common behavior and characteristics shared among subclasses while allowing each subclass to provide its own implementation details for abstract methods. A common use case for abstract classes is in defining a base class with common functionality that multiple subclasses can inherit and extend.

Select Languages

Examples

C

// No Native Support or Implementation

C#

abstract class Animation {
  public abstract void walk();
  public abstract void run();
  public abstract void idle();
}

class Human: Animation {
  public override void walk() {
    Console.WriteLine("Human walks" );
  }
  public override void run() {
   Console.WriteLine("Human runs" );
  }
  public override void idle() {
   Console.WriteLine("Human idles" );
  }
}

class Zombie: Animation {
  public override void walk() {
    Console.WriteLine("Zombie walks" );
  }
  public override void run() {
    Console.WriteLine("Zombie runs" );
  }
  public override void idle() {
    Console.WriteLine("Zombie idles" );
  }
}

Human player = new Human();
player.walk();

Zombie boss1 = new Zombie();
boss1.run();

C++

#include <iostream>
using namespace std;

class Animation {
  public:
    // optional to implement
    virtual void walk(){};
    
    // mandatory to implement
    virtual void run() = 0;
    virtual void idle() = 0;
};

class Human: public Animation {
  public: 
    void walk() {
      cout << "Human walks" << endl;
    }
    void run() {
      cout << "Human runs" << endl;
    }
    void idle() {
      cout << "Human idles" << endl;   
    }
};

class Zombie: public Animation {
  public:
    void run() {
      cout << "Zombie runs" << endl;
    }
    void idle() {
      cout << "Zombie idles" << endl;
    }
};

Human player;
player.walk();
    
Zombie boss1;
boss1.run();

Go

// No Native Support or Implementation

Java

abstract class Animation {
  public abstract void walk();
  public abstract void run();
  public abstract void idle();
}

class Human extends Animation {
  public void walk() {
    System.out.println("Human walks" );
  }
  public void run() {
    System.out.println("Human runs" );
  }
  public void idle() {
    System.out.println("Human idles" );
  }
}

class Zombie extends Animation {
  public void walk() {
    System.out.println("Zombie walks" );
  }
  public void run() {
    System.out.println("Zombie runs" );
  }
  public void idle() {
    System.out.println("Zombie idles" );
  }
}

Human player = new Human();
player.walk();

Zombie boss1 = new Zombie();
boss1.run();

JavaScript

class Animation {
  constructor() {
    if(this.constructor == Animation) {
      throw new Error(
        "Cannot initiate abstract class"
      );
    };
    if(this.walk == undefined) {
      throw new Error(
        "walk method not implemented"
      );
    };
    if(this.run == undefined) {
      throw new Error(
        "run method not implemented"
      );
    };
    if(this.idle == undefined) {
      throw new Error(
        "idle method not implemented"
      );
    };
  }
}

class Human extends Animation {
  walk() {
    console.log("Human walks");
  }
  run() {
    console.log("Human runs");
  }
  idle() {
    console.log("Human idles");
  }
}

class Zombie extends Animation {
  walk() {
    console.log("Zombie walks");
  }
  run() {
    console.log("Zombie runs");
  }
  idle() {
    console.log("Zombie idles");
  }
}

const player = new Human();
player.walk();

const zombie = new Zombie();
zombie.run();

Kotlin

abstract class Animation {
  abstract fun walk();
  abstract fun run();
  abstract fun idle();
}

class Human: Animation() {
  override fun walk() {
    println("Human walks" );
  }
  override fun run() {
    println("Human runs" );
  }
  override fun idle() {
    println("Human idles" );
  }
}

class Zombie: Animation() {
  override fun walk() {
    println("Zombie walks" );
  }
  override fun run() {
    println("Zombie runs" );
  }
  override fun idle() {
    println("Zombie idles" );
  }
}

val player = Human();
player.walk();

val boss1 = Zombie();
boss1.run();

MatLab

classdef Animation
   methods(Abstract)
      animate_1 = walk(obj);
      animate_2 = run(obj);
      animate_3 = idle(obj);
   end
end

classdef Human < Animation
  methods
    function walk(obj)
      disp("Human walks");
    end
    function run(obj)
      disp("Human runs");
        end
    function idle(obj)
      disp("Human idles");
    end
  end
end

classdef Zombie < Animation
  methods
    function walk(obj)
      disp("Zombie walks");
    end
    function run(obj)
      disp("Zombie runs");
        end
    function idle(obj)
      disp("Zombie idles");
    end
  end
end

player = Human;
player.walk;

boss1 = Zombie;
boss1.run;

PHP

abstract class Animation {
  abstract public function walk();
  abstract public function run();
  abstract public function idle();
}

class Human extends Animation {
  public function walk() {
    echo "Human walks". "\n";
  }
  public function run() {
    echo "Human runs". "\n";
  }
  public function idle() {
    echo "Human idles". "\n";
  }
}

class Zombie extends Animation {
  public function walk() {
    echo "Zombie walks". "\n";
  }
  public function run() {
    echo "Zombie runs". "\n";
  }
  public function idle() {
    echo "Zombie idles". "\n";
  }
}

$player = new human();
$player->walk();

$boss1 = new Zombie();
$boss1->run();

Python

from abc import ABC, abstractmethod 

class Animation (ABC):
  @abstractmethod
  def walk(self):
    pass
  @abstractmethod
  def run(self):
    pass
  @abstractmethod
  def idle(self):
    pass

class Human(Animation):
  def walk(self):
    print('Human walks')
  def run(self):
    print('Human runs')
  def idle(self):
    print('Human idles')

class Zombie(Animation):
  def walk(self):
    print('Zombie walks')
  def run(self):
    print('Zombie runs')
  def idle(self):
    print('Zombie idles')
  
player = Human()
player.walk()

zombie = Zombie()
zombie.run()

R

// No Native Support or Implementation
// Use the R6 library 

Ruby

// No Native Support or Implementation

Rust

// No Native Support or Implementation
// Rust Traits are the closest alternative

Scala

abstract class Animation {
  def walk(): Unit
  def run(): Unit
  def idle(): Unit
}

class Human() extends Animation() {
  override def walk() {
    println("Human walks" );
  }
  override def run() {
    println("Human runs" );
  }
  override def idle() {
    println("Human idles" );
  }
}

class Zombie() extends Animation() {
  override def walk() {
    println("Zombie walks" );
  }
  override def run() {
    println("Zombie runs" );
  }
  override def idle() {
    println("Zombie idles" );
  }
}

val player = new Human();
player.walk();

val boss1 = new Zombie();
boss1.run();

Swift

// No Native Support or Implementation

TypeScript

abstract class Animation {
  abstract walk(): () => void;
  abstract run(): () => void;
  abstract idle(): () => void;
  
}

class Human extends Animation {
  walk() {
    console.log("Human walks");
  }
  run() {
    console.log("Human runs");
  }
  idle() {
    console.log("Human idles");
  }
}

class Zombie extends Animation {
  walk() {
    console.log("Zombie walks");
  }
  run() {
    console.log("Zombie runs");
  }
  idle() {
    console.log("Zombie idles");
  }
}

const player = new Human();
player.walk();

const zombie = new Zombie();
zombie.run();

Copyright 2025. All Rights Reserved. IronCodeMan.