DEV Community

Cover image for 5-TS OOP: Abstract Classes
Hasan Zohdy
Hasan Zohdy

Posted on

5-TS OOP: Abstract Classes

Introduction

Abstract classes are classes that cannot be instantiated, they are used as a base class for other classes, they can contain abstract methods and concrete methods.

That's the academic definition, let's simplify it.

Imagine Abstract classes like something that is only a definition, but it can not be existing by itself, for example, Fruit is a concept of something sweet (Mostly) that can be eaten from plants, it does not exist as an entity, but Mango is a kind of fruit that exists as an entity, it is a real thing, it can be eaten, it can be thrown, it can be used in many ways, but Fruit is just a concept, it is not a real thing, it is just a definition.

From this point we can say Fruit is an abstract class, and Mango is a concrete class AKA the thing that makes Fruit a real thing.

Abstract classes can not be instantiated, they can only be inherited from, and they can contain abstract methods (methods that are not implemented) and concrete methods (methods that can be called and implemented by the class itself).

Abstract Class In Javascript

Unfortunately, Javascript does not have abstract classes, but luckily Typescript does, that's why i mentioned it in the title, and this course will cover OOP in both Javascript and Typescript.

I will just explain the concept of abstract in Typescript, you don't really need to have an experience in Typescript to follow up with me, you can just follow the code examples and you will be fine.

Abstract Class In Typescript

Let's see an example in TS:

abstract class Fruit {
    name: string;
    taste: string;
    color: string;
}

class Mango extends Fruit {
    name = 'Mango';
    taste = 'Sweet';
    color = 'Yellow';
}
Enter fullscreen mode Exit fullscreen mode

Here we have 2 classes, the Fruit class is the abstract class, and the Mango class is the concrete class.

So the class that extends the abstract class is called concrete class, and the abstract class is the base class.

Abstract class characteristics

Here are the characteristics of an abstract class:

  • Abstract classes can not be instantiated, (you can not create a new object of it).
  • Abstract classes can only be inherited from.
  • Abstract classes can contain abstract methods and concrete methods, (We will discuss it in the next article)

So in a nutshell, abstract classes are classes that can not be instantiated (A.K.A can not exist by its own), they are used as a base class for other its children classes, they can contain abstract methods and concrete methods.

🎨 Conclusion

In this article we learned about abstract classes, we learned what is an abstract class, what are the characteristics of an abstract class, and we learned that abstract classes are classes that can not be instantiated, they are used as a base class for other its children classes, they can contain abstract methods and concrete methods.

☕♨️ Buy me a Coffee ♨️☕

If you enjoy my articles and see it useful to you, you may buy me a coffee, it will help me to keep going and keep creating more content.

😍 Join our community

Join our community on Discord to get help and support.

📚 Bonus Content 📚

You may have a look at these articles, it will definitely boost your knowledge and productivity.

General Topics

Packages & Libraries

React Js Packages

Courses (Articles)

Top comments (0)