C
C#3mo ago
Ewan

I dont fully understand what abstract classes are for

1. What does it mean when they cant make instances of the classes? does that mean if I were to make an abstract class "student" then I cant do "student student1 = new student"? is that what it means? i cant make an object from it? 2. Can I take methods out of them and override them in other classes? do i have to inherit the abstract class first? 3 Can u only override abstract and virtual methods? if so then whats the difference 4. What does it mean and what can i do when i see a method that isnt abstract like static in an abstract class? is the only way for me to use that method is using another class to inherit from it? Help is greatly appreciated
9 Replies
Angius
Angius3mo ago
1. It means you can't do var foo = new AbstractClass(); 2. You have to inherit from the abstract class to override its methods, yes 3. Sure. Abstract methods have to be overriden, while virtual ones can be 4. ??? Could you specify what you mean by "use that method"? Because you can use methods of class Foo in class Bar without one inheriting from another, yes
Ewan
Ewan3mo ago
so am i correct on the first question? for the fourth i mean like, i saw an abstract class with a static method inside it, u cant override this can you? normally i just see abstract classes where every method in the class u can override because theyre abstract as well. what can i do with a static method inside an abstract class? can there be other method types too? i thoguht they all had to be abstract methods so they can be overidden, how could i use the static method thats inside the abstract class?
Angius
Angius3mo ago
An abstract class can have non-abstract members, yeah
Ewan
Ewan3mo ago
oh so i can just take those out and use them in other stuff but i HAVE to inherit it first to use them right?
Angius
Angius3mo ago
$tias
Ewan
Ewan3mo ago
ok ill have look thanks ;-;
Becquerel
Becquerel3mo ago
public abstract class Test
{
public static void SayHello() => Console.Write("Hello world");
}

public class OtherClass
{
public void OtherMethod()
{
Test.SayHello();
}
}
public abstract class Test
{
public static void SayHello() => Console.Write("Hello world");
}

public class OtherClass
{
public void OtherMethod()
{
Test.SayHello();
}
}
You are able to use static methods/members on abstract classes Because abstract doesn't mean 'you need to inherit from this to use it', it means 'you cannot instantiate an instance of this class' and static methods/members aren't attached to class instances anyhow
Ewan
Ewan3mo ago
ah i see thanks