M.Azad
M.Azad

Reputation: 3763

Inheritance from interface Or Impliment interface?

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... ...

Upvotes: 0

Views: 231

Answers (5)

b0rg
b0rg

Reputation: 1897

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

Upvotes: 0

Java
Java

Reputation: 2489

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(){

    }
    }

Upvotes: 0

adelphus
adelphus

Reputation: 10328

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.

Upvotes: 0

Alex
Alex

Reputation: 35399

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.

Upvotes: 5

RobSiklos
RobSiklos

Reputation: 8849

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

Upvotes: 0

Related Questions