Abstract method in non-abstract class

  • Thread starter Thread starter Chris Zopers
  • Start date Start date
C

Chris Zopers

Hello,

I would like to know if it's possible to mark a method as abstract in a
non-abstract class, like this:

public class Test
{
public string NormalMethod()
{
return "somevalue";
}

public abstract string AbstractMethod();
}

When I compile this code I get an error indicating that an abstract
method in a nonabstract class is not possible. But I would like to
inherit from the class and all of it's functionality, only one method
has to be overriden by the inheriting class. Can I do this maybe in an
other way?

Greetings,
Chris
 
Hello Chris,
Hello,

I would like to know if it's possible to mark a method as abstract in
a non-abstract class, like this:

public class Test
{
public string NormalMethod()
{
return "somevalue";
}
public abstract string AbstractMethod();
}
When I compile this code I get an error indicating that an abstract
method in a nonabstract class is not possible. But I would like to
inherit from the class and all of it's functionality, only one method
has to be overriden by the inheriting class. Can I do this maybe in an
other way?

Your class needs to be abstract.

Imagine if it were directly constructable.

What would happen when someone called your abstract emthod?
 
Chris said:
Hello,

I would like to know if it's possible to mark a method as abstract in a
non-abstract class, like this:

public class Test
{
public string NormalMethod()
{
return "somevalue";
}

public abstract string AbstractMethod();
}

When I compile this code I get an error indicating that an abstract
method in a nonabstract class is not possible. But I would like to
inherit from the class and all of it's functionality, only one method
has to be overriden by the inheriting class. Can I do this maybe in an
other way?

Greetings,
Chris

That's exactly what the abstract class is for. You can implement some
methods and leave some methods abstract to be implemented by the
inheriting class.

Also consider if virtual methods is useful for you, where you can make a
default implementation in the base class, and override it in the
inheriting class with an implementation specific for that class.
 
Back
Top