PC Review


Reply
Thread Tools Rate Thread

Abstract method in non-abstract class

 
 
Chris Zopers
Guest
Posts: n/a
 
      8th Aug 2008
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

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
 
Rory Becker
Guest
Posts: n/a
 
      8th Aug 2008
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?

--
Ror


 
Reply With Quote
 
Göran Andersson
Guest
Posts: n/a
 
      8th Aug 2008
Chris Zopers wrote:
> 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
>
> *** Sent via Developersdex http://www.developersdex.com ***


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.

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pure virtual method in a non abstract and a non interface class Sharon Microsoft C# .NET 2 1st Oct 2009 01:15 PM
what is abstract class and abstract method N.RATNAKAR Microsoft C# .NET 4 19th Oct 2006 08:23 PM
protected internal method on an abstract base class ssg31415926 Microsoft C# .NET 3 1st Mar 2006 02:54 PM
Using reflection to get the class name from a virtual method in an abstract class Kevin Grigorenko Microsoft Dot NET Framework 4 28th Mar 2005 08:05 PM
Abstract members in non-abstract classes? Dave Veeneman Microsoft C# .NET 2 13th Nov 2003 10:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:26 PM.