What is the use of interface

  • Thread starter Raja Chandrasekaran
  • Start date
R

Raja Chandrasekaran

Hai friends,

I really wonder, If the interface does not have any
definition, Y do we need to use interface. You can then only we can use
Multiple inheritance. I really cant understand, Just for declararion y
do we need to use interface. Anyhow the method name and definition ll
be in the derived class. Instead of that we can do all code in the
derived class itself right...? Then y these concept came. If anybody
know, please explain me with an example has right situation...

Thanks in advance
 
J

Joanna Carter [TeamB]

"Raja Chandrasekaran" <[email protected]> a écrit dans le message de (e-mail address removed)...

| I really wonder, If the interface does not have any
| definition, Y do we need to use interface. You can then only we can use
| Multiple inheritance. I really cant understand, Just for declararion y
| do we need to use interface. Anyhow the method name and definition ll
| be in the derived class. Instead of that we can do all code in the
| derived class itself right...? Then y these concept came. If anybody
| know, please explain me with an example has right situation...

The quick answer is that an interface is a contract that can be implemented
in a class.

You mention multiple inheritance; yes, one interface can inherit from more
than one other interface, but classes do not inherit from interfaces, they
implement them.

The difference between an interface and a class is that an interface is
purely a contractual definition that has no implementation at all. It is
similar to a pure abstract class, except that an abstract class can also
contain state fields and base behaviour.

Interfaces are intended to define the what without defining the how.

If you inherit from one class into another, then the derived class "is" also
the base class.

But because you can never derive from more than one base class at a time,
the ability to implement more than one interface gives people the impression
that interfaces provide multiple inheritance.

Another difference between an interface and a base class is that a base
class has to be the root of a hierarchy, vertical inheritance; whereas an
interface can be implemented across multiple hierarchies, a sort of
horizontal "inheritance"

A simple example of the differences is :

class Vehicle
{
}

class Car : Vehicle
{
}

interface ISteerable
{
void Steer();
}

class Car : Vehicle, ISteerable
{
void Steer()
{
// change direction of wheels
}
}

abstract class Boat : ISteerable
{
abstract void Steer();
}

class Speedboat : Boat
{
override void Steer()
{
// turn outboard motor
}
}

class Ship : Boat
{
override void Steer()
{
// turn rudder
}
}

Joanna
 
G

Guest

Joanna,

Thanks for pointing this out (always appreciate your comments).
As always pressed for time an I should have put in a few more links.

http://en.wikipedia.org/wiki/Virtual_inheritance
http://en.wikipedia.org/wiki/Multiple_inheritance
http://en.wikipedia.org/wiki/Interface_(Java)
http://en.wikipedia.org/wiki/Interface_(computer_science)

A key concept to understand is Implementation Inheritance (II) vs Virtual
Inheritance (VI).

This separates Multiple Inheritance (which has II and VI functionality) from
Interfaces (which only has VI functionality).

Although the links disgress from C# it helps to understand the Java/C++
architectures to better understand the single inheritance/interface
inheritance architecture of C#.

Understanding the semantics (logical meaning) of 'Type polymorphism',
'Virtual inheritance', 'Implementation Inheritance' and 'Mulitple
Inheritance' also helps to understand the abstract functionality of virtual
inheritance.

I often use WikiPedia as a terminology reference as it has helped me (and
hopefully others) to articulate design/architecture issues (independent of
our personal preferences in design choices).

Thanks, again, for pointing out my rather terse reply :)

Shawnk
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top