473,804 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about interface

Hi:

C# doesn't support multiple inheritance but it supports interface.But I
think these ways are different.

For example, C++ supports multiple inheritance. I have two classess classA
and classB:

class classA(){
......
public void fly(){
......
}
......
}

class classB(){
......
public void swim(){
......
}
......
}

Now we can create another class "classC" which inherits from "classA" and
"classB".
So classC will have the features of fly and swim. The important is we don't
need do anything else.

But in C#, it doesn't support multiple inheritance. Now, if I need to create
a class "classC" and it will have the "fly" and "swim" functions in the
existing classes "classA" and "classB", how can I do ?

I think one solution is
- create an interafce "interfaceS wim" and "swim" method;
- classB inherits from classA and interfaceSwim, and implements the swim
method;
- create classC which inherits from classB.

But there will be one more level of inheritance architecture. Is it correct?
or better solution?

Thanks

Q.
Nov 17 '05 #1
3 1111
Actually I would just implement an abstract method, Travel(), and let *its*
implementations in child classes decide whether to fly or swim.

Multiple inheritance is one of those sexy ideas that degenerates into a
quagmire of spaghetti very quickly. Interfaces let you implement a species
of mixins which is IMO about all MI is good for anyway.

If you really want MI, however, the Eiffel.NET language *does* provide it.
It even provides generics without waiting for CLR 2.0.

--Bob

"Quentin Huo" <q.***@manyworl ds.com> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
Hi:

C# doesn't support multiple inheritance but it supports interface.But I
think these ways are different.

For example, C++ supports multiple inheritance. I have two classess classA
and classB:

class classA(){
......
public void fly(){
......
}
......
}

class classB(){
......
public void swim(){
......
}
......
}

Now we can create another class "classC" which inherits from "classA" and
"classB".
So classC will have the features of fly and swim. The important is we
don't need do anything else.

But in C#, it doesn't support multiple inheritance. Now, if I need to
create a class "classC" and it will have the "fly" and "swim" functions in
the existing classes "classA" and "classB", how can I do ?

I think one solution is
- create an interafce "interfaceS wim" and "swim" method;
- classB inherits from classA and interfaceSwim, and implements the swim
method;
- create classC which inherits from classB.

But there will be one more level of inheritance architecture. Is it
correct? or better solution?

Thanks

Q.

Nov 17 '05 #2

"Quentin Huo" <q.***@manyworl ds.com> skrev i meddelandet
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
Hi:

C# doesn't support multiple inheritance but it supports interface.But I
think these ways are different.

For example, C++ supports multiple inheritance. I have two classess classA
and classB:

class classA(){
......
public void fly(){
......
}
......
}

class classB(){
......
public void swim(){
......
}
......
}
Now we can create another class "classC" which inherits
from "classA" and "classB".
So classC will have the features of fly and swim. The
important is we don't need do anything else.

But in C#, it doesn't support multiple inheritance. Now, if I need to
create a class "classC" and it will have the "fly" and "swim" functions in
the existing classes "classA" and "classB", how can I do ?


One fairly common way is to use composition:
interface IFlyer {
void fly();
}

interface ISwimmer {
void swim();
}
class ClassA : IFlyer {
......
public void fly(){
......
}
......
}

class ClassB : ISwimmer {
......
public void swim(){
......
}
......
}

class ClassC : IFlyer, ISwimmer {

......
ClassA a;
ClassB b;
......

public void fly(){
a.fly();
}

public void swim() {
b.swim();
}
}

// Bjorn A
Nov 17 '05 #3
One variation on this is to simply make the composed parts available, while
hiding their construction. You lose substituability and you lose the
ability for the descendent class to access private members, but you get a
flexible interface that doesn't need to be changed when the parent types
change.

class ClassC {

......
ClassA _a;
ClassB _b;

public ClassC {
_a = new ClassA();
_b = new ClassB();
}
......

public ClassA a {
get { return _a;}
}

public ClassB b {
get { return _b;}
}
}

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Bjorn Abelli" <bj**********@D oNotSpam.hotmai l.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..

