What is abstraction?
I'll keep it fairly short. Abstraction focuses on simplifying complex systems by modeling classes based on essential characteristics, hiding unnecessary details from the user.
Someone who buys a car does not necessarily need to know how to fix it; that will be abstracted to a mechanic. The mechanic is not required to know how to design the car; that's abstracted to the engineers.
Let's look at an example (Inheritance understanding is assumed)
public class Vehicle
{
private float speed = 0f;
public void Accelerate()
{
// Physics calculations to accelerate the vehicle
}
public void Decelerate()
{
// Physics calculations to decelerate the vehicle
}
}
public class Car : Vehicle
{
private int doors;
private Color color;
public Car(int doors, Color color)
{
this.doors = doors;
this.color = color;
}
}
public class SomeCarModel : Car
{
private string name;
public SomeCarModel(string name, int doors, Color color) : base(doors, color)
{
this.name = name;
}
}
public class CarSpawner
{
public CarSpawner()
{
SomeCarModel someCarModel = new SomeCarModel("someCar", 4, new Color(100, 100, 100));
if (userInput.w)
{
someCarModel.Accelerate();
}
}
}
Now, someone can add a new car without worrying about creating the logic to make the car move; we have abstracted that away.
Other abstraction topics we will focus on in the future are:
- Data Abstraction
- Control Abstraction
- Abstraction Levels
- Abstract Classes
I am creating these articles for my Advanced OOP roadmap for GameDev, which you can visit here over on roadmap.sh
Top comments (0)