473,395 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 20624
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: HAM | last post by:
One of my friends asked if the followings have any meanings? '------------- Public Interface IRenderable Sub Render() MustInherit Class Engine MustOverride Sub TurnOn() Interface IAutomatic...
12
by: Steve W. | last post by:
I just read the section (and did the exercise) in the C# Step by Step book that covers Explict Interface Implementation (where you specify in the method implementation the specific interface that...
3
by: Amit chaturvedi | last post by:
Dear Friends Please tell me following thing Que -: what is the main use of interface in .net Que -: What is difference between abstract class and interface Que -: How to make class in Object...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
6
by: John Salerno | last post by:
I understand how they work (basically), but I think maybe the examples I'm reading are too elementary to really show their value. Here's one from Programming C#: #region Using directives ...
1
by: Mitan | last post by:
Hello, I'm a beginner with what appears to be a simple question for which I haven't been able to get an answer. Can someone explain what "implementation code" is in relation to VB.NET? I want to be...
19
by: Charles Law | last post by:
Take a solution with a project hierarchy along the lines of an n-tier system, so that we have a data layer, business layer and presentation layer. The presentation layer is coupled to the business...
17
by: Zytan | last post by:
Aren't all classes interfaces? What constitutes an interface (and with it, the "I" prefix distinction)? Zytan
6
by: S_K | last post by:
Hi, I've been toying around with interfaces in C#. They are fun but can anybody give me some examples of where interfaces are used and what they are used for? Thanks so much. Steve
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.