Object-oriented programming (OOP) Flashcards

(8 cards)

1
Q

What is a class in programming? Can you give an example?

A

A class in programming is a blueprint or template for creating objects (instances). It defines the properties (data) and methods (behavior) that the objects created from it will have.

class Person {
name: string;
age: number;

constructor(name: string, age: number) {
this.name = name;
this.age = age;
}

greet() {
console.log(Hello, my name is ${this.name} and I'm ${this.age} years old.);
}
}

// Creating an object (instance) of the class
const user = new Person(“Alice”, 30);
user.greet(); // Output: Hello, my name is Alice and I’m 30 years old.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the benefit of interfaces?

A

Interfaces provide a way to define the shape or contract of an object in TypeScript (and other statically typed languages). They do not contain implementation, only structure — which helps enforce consistency, safety, and clarity in code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is inheritance in OOP?

A

Inheritance in Object-Oriented Programming (OOP) is a mechanism that allows a class (child or subclass) to inherit properties and methods from another class (parent or superclass). It enables code reuse, hierarchical classification, and polymorphism.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is meant by polymorphism in OOP?

A

Polymorphism in Object-Oriented Programming (OOP) means “many forms” — it allows objects of different classes to be treated as instances of a common superclass, typically through a shared interface or base class, while each class can have its own specific implementation of methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is encapsulation important in programming?

A

Encapsulation is a fundamental principle of programming and especially of Object-Oriented Programming (OOP). It means bundling data (properties) and the methods (functions) that operate on that data into a single unit, typically a class, and restricting direct access to some of the object’s components.

  1. Data Protection & Integrity
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why do we use abstraction in programming?

A

Abstraction in programming is the concept of hiding complex implementation details and showing only the essential features or behaviors to the user. It helps manage complexity by focusing on what an object does, rather than how it does it.

  1. Simplify Complex Systems
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between a function and a constructor?

A

A function is a reusable block of code designed to perform a specific task.

A constructor is a special function used to create and initialize objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between prototype inheritance and classical inheritance?

A

Classical Inheritance is based on classes, where a subclass inherits from a superclass.

Prototype Inheritance (JavaScript) is based on objects, where an object inherits from a prototype object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly