0

I have an interface like this:

   interface  IService
    {
    ....
    }
    public  class myservice:IService
    {
    ...
    }

myservice class Impliment IService interface; if I say that

myservice class Inherit from IService

is it wrong?

if its wrong why for multiple inherit we have this definition:

Multiple inheritance is a feature of some object-oriented computer programming languages in which a class can inherit behaviors and features from more than one superclass.

Languages that support multiple inheritance include: C++, Common Lisp (via CLOS), EuLisp (via The EuLisp Object System TELOS), Curl, Dylan, Eiffel, Logtalk, Object REXX, Scala (via the use of mixin classes), OCaml, Perl, Perl 6, Python, and Tcl (via Incremental Tcl).1

Some object-oriented languages, such as C#, Java, and Ruby implement single inheritance, although protocols, or "interfaces," provide some of the functionality of true multiple inheritance.. multiple inheritance

If interface provide multiple inheritance ...I can say I Inherit from an interface... ...

M.Azad
  • 3,673
  • 8
  • 47
  • 77
  • Think about it, what would it inherit from an interface? no implementation is defined in an interface! They are just contracts. – Louis Kottmann Mar 12 '12 at 13:55
  • 1
    Baboon - you inherit the contract. Like from abstract methods. – H H Mar 12 '12 at 14:00
  • 1
    More detailed explanation from someone that asked the same question, in `C++`, that still applies to `C#`: http://stackoverflow.com/questions/3774204/difference-between-interface-inheritance-and-implementation-inheritance#answer-3774277 – Alex Mar 12 '12 at 14:11

5 Answers5

5

Classes implement interfaces.

Classes inherit other classes that aren't sealed.

Interfacees by definition have no implementation, therefore no behavior to inherit. On the other-hand, classes can have implementations, thus the distinction.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
Alex
  • 34,899
  • 5
  • 77
  • 90
0

Yes - it is wrong to say that myservice inherits from IService

RobSiklos
  • 8,348
  • 5
  • 47
  • 77
  • But isn't the widly accepted term of interface implementation "implements" and not "inherits"? "Inherits" as term is (At least to my limited knowledge) used in relation with class-inheritance and not interface-implementation. Aren't these two abstractions clearly separated (Interface: has-A, Inheritance: Is-A). – Alex Mar 12 '12 at 14:05
  • @Alex An interface is still an Is-A relationship. If MyClass implements IService, it is accurate to say that MyClass is an IService, because anything that expects an IService will accept a MyClass. Containment is a Has-A relationship. – That Chuck Guy Mar 12 '12 at 14:31
  • Agreed, the "Has-a" more likely fits to composition and the likes. Implements and Inherits however have two seperate meanings in terms of OO. – Alex Mar 12 '12 at 14:41
0

A class inherits from another class and implements interfaces.

But you can construct interfaces which inherit from other interfaces:

interface IService
{
    void SomeServiceMethod();
}

interface IService2 : IService
{
    void SomeServiceMethod2();
}

Ultimately though, it's a class that always provides the implementation of interface methods.

adelphus
  • 10,116
  • 5
  • 36
  • 46
0

Not really. As interface concept says us, if any class want to use particular interface then he needs to implements all of these methods,and yes For this statement there is one subway which is , if any particular class do not wish to implement all the methods of interface he can declare his class as Abstract class.

So in short A class implements interface and

class inherits properties from other parent classes. (Example in case of java)

    interface IService
    {
        void Method1();
        void Method2();
    }


class A implements IService{

void Method1(){

}
void Method2(){

}

}

or

Abstract class B implements IService{
    void Method2(){

    }
    }
Java
  • 2,451
  • 10
  • 48
  • 85
0

The way I explained to myself:

parent - child classes have a relationship, so it is inheritance. child inherits all parent functionality.

interface however is just a contract: child pretends to look alike IServiceClass, but does not inherit any of functionality because IServiceClass doesn't have any. therefore implements.

One of those things that you need to remember :)

A while ago there was silly interview question: "what's the difference between abstract class an interface?"... go figure

b0rg
  • 1,879
  • 12
  • 17