473,811 Members | 3,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with abstract classes

Hello Guys

I'm a C++ programmer moving to C# and I'm trying to do something which
I regularly do in C++ and am wondering why it doesn't work in C#.

Basically in C++ I can do something similar to the following

class A{
public:
virtual void foo()=0;
};

class B : public A{
public:
virtual void foo()=0;
};

class C : public B{
public:
virtual void foo(){
cout << "Test";
}
};

Where class B and A are abstract classes with a pure virtual function
"foo"

If I try the equivalent in C#

public abstract class A {
public abstract void foo();
}
public abstract class B : A {
public abstract void foo();
}
public class C : B {
public override void foo() {
return "Hello Sailor";
}
}

I get....

error CS0533: 'B.foo()' hides inherited abstract member 'A.foo()'
error CS0534: 'C' does not implement inherited abstract member
'A.foo()'

Is this inheritance structure not valid in C# or am I missing
something.

Regards,

Fergal

Feb 24 '06 #1
3 10023
Try using the override keyword on the function definition in B.

Fergal Moran wrote:
Hello Guys

I'm a C++ programmer moving to C# and I'm trying to do something which
I regularly do in C++ and am wondering why it doesn't work in C#.

Basically in C++ I can do something similar to the following

class A{
public:
virtual void foo()=0;
};

class B : public A{
public:
virtual void foo()=0;
};

class C : public B{
public:
virtual void foo(){
cout << "Test";
}
};

Where class B and A are abstract classes with a pure virtual function
"foo"

If I try the equivalent in C#

public abstract class A {
public abstract void foo();
}
public abstract class B : A {
public abstract void foo();
}
public class C : B {
public override void foo() {
return "Hello Sailor";
}
}

I get....

error CS0533: 'B.foo()' hides inherited abstract member 'A.foo()'
error CS0534: 'C' does not implement inherited abstract member
'A.foo()'

Is this inheritance structure not valid in C# or am I missing
something.

Regards,

Fergal

Feb 24 '06 #2
public abstract class B : A
{
public *override* abstract void foo();
}
Fergal Moran wrote:
Hello Guys

I'm a C++ programmer moving to C# and I'm trying to do something which
I regularly do in C++ and am wondering why it doesn't work in C#.

Basically in C++ I can do something similar to the following

class A{
public:
virtual void foo()=0;
};

class B : public A{
public:
virtual void foo()=0;
};

class C : public B{
public:
virtual void foo(){
cout << "Test";
}
};

Where class B and A are abstract classes with a pure virtual function
"foo"

If I try the equivalent in C#

public abstract class A {
public abstract void foo();
}
public abstract class B : A {
public abstract void foo();
}
public class C : B {
public override void foo() {
return "Hello Sailor";
}
}

I get....

error CS0533: 'B.foo()' hides inherited abstract member 'A.foo()'
error CS0534: 'C' does not implement inherited abstract member
'A.foo()'

Is this inheritance structure not valid in C# or am I missing
something.

Regards,

Fergal

Feb 24 '06 #3
Thanks a log guys - perfect.
As also pointed out on another newsgroup - I could also have left out
the definition in class B completely.

Fergal.
Glad no-one noticed a void function returning a string :)

Feb 24 '06 #4

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

Similar topics

3
3154
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This design pattern is very similar to the design pattern applied by the Assembly class. The twist is...
12
3099
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already ordered. I'm just too curious about this one to wait for the book. I would like to know is if it's good php programming practice to use abstract classes instead of singleton classes. For exemple a login class. I've made one as an abstract class...
2
9610
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will define the behavior for most, but not all of its members. The derived classes will define the behavior for the remaining members (the undefined members). I'd like to force the derived classes to implement the undefined members in the base class. I...
9
5202
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
12
3028
by: scottt | last post by:
hi, I am having a little problem passing in reference of my calling class (in my ..exe)into a DLL. Both programs are C# and what I am trying to do is pass a reference to my one class into a DLL function. When I try and compile the DLL I get "The type or namespace name "MyForm" could not be found. I think I have to reference the class but since the DLL needs to be built before the EXE it looks like I have a chicken and egg type problem....
7
1807
by: ankitjain.bvcoe | last post by:
Hi i have the following problem in my design :::: i want to define an abstract class LogBuffer and derive two singleton classes from it i.e AlarmBuffer and FireWallBuffer.For this my design is such that i have to define data members in class LogBuffer.i.e ************************************************************************************ class LogBuffer
7
4478
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
0
2840
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass magic, I wrote the following module. It is mainly useful as a light weight tool to help programmers catch mistakes at definition time (e.g., forgetting to implement a method required by the given interface). This is handy when unit tests or...
0
2798
by: Kristopher Wragg | last post by:
Hi, I'm having a problem with XMLSerializer, I have classes like the following: public abstract class EulerObject { private Rectangle rect; private string guid = "";
5
3019
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if C# even has Iinterfaces. I took some Java programming classes and Interfaces are a main staple of Java. And in VB.Net I use interfaces for setting up definitions of classes. I am guessing that Abstract classes in C# do the same thing as...
0
9730
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10392
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...
0
10136
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...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6893
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
5555
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...
1
4341
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
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.