OOP + Four Pillars Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is OOP?

A

Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around objects and data, rather than actions and logic. It allows developers to create modular, reusable code by representing real-world objects as software objects with properties (attributes) and behaviors (methods). Key concepts in OOP include:

Classes and Objects:

Class: A blueprint or template for creating objects. It defines the properties and behaviors common to all objects of that type.

Object: An instance of a class that encapsulates data (properties) and behaviors (methods).

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

Encapsulation

A

Description:
Encapsulation is the bundling of data (variables) and methods (functions) that operate on the data into a single unit, called a class. It serves two main purposes: information hiding and abstraction. Information hiding means that the internal representation of an object is hidden from the outside world, and only the necessary details are exposed. Abstraction allows us to focus on what an object does instead of how it does it, simplifying the complexity of the system.

public class Player {
private int health; // Encapsulated variable

public void TakeDamage(int damageAmount) {
    health -= damageAmount;
    if (health <= 0) {
        Die();
    }
}

private void Die() {
    // Handle player death
} }

In this example, health is encapsulated within the Player class as a private variable. The public method TakeDamage() allows controlled access to modify the health variable, ensuring that changes to health are validated and handled internally without exposing the internal state directly.

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

Inheritance

A

Description:
Inheritance is a mechanism in object-oriented programming where one class (child class or subclass) derives from another class (parent class or superclass), inheriting its properties and behaviors (methods and fields). This allows for code reuse and facilitates the creation of hierarchical relationships between classes.

public class Character {
public void Move() {
// Base movement logic
}

public void Attack() {
    // Base attack logic
} }

public class Player : Character {
public void UsePowerUp() {
// Additional functionality specific to Player
}
}

Here, Player inherits from Character. It gains access to the Move() and Attack() methods defined in Character and can also add its own method UsePowerUp(). This inheritance relationship allows Player to reuse and extend the functionality defined in Character.

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

Polymorphism

A

Description:
Polymorphism means “many forms” and refers to the ability of different objects to be treated as instances of the same class hierarchy. It allows methods to be defined in the superclass and overridden in subclasses, providing a way to implement common interfaces while still allowing each subclass to provide its own specific implementation.

public class Shape {
public virtual void Draw() {
Console.WriteLine(“Drawing a shape”);
}
}

public class Circle : Shape {
public override void Draw() {
Console.WriteLine(“Drawing a circle”);
}
}

public class Rectangle : Shape {
public override void Draw() {
Console.WriteLine(“Drawing a rectangle”);
}
}

In this example, Shape is the superclass with a virtual Draw() method. Both Circle and Rectangle inherit from Shape and override the Draw() method to provide their specific drawing implementations. At runtime, polymorphism allows you to treat a Circle or Rectangle object as a Shape, calling their respective Draw() methods based on their actual types.

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

Summary

A

Encapsulation bundles data and methods into a class, promoting information hiding and abstraction.

Inheritance enables code reuse and hierarchical relationships between classes.

Polymorphism allows objects of different types to be treated as instances of a common superclass, facilitating flexibility and extensibility in code design.

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