Knowee
Questions
Features
Study Tools

What are abstract classes and interfaces in TypeScript?

Question

What are abstract classes and interfaces in TypeScript?

🧐 Not the exact question you are looking for?Go ask a question

Solution

Abstract classes and interfaces in TypeScript are powerful features that support object-oriented programming (OOP).

  1. Abstract Classes: Abstract classes are base classes from which other classes may be derived. They may not be instantiated directly. Unlike an interface, an abstract class may contain implementation details for its members. The abstract keyword is used to define abstract classes as well as abstract methods within an abstract class.

Example:

abstract class Animal {
    abstract makeSound(): void;

    move(): void {
        console.log('Moving along!');
    }
}

In the above example, Animal is an abstract class that has an abstract method makeSound. It also has a normal method move.

  1. Interfaces: Interfaces are a powerful way to define contracts within your code and they are the bread and butter of software design in TypeScript. They are used to type-check whether an object fits a certain structure. By defining an interface we can name a specific combination of variables, making sure that they will always go together.

Example:

interface LabelledValue {
    label: string;
}

function printLabel(labelledObj: LabelledValue) {
    console.log(labelledObj.label);
}

let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);

In the above example, LabelledValue is an interface and it's used to type-check myObj. If myObj doesn't have a label property, TypeScript will throw an error.

In summary, abstract classes and interfaces are two of the ways TypeScript allows you to write code in a more object-oriented style. Abstract classes provide a way to define classes that can't be instantiated, but can be extended. Interfaces provide a way to define the shape of certain data.

This problem has been solved

Similar Questions

What are differences among Interface, Abstract Class and Class? Hints: Use case Syntax and code example

How do you define an abstract class?

Which of the following is the correct way to define an interface in TypeScript?A)interface Person { name: string; age: number;}B)interface Person { name: string; age: string;}C)interface Person { name: number; age: number;}D)interface Person { name: number; age: string;}

Stat A)  An abstract class is a class which contains some abstract methods as well as concrete methods also. Stat B)  Consider that class that contains only abstract methods, and there are no concrete methods. It becomes an abstract class. Stat C) This means an interface is an abstract class but it contains only abstract methods. None of the methods in interface can be defined.  Stat D) Only method names or headers can be written in interface. So that an interface can be defined as a specification of method headers.ABCD TrueACD TrueBCD TrueAD True

Select the correct statements on differences of abstract classes and interfacesSelect one or more:interface is better suited for Type declaration and abstract class is more suited for code reuse and evolution perspective.you can extend only one abstract classyou can create a non-abstract method in abstract classyou can implement multiple inheritance in Java.you can create a non-abstract method in an interface

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.