EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript Abstract Class
Secondary Sidebar
TypeScript Tutorial
  • TypeScript Basic and Advanced
    • What is TypeScript?
    • Typescript Examples
    • TypeScript Versions
    • TypeScript Operators
    • JavaScript dump object
    • JavaScript get Method
    • Webpack ReactJS
    • Code Generator JavaScript
    • JavaScript Projects
    • Call Stack JavaScript
    • JavaScript Projects GitHub
    • JavaScript Filter Function
    • JavaScript nan
    • JavaScripttimestamp
    • TypeScript loop
    • CoffeeScript
    • TypeScript Webpack
    • setTimeout TypeScript
    • DHTMLX
    • CoffeeScript for loop
    • TypeScript number
    • JavaScript export module
    • TypeScript string contains
    • TypeScript Inheritance
    • TypeScript get
    • TypeScript undefined
    • TypeScript Global Variable
    • TypeScript Dictionary
    • TypeScript Generic
    • TypeScript Cast Object
    • TypeScript Optional Parameters
    • TypeScript? switch
    • TypeScript promise
    • TypeScript tuple
    • TypeScript Hashmap
    • TypeScript let
    • TypeScript Getter
    • TypeScript Pattern Matching
    • TypeScript number to string
    • TypeScript substring
    • TypeScript?lambda
    • TypeScript UUID
    • TypeScript JSDoc
    • TypeScript Decorators
    • Typescript for loop
    • TypeScript HTTP Request
    • TypeScript Abstract Class
    • TypeScript Question Mark
    • TypeScript Nullable
    • TypeScript reduce
    • TypeScript Mixins
    • TypeScript keyof
    • TypeScript string to number
    • TypeScript JSON parse
    • TypeScript const
    • TypeScript declare module
    • TypeScript String
    • TypeScript filter
    • TypeScript Multiple Constructors
    • TypeScript? Set
    • TypeScript string interpolation
    • TypeScript instanceof
    • TypeScript JSON
    • TypeScript Arrow Function
    • TypeScript generator
    • TypeScript namespace
    • TypeScript default parameter
    • TypeScript cast
    • TypeScript babel
    • Typescript Key-Value Pair
    • TypeScript if
    • TypeScript keyof Enum
    • TypeScript wait
    • TypeScript Optional Chaining
    • TypeScript JSX
    • TypeScript Version Check
    • TypeScript Unit Testing
    • TypeScript Handbook
    • TypeScript module
    • TypeScript Extend Interface
    • TypeScript npm
    • TypeScript pick
    • TypeScript Interface Default Value
    • JavaScript import module
    • Obfuscate Javascript
    • TypeScript basics
    • setInterval TypeScript
  • Type of Union
    • TypeScript Object Type
    • TypeScript type check
    • TypeScript promise type
    • TypeScript JSON type
    • TypeScript Union Types
    • TypeScript typeof
    • TypeScript Types
  • TypeScript Array
    • TypeScript Array of Objects
    • Methods TypeScript Array
    • TypeScript remove item from array
    • TypeScript add to array
    • TypeScript Array Contains
  • Function Of Array
    • TypeScript Function Interface
    • TypeScript Functions
    • TypeScript Export Function
    • TypeScript function return type

TypeScript Abstract Class

By Shalu SharmaShalu Sharma

TypeScript Abstract Class

Definition of TypeScript Abstract Class

Abstract class is the concept of object-oriented programming language. Abstract class is used when we want to give a specific implementation of our methods. In TypeScript, we can create abstract class by simply using ‘abstract’keyword with it. Inside this, we can define our methods that implementation needs to be provided when implemented. In the coming section, we will discuss more abstract class in detail for better understanding and its usage.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

As we discussed that abstract classes are used to implement the concept of abstraction from an object-oriented programming language. To define them we use ‘abstract’ keyword. Let’s see its syntax in details for better understanding while doing programming see below;

abstract class class_name {}

As you can see in the above lines of syntax we are using the ‘abstract’ keyword before the class keyword. Inside this class, we can define our methods and variable we want. It is just like the normal class in TypeScript but the difference of ‘abstract’ keyword. Let’s see one practice syntax for a better understanding of abstract class in TypeScript see below;

e.g. :

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,198 ratings)

abstract class DemoABS {}

In this way, we can define it. In the coming section, we will discuss more how it works internally and where we should use this to make our code more reusable in detail.

How abstract class works in TypeScript?

As now we already know that abstract class in TypeScript is used to achieve abstraction in TypeScript, this works in the same way as java. We can define any class as abstract class using ‘abstract’ keyword before the class identifier in TypeScript. Inside this class we can define our method signature, after this, we can extend this class to use its methods to have their own implementation. In this section we will first see a sample example where we will understand its internal working in detail see below;

e.g. :

