C
C#3mo ago
stanfx

help understanding something

do I always have to write all the methods in the abstract class first then override them in the classes that inherit from it? Or is there a way to leave the abstract class empty and only create the methods in the subclasses
6 Replies
canton7
canton73mo ago
Feel free to define methods in a subclass which aren't defined in the abstract class... But, then someone who just has a variable which has the type of the abstract class won't know that those methods exist
stanfx
stanfx3mo ago
oh I see now
canton7
canton73mo ago
public abstract class A
{
}

public class B : A
{
public void Foo() { }
}

A a = new B();
a.Foo(); // Error, A doesn't have a method called Foo
public abstract class A
{
}

public class B : A
{
public void Foo() { }
}

A a = new B();
a.Foo(); // Error, A doesn't have a method called Foo
stanfx
stanfx3mo ago
so when that error happens I need to define the method in the parent class
canton7
canton73mo ago
Well, you define a method in the parent class when you want people who have a variable whose type is the parent class to be able to access it
stanfx
stanfx3mo ago
I understand now, thank you for your help 🙏