16.5. Object Orientation in C#: Abstraction

Página 21

Object orientation is a programming paradigm that allows developers to structure their programs in a more intuitive and flexible way. In this context, abstraction is one of the main concepts that support object orientation in C#. Let's explore this concept in detail.

Abstraction, in object-oriented programming (OOP) terms, is the process of identifying the essential characteristics and behaviors of an object while ignoring non-essential details. In other words, abstraction allows programmers to focus on the 'what' and ignore the 'how'. This makes the code easier to read and maintain, and allows for a greater degree of code reuse.

In C#, abstraction is implemented through abstract classes and interfaces. An abstract class is one that cannot be instantiated directly, but can be inherited by other classes. An abstract interface is similar to an abstract class, but it does not contain any method implementations - just method declarations. Both are useful for defining 'contracts' that other classes must follow.

For example, imagine we are creating a game that has several different types of characters, such as warriors, wizards, and archers. Each of these characters has their own specific abilities and behaviors, but they also share many common characteristics. They have hit points, can attack and defend, and can move around the game map.

Instead of defining these common characteristics in each character class individually, we can define an abstract 'Character' class that contains the implementations of these common behaviors and characteristics. Then each specific character class (Warrior, Mage, Archer, etc.) can inherit from this abstract 'Character' class and add or replace behaviors as needed.

public abstract class Character
{
    public int LifePoints { get; set; }
    public abstract void Attack();
    public abstract void Defender();
    public abstract void Move();
}

Specific character classes can then implement these abstract methods in ways that are appropriate for their unique abilities and behaviors.

public class Warrior: Character
{
    public override void Attack()
    {
        // Warrior-specific attack implementation
    }

    public override void Defender()
    {
        // Warrior specific defense implementation
    }

    public override void Move()
    {
        // Warrior-specific movement implementation
    }
}

This allows programmers to focus on what each character should do, rather than how they do it. Additionally, if we decide to change the way characters move or attack, we only need to make that change in one place (the 'Character' abstract class), rather than each character class individually.

In summary, abstraction in C# and OOP is a powerful tool that helps developers write code that is easier to understand, maintain, and reuse. It's a fundamental concept that all Unity game developers should understand and apply to their projects.

Now answer the exercise about the content:

What is abstraction in object-oriented programming in C# and what is its role?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

2216.6. Object Orientation in C#: Interfaces

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text