TypeScript TypeScript Classes & Interfaces 1 — Questions and Answers
Question 1: In TypeScript, what is the key difference between an `interface` and a `type` alias?
- Interfaces support declaration merging; type aliases do not (Correct answer)
- Type aliases support generics; interfaces do not
- Interfaces can only describe objects; type aliases can only describe primitives
- They are completely interchangeable with no differences
Correct answer: Interfaces support declaration merging; type aliases do not
Interfaces support declaration merging (multiple declarations of the same interface are merged), while type aliases cannot be reopened or merged after definition.
Question 2: Which access modifier restricts a class member to be accessible only within the class itself?
- protected
- public
- private (Correct answer)
- readonly
Correct answer: private
The `private` modifier limits access to a class member so it can only be used within the declaring class, not from subclasses or external code.
Question 3: What does the `implements` keyword do in a TypeScript class declaration?
- Inherits all methods from a parent class
- Declares that the class satisfies the contract of an interface (Correct answer)
- Creates an abstract base class
- Merges two interfaces into the class
Correct answer: Declares that the class satisfies the contract of an interface
The `implements` keyword tells TypeScript that a class must provide all properties and methods defined by the specified interface, acting as a contract check.
Question 4: What is an abstract class in TypeScript?
- A class that cannot have any methods
- A class that can be instantiated but not extended
- A class that cannot be instantiated directly and may contain abstract methods (Correct answer)
- A class with only static members
Correct answer: A class that cannot be instantiated directly and may contain abstract methods
An abstract class serves as a base class blueprint; it cannot be instantiated with `new` directly and may declare abstract methods that subclasses must implement.
Question 5: In TypeScript, what does parameter shorthand in a constructor do?
- Automatically calls the parent constructor
- Declares and assigns class properties directly from constructor parameters using access modifiers (Correct answer)
- Makes all constructor parameters optional
- Prevents the constructor from being overridden
Correct answer: Declares and assigns class properties directly from constructor parameters using access modifiers
Adding an access modifier (like `public` or `private`) to a constructor parameter automatically creates and initializes a class property with the same name.
Question 6: What is method overriding in TypeScript classes?
- Defining two methods with the same name but different parameter counts
- Redefining an inherited method in a subclass to change its behavior (Correct answer)
- Making a method private in a subclass
- Calling a method before the class is instantiated
Correct answer: Redefining an inherited method in a subclass to change its behavior
Method overriding occurs when a subclass provides its own implementation of a method inherited from a parent class, optionally using the `override` keyword for safety.
In TypeScript, what is the key difference between an `interface` and a `type` alias?