abstract class DemoABS {
constructor() {
}
getmsg(): void{
console.log("i am abstract class method");
}
}
class DemoNor extends DemoABS {
}
let obj: DemoNor = new DemoNor();
obj.getmsg();

In the above lines of code, you can see we are trying to use abstract class in TypeScript. First, we have created one class named ‘DemoABS’ this will be our abstract class. Inside this class, we have defined one constructor and one method named getmsg(). Both these does not accept any parameter. we have used no parameterized constructor and no argument method here just to demonstrate the use of abstract class in detail. After this, we are creating one more class that is going to use as an abstract class. To use the abstract class methods inside other class we have to extend the class using the ‘extends’ keyword just like java. After this, we have to provide implementation to all the methods available in the abstract class. If we do not provide implementation it will give us a compile-time error. To call the abstract class method we have to create an object of the other class because we cannot create an object of abstract class in an object-oriented programming language. So in the next line, we are creating an object of the DemoNor class which is extending DemoABS class, then only we can call getmsg() method available in an abstract class. So in this way, we can give or provide different implementations to the same method which helps in code optimization and reuse of code.

In the real-life case, we have so many cases where we can implement an abstract class concept. Suppose we have a bank which is a parent bank for all other banks. But in all the bank operations and features are the same, which can be named as withdrawal, deposit, etc. This operation would be the same for all the banks. Also, the bank provides the rate of interest which are different and may vary according to a different bank, so if we have one method named as rateOfinterest() in the abstract class then all the other bank class which are implementing this abstract class can define their own rateOfinterest() so this made it reusable and easy to handle.

Rules and regulation for abstract class

We have so many rules and regulation to define abstract class in any programming language see below;

1) To define abstract class we have to use ‘abstract’ keyword before the class identifier in TypeScript.

2) Abstract class can contain both the methods with and without a body in TypeScript. These are said to be concrete methods in general.

3) If the abstract class contains any abstract method whose body is not defined then e have to give the implementation to the methods when we extend the abstract class in any other class. In short, any class who is extending the abstract class it has to provide implementation to all the methods from the abstract class, or else it has to define itself abstract if all are not implemented.

4) We can also have concrete methods inside the abstract class, this methods do not require any implementation, we can directly access them using the child class object in TypeScript.

5) Most important we cannot create instance of the abstract class, to access its methods we have to extend it.

6) They are very easy to maintain, readable, and easy to understand by the other developer it makes the code clean and divide them in layers.

Examples

1) In this example we are trying to use an abstract class. We have defined two methods one is concrete and the other is abstract method which accepts one parameter. Also, we have several classes that implement the abstract class ‘DemoABS’, this is a sample example for beginners to understand the concept of abstract class in detail for better understanding and use while programming.

Example:

abstract class DemoABS {
constructor() {
}
getmsg(): void{
console.log("i am abstract class method");
}
abstract getValue(string);
}
class DemoNor1 extends DemoABS {
getValue(sample:string) {
console.log("sample is::" +sample);
console.log("From demo 1 class ");
}
}
class DemoNor2 extends DemoABS {
getValue(sample:string) {
console.log("sample is::" +sample);
console.log("From demo 2 class ");
}
}
class DemoNor3 extends DemoABS {
getValue(sample:string) {
console.log("sample is::" +sample);
console.log("From demo 3 class ");
}
}
class DemoNor4 extends DemoABS {
getValue(sample:string) {
console.log("sample is::" +sample);
console.log("From demo 4 class ");
}
}
class DemoNor5 extends DemoABS {
getValue(sample:string) {
console.log("sample is::" +sample);
console.log("From demo 5 class ");
}
}
let obj1: DemoNor1 = new DemoNor1();
obj1.getmsg();
obj1.getValue("hello from first object here !!")
let obj2: DemoNor1 = new DemoNor1();
obj2.getmsg();
obj2.getValue("hello from second object here !!")
let obj3: DemoNor1 = new DemoNor1();
obj3.getmsg();
obj3.getValue("hello from third object here !!")
let obj4: DemoNor1 = new DemoNor1();
obj4.getmsg();
obj4.getValue("hello from fourth object here !!")
let obj5: DemoNor1 = new DemoNor1();
obj5.getmsg();
obj5.getValue("hello from fifth object here !!")

Output:

TypeScript Abstract Class 1-1

Conclusion

Abstraction can be achieved by using abstract class in TypeScript, by the use of it we can reuse our code and provide a different implementation of the same method available. This can be useful when we have different implementations of the same thing in programming.

Recommended Articles

This is a guide to TypeScript Abstract Class. Here we discuss the Definition, How abstract class work in TypeScript? rules and regulations and examples with code implementation. You can also go through our other suggested articles to learn more –

  1. TypeScript let
  2. TypeScript typeof
  3. TypeScript Optional Parameters
  4. TypeScript Dictionary
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more