C
C#2w ago
[Helium]

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
Keswiik
Keswiik2w ago
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.
Pobiega
Pobiega2w ago
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 class
[Helium]
[Helium]OP2w ago
thanks im still having trouble with it, if you are free, can we talk it on DM?
Pobiega
Pobiega2w ago
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
[Helium]
[Helium]OP2w ago
makes sense
Pobiega
Pobiega2w ago
Do you know about inheritance in general? Interfaces?
[Helium]
[Helium]OP2w ago
i have to go to a medic check up rigth now, but ill be back in at least 1 hour
Pobiega
Pobiega2w ago
00:45 for me, very unlikely I'm awake then but others will be so just write here 🙂
[Helium]
[Helium]OP2w ago
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
Pobiega
Pobiega2w ago
well then you actually understand abstract classes! its just an extreme version of normal inheritance, where you must inherit it to use it
[Helium]
[Helium]OP2w ago
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
Pobiega
Pobiega2w ago
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

Did you find this page helpful?