Abstract method in non-abstract class

Discussion in 'ASP .Net' started by Chris Zopers, Aug 8, 2008.

  1. Chris Zopers

    Chris Zopers Guest

    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 ***
     
    Chris Zopers, Aug 8, 2008
    #1
    1. Advertising

  2. Chris Zopers

    Rory Becker Guest

    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
     
    Rory Becker, Aug 8, 2008
    #2
    1. Advertising

  3. 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
     
    Göran Andersson, Aug 8, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler
    Replies:
    1
    Views:
    443
    R.F. Pels
    May 22, 2005
  2. Sameer
    Replies:
    4
    Views:
    605
    Roedy Green
    Aug 31, 2005
  3. Replies:
    4
    Views:
    824
    Rolf Magnus
    May 17, 2006
  4. Stefan Ram
    Replies:
    27
    Views:
    745
    Stefan Ram
    Aug 17, 2009
  5. Marcel Müller
    Replies:
    2
    Views:
    121
    Marcel Müller
    Mar 15, 2014
Loading...

Share This Page