-1

We have an interface with four methods initialised.

interface A
{
    double S(double R);
    double L(double R); 
    double V(double R,double h);
    double W(double R,double h);   
}

Is it possible to implement only some methods in its derived class?

class Ball:A
{
  public double S(double R)
    {
        return Math.PI * R * R * R;
    }
    public double L(double R)
    {
         return 2*Math.PI * R ;
    }

}

4
  • 3
    No. You should split this into two or more interfaces, or use an abstract class. May 22, 2014 at 11:31
  • no thats not possible unless you make Ball abstract May 22, 2014 at 11:31
  • Even with an abstract class, you will have to declare all the methods, though some of them can be abstract, you would still need to have them present. May 22, 2014 at 11:33
  • @LasseV.Karlsen Exactly. May 22, 2014 at 11:35

7 Answers 7

2

Interfaces are used when you have to enforce certain properties on an object (class).

Say for example: we have a car class and a motorbike class and a truck class. Each of these three classes should have a StartEngine() action. How the "engine is started" for each vehicle is left to each particular class, but the fact that they must have a StartEngine action is the domain of the interface. So, it makes the implementation of each method of an interface is compulsory.

So we can group some list of functionality in an interface and whenever a class implements the interface it needs to fulfill the contract. So, we cannot just implement some of the functionality of an interface. Actually, They are just to make sure that the functions (in the interface) are implemented in the inheriting class.

We can split functionality like bellow:

 interface B
    {
        double S(double R);
        double L(double R); 
    }

 interface A: B
    {
       double V(double R,double h);
       double W(double R,double h);   
    }
0

Is it possible, in a his derived class use only some methods?

No Its Not Possible.

Instead of interface try to use any of the options

  1. Abstract class
  2. NotImplementedException for unused methods on derived class
0

No, though you can declare a base class implementing dummy-versions of those methods.

However, this seems like bad design.

If a ball does not have a V or W operation then perhaps you should not lump all of those methods into the same interface.

1
  • What if I need to create an interface for calculating the area of some objects. Like method overloading, when we use only one version of the method in a class? As a point of design, how to make it?
    – Dmytro
    May 22, 2014 at 11:43
0

No it's not possible. You have to implement them at least as empty method doing nothing or method that throws exception.

Althought this will probably not good spot to use interface for that kind of funcionality since interface is feature that ensures that class which implement it have at least full set of methods defined in interface.

2
  • 1
    That second bit seems like a fairly bad anti-pattern. May 22, 2014 at 11:32
  • I agree, it's very bad way to get that code compiled. May 22, 2014 at 11:35
0

NO its not possible

as stated at official website http://msdn.microsoft.com/en-in/library/orm-9780596521066-01-13.aspx

An interface is a contract. When you design an interface, you're saying, "If you want to provide this capability, you must implement these methods, provide these properties and indexers, and support these events." The implementer of the interface agrees to the contract and implements the required elements.

When you define a class that implements an interface, you must then implement all the required members of that interface.

Inshort if you want to take just 2 methods as you have mentioned I would suggest take one more interface for the same because whatever you are thinking is not possible.

0

Following the SOLID design principles, particularly Interface segregation, it results that your interface should be split into more than one interface. As it has been answered already, what you asked is not possible in .NET. More on solid here

Another option is to have interface A only with required methods, then interface B deriving from A. The Ball class then will implement interface A.

-2

You can use explicit implementation:

class Ball : A
{
    public double S(double R)
    {
        return Math.PI * R * R * R;
    }
    public double L(double R)
    {
        return 2 * Math.PI * R;
    }

    double A.V(double R, double h)
    {
        throw new NotImplementedException();
    }

    double A.W(double R, double h)
    {
        throw new NotImplementedException();
    }
}

MSDN Link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.