"Quentin Huo" <q.***@manyworl ds.com> skrev i meddelandet
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
Hi:

C# doesn't support multiple inheritance but it supports interface.But I
think these ways are different.

For example, C++ supports multiple inheritance. I have two classess
classA and classB:

class classA(){
......
public void fly(){
......
}
......
}

class classB(){
......
public void swim(){
......
}
......
}
Now we can create another class "classC" which inherits
from "classA" and "classB".
So classC will have the features of fly and swim. The
important is we don't need do anything else.

But in C#, it doesn't support multiple inheritance. Now, if I need to
create a class "classC" and it will have the "fly" and "swim" functions
in the existing classes "classA" and "classB", how can I do ?


One fairly common way is to use composition:
interface IFlyer {
void fly();
}

interface ISwimmer {
void swim();
}
class ClassA : IFlyer {
......
public void fly(){
......
}
......
}

class ClassB : ISwimmer {
......
public void swim(){
......
}
......
}

class ClassC : IFlyer, ISwimmer {

......
ClassA a;
ClassB b;
......

public void fly(){
a.fly();
}

public void swim() {
b.swim();
}
}

// Bjorn A

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
5398
by: perseus | last post by:
I think that everyone who told me that my question is irrelevant, in particular Mr. David White, is being absolutely ridiculous. Obviously, most of you up here behave like the owners of the C++ language. A C++ interface installation IS ABOUT THE C++ LANGUAGE! The language does not possess the ability to handle even simple file directory manipulation. Those wise people that created it did not take care of it. So, BOOST is a portable...
10
3383
by: David Rubin | last post by:
This is a question related to my previous post titled "private member functions:" I recently saw that a colleage of mine submitted code which defines a class Interface as follows: class Interface { public: Interface(UINT maxidx);
4
8583
by: jm | last post by:
Consider: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconwhenshouldiimplementinterfacesinmycomponent.asp // Code for the IAccount interface module. public interface IAccount { void PostInterest(); void DeductFees(IFeeSchedule feeSchedule); }
13
3267
by: John Salerno | last post by:
Hi all. I have a question about interfaces now. According to the book I'm reading, when you implement an interface, the class or structure has to declare all the methods that the interface includes. My question is, if you must declare each method (as opposed to simply 'using' those methods like an #included file), then why not just declare the methods on your own as if you created them yourself? What advantage does using an interface...
0
918
by: Kristof Thys | last post by:
I'm creating a ASP.net webservice written in c++, so I'm posting my question on both the aspnet and vc newsgroups. I hope somebody can help me... What I want to do seems basic c++ to me... I create a template ASP.net webservice. In the helloworld function, I create an instance of a wrapperclass (wich wraps unmanaged C++), and call a function upon it, like this:
5
1673
by: Colin McGuire | last post by:
Hi all, when I write the class below Private Class employee End Class and then add the line "Implements IVF" which is an interface I have written, the IDE modifies my code to display
7
4477
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears to be checking that the correct data type is used DataAcess sets an abstract class and methods
6
282
by: Robert Dufour | last post by:
What is the meaning of the word marshal and unmarshal in plain english as applied to an exe file? Does it mean the application has started and ended? Thanks for any help, Happy new year, Bob
1
3224
by: =?Utf-8?B?Sk0=?= | last post by:
In an application I have an interface with methods and properties. The interface is used on a Class (ie class MyClass : IMyClassA, IMyClassB). On a windows form I define a BindingSource (_bindingMyClass) where I set the DataSource property to the interface IMyClass. Now when the MyClass object is instantiated I set the BindingSource to the instance of the IMyClass interface (_MyClass) _bindingMyClass.DataSource = this._MyClass;
4
1629
by: sip.address | last post by:
Hi there, When creating interfaces and implementations, the usual thing is doing somethign like class Interface { public: virtual void f() = 0; virtual void g() = 0; };
0
10603
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10353
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.