hungryMind
hungryMind

Reputation: 7009

Force abstract implementation to child of child

I want something like this

    public abstract class abc
    {
        public abstract void test();
    }

    public class def : abc
    {
            // ignore test(), this is concrete class which can be initialized
            // test method is not needed for this class
    }

    public class ghi : def
    {
        public override void test()
        {
            // force test method implementation here
        }
    }

What are possible ways to do that. I want to ignore use of interface at GHI class as these are not under our application.

Edit

All of you are correct, but I need similar implementation. Point is I have various objects which has common functionality so I inherited from a class. I want to give this class to other ppl who must implement test method.

Upvotes: 0

Views: 2863

Answers (6)

Ravindra-Ravi Verma
Ravindra-Ravi Verma

Reputation: 535

You need to make def class as abstract class.

Upvotes: 0

Hari Subramaniam
Hari Subramaniam

Reputation: 1876

You can have the Test method in your base class as Virtual and leave the method body blank. Therefore you can override it wherever you want to. This is more of a hack and using an interface is a better way to go.

public abstract class abc
{
    public virtual void test()
    {
    }
}

public class def : abc
{
        // ignore test(), this is concrete class which can be initialized
        // test method is not needed for this class
}

public class ghi : def
{
    public override void test()
    {
        // force test method implementation here
    }
}

Or

You can have another abstract class

public abstract class abc
{
}

public abstract class lmn : abc
{
 public abstract void Test();
}
public class def : abc
{
    // ignore test(), this is concrete class which can be initialized
    // test method is not needed for this class
}

public class ghi : lmn
{
 public override void test()
 {
    // force test method implementation here
 }

} NOTE - This abstraction is completely dependent upon your domain. The suggestion is just a technical way to do it. Not sure if its aligned with the problem domain at hand.

Upvotes: 0

Tipx
Tipx

Reputation: 7515

Edits in bold. Abstract means you MUST implement it in your child classes. If you want to force the implementation of a functionality that not every "inheriters" have, you should be using interfaces. Here is what I would do :

public abstract class abc
{
    // Everything you want here, but not "Test()".
}

public class def : abc
{
}

public class ghi : def, ITestable
{
    public void ITestable.Test()
    {
    }
}

public interface ITestable
{
    void Test();
}

Upvotes: 1

hcb
hcb

Reputation: 8357

There isn't a way. This is a definition problem, you say "i want every class which inherits from this class to have this method" and then try to create a child without it.

Upvotes: 0

fixagon
fixagon

Reputation: 5566

its not possible what you are doing. You can not add an abstract method to the base class (abc) which doesnt habve to be implemented in a class (def) which inherits from the base class or it has to be abstract too

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292765

It's not possible. def has to implement test, unless it is abstract too.

Upvotes: 2

Related Questions