By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,948 Members | 1,595 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,948 IT Pros & Developers. It's quick & easy.

What is the use of interface

P: n/a
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

Jun 7 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Raja Chandrasekaran" <Ra*****@gmail.com> a écrit dans le message de news:
11**********************@j55g2000cwa.googlegroups. com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 7 '06 #2

P: n/a
See;

http://en.wikipedia.org/wiki/Virtual_inheritance
"Raja Chandrasekaran" wrote:
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

Jun 7 '06 #3

P: n/a
"Shawnk" <Sh****@discussions.microsoft.com> a écrit dans le message de news:
BF**********************************@microsoft.com...

| http://en.wikipedia.org/wiki/Virtual_inheritance

This article doesn't talk about interfaces at all, which is what the OP
asked. Did you get the right link ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 7 '06 #4

P: n/a
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_%28Java%29
http://en.wikipedia.org/wiki/Interfa...ter_science%29

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



"Joanna Carter [TeamB]" wrote:
"Shawnk" <Sh****@discussions.microsoft.com> a écrit dans le message de news:
BF**********************************@microsoft.com...

| http://en.wikipedia.org/wiki/Virtual_inheritance

This article doesn't talk about interfaces at all, which is what the OP
asked. Did you get the right link ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Jun 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.