469,331 Members | 4,340 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

What is the use of interface

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
4 20454
"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
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
"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
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.

Similar topics

3 posts views Thread by Amit chaturvedi | last post: by
21 posts views Thread by Helge Jensen | last post: by
6 posts views Thread by John Salerno | last post: by
1 post views Thread by Mitan | last post: by
19 posts views Thread by Charles Law | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.