Abstract method in non-abstract class

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
 
R

Rory Becker

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?
 
G

Göran Andersson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top