Any good guides/videos to learn Abstract Classes?
As an exercise to begin using C# I´m creating a basic text game as a Console App. But im stuck trying to understand how to use Abstract classes.
Any guide or help is appreciated.
12 Replies
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members they have some docs on it, dunno if it will help you much though
Abstract and Sealed Classes and Class Members - C#
The abstract keyword in C# creates incomplete classes and class members. The sealed keyword prevents inheritance of previously virtual classes or class members.
its literally just a class that cant be instantiated directly, ie you cant do
new MyAbstractClass()
the idea is that if you have code that needs to be shared among serveral types, they can inherit the abstract classthanks
im still having trouble with it, if you are free, can we talk it on DM?
We don't really do DMs on this server, you're free to just ask here.
Means other people can learn too, or if I make a mistake someone can correct me.
Win-win
makes sense
Do you know about inheritance in general? Interfaces?
i have to go to a medic check up rigth now, but ill be back in at least 1 hour
00:45 for me, very unlikely I'm awake then
but others will be
so just write here 🙂
i sort of understand it yeah
ill write the more specifics of what im trying to get done before leaving in case someone knows how to solve it
well then you actually understand abstract classes! its just an extreme version of normal inheritance, where you must inherit it to use it
the idea i have is to use the abstract classes to create an inventory sistem with a max of 5 objects, each with 3 variables
I could kinda see how an abstract class could be used with an item system in general, like you could have a
BaseItem
class or something
but inheritance tends to break down poorly in situation like that, if you ask me
Consider if you make a hierarchy like...
Item > EquippableItem > Weapon > Sword
and then you want to make a sword that deals ice damage, instead of slashing
so you make Sword > IceSword
but then you want to make another sword that is both ice AND fire... oops, what do we do now?
do we base it on IceSword, or FireSword? or just on Sword itself?
what if the sword also acts as a fluid container? (crazy I know but bear with me)
this is where "composition over inheritance" comes from
inheritance solves some problems nicely, like the "is a" question
a dog is an animal
but for games, composition tends to give better results