473,805 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Implement interface for that class, not dervived classes

Hi, im trying to do something and just been caught off guard. I want
to implement an interface but only for certain class, but of course
when i derive from this base class the derived class also inherits the
interface.

public class Base : IGetType
{
protected byte m_type;

object IGetType.TypeId entifier
{
get { return m_type; }
}
}

public class Base2 : Base
{
}

void TestingFunc()
{
object obj = new Base2();
if (obj is IGetType)
{
// Base2 implements IGetType
}
}

I am checking objects because i am doing a recursive check against
arbitary types to see if they implement the interface. Now i want Base
to implement the interface, but not inherit it to Base2. Furthermore i
do need Base2 to inherit from Base. I know that is the definition of
inheritance, so maybe someone would be kind enough to think about how
else i could do this?

Many thanks,

Chris

Feb 2 '07 #1
5 1652
g1**@hotmail.co m wrote:
Hi, im trying to do something and just been caught off guard. I want
to implement an interface but only for certain class, but of course
when i derive from this base class the derived class also inherits the
interface.

public class Base : IGetType
{
protected byte m_type;

object IGetType.TypeId entifier
{
get { return m_type; }
}
}

public class Base2 : Base
{
}

void TestingFunc()
{
object obj = new Base2();
if (obj is IGetType)
{
// Base2 implements IGetType
}
}

I am checking objects because i am doing a recursive check against
arbitary types to see if they implement the interface. Now i want Base
to implement the interface, but not inherit it to Base2. Furthermore i
do need Base2 to inherit from Base. I know that is the definition of
inheritance, so maybe someone would be kind enough to think about how
else i could do this?
Without knowing what these classes actually are and what they represent,
it's hard to offer much advice. By the definition of inheritance, all
instances of Base2 instances *are* instances of Base also, thus
implement IGetType. I suppose you _could_ override the
IGetType-implementing functions in Base2 and throw
NotSupportedExc eptions if they are invoked, but this would be ugly in
the extreme, happen at runtime rather than compile time, and I'm not
sure it would work.

You need to work out semantically what you actually want to be happening
here.

--
Larry Lard
la*******@googl email.com
The address is real, but unread - please reply to the group
Feb 2 '07 #2

Use three classes:

public class Base {
.. base class stuff...
}

public class Base2 : Base {
... child class stuff ...
}

public class BaseWithGetType : Base, IGetType {
... IGetType implementation ...
}

And then in your app use BaseWithGetType instead of Base.

If the purpose of this is specifically for reflection and identifying
classes then perhaps you'd be better off using Attributes than
Interface implementation. Something like:

[TypeIdentifier( 2)]
public class Base { }

public class Base2 : Base { }

Then Base will have the attribute but Base2 will not (well, it will or
won't depending the parameters passed to GetCustomAttrib utes).

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 2 Feb 2007 01:26:01 -0800, g1**@hotmail.co m wrote:
>Hi, im trying to do something and just been caught off guard. I want
to implement an interface but only for certain class, but of course
when i derive from this base class the derived class also inherits the
interface.

public class Base : IGetType
{
protected byte m_type;

object IGetType.TypeId entifier
{
get { return m_type; }
}
}

public class Base2 : Base
{
}

void TestingFunc()
{
object obj = new Base2();
if (obj is IGetType)
{
// Base2 implements IGetType
}
}

I am checking objects because i am doing a recursive check against
arbitary types to see if they implement the interface. Now i want Base
to implement the interface, but not inherit it to Base2. Furthermore i
do need Base2 to inherit from Base. I know that is the definition of
inheritance, so maybe someone would be kind enough to think about how
else i could do this?

Many thanks,

Chris
Feb 2 '07 #3
<g1**@hotmail.c omwrote:
Hi, im trying to do something and just been caught off guard. I want
to implement an interface but only for certain class, but of course
when i derive from this base class the derived class also inherits the
interface.
And so it should. Anything you can do with a base class, you must be
able to do with a derived class. Search for "Liskov Substitution
Principle" for more details.
I am checking objects because i am doing a recursive check against
arbitary types to see if they implement the interface. Now i want Base
to implement the interface, but not inherit it to Base2. Furthermore i
do need Base2 to inherit from Base. I know that is the definition of
inheritance, so maybe someone would be kind enough to think about how
else i could do this?
Check whether the object implements the interface, and then if it does,
exclude it if its base type implements the interface too?

Sounds a bit ugly though - have you considered using non-inherited
attributes to mark the types that you want to consider?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 3 '07 #4
Sounds a bit ugly though - have you considered using non-inherited
attributes to mark the types that you want to consider?
Thanks for the replies, i have done some thinking and how about
keeping a list of registered types? If the type is registered i would
then go on to call the objects interface (which of course must be
implemented by the registered type).

public interface IGetType
{
object TypeIdentifier {get;}
}

public class Base : IGetType
{
protected byte m_type;

public object TypeIdentifier
{
get { return m_type; }
}
}

public class Base2 : Base
{
public Base2()
{
base.m_type = 0x01;
}
}

public class Base3 : Base, IGetType
{
UInt32 m_Subtype = 0x100;

public Base3()
{
base.m_type = 0x02;
}

object IGetType.TypeId entifier
{
get { return m_Subtype; }
}
}

void test()
{
List<Typeht = new List<Type>();
ht.Add(typeof(B ase));
ht.Add(typeof(B ase3));

object obj2 = new Base();
bool isRegistered = ht.Contains(obj 2.GetType()); //
true
if (isRegistered && (obj2 is IGetType))
{
// prints: Type: System.Byte, Value: 0
object next = ((IGetType)obj2 ).TypeIdentifie r;
Console.WriteLi ne("Type: " + next.GetType() + ",
Value: " + next);
}

obj2 = new Base2();
isRegistered = ht.Contains(obj 2.GetType()); // false
if (isRegistered && (obj2 is IGetType))
{
object next = ((IGetType)obj2 ).TypeIdentifie r;
Console.WriteLi ne("Type: " + next.GetType() + ",
Value: " + next);
}

obj2 = new Base3();
isRegistered = ht.Contains(obj 2.GetType()); // false
if (isRegistered && (obj2 is IGetType))
{
object next = ((IGetType)obj2 ).TypeIdentifie r;
// prints: Type: System.UInt32, Value: 256
Console.WriteLi ne("Type: " + next.GetType() + ",
Value: " + next);
}
}

This does work and do excatly what i want, but maybe there are nicer
ways of doing the same?

Thanks in advance,

Chris

Feb 5 '07 #5
This does work and do excatly what i want, but maybe there are nicer
ways of doing the same?
Actualy answering this question i think you are correct in that
attributes will be the way to go and make for much nicer code.

Cheers for the ideas,

Chris

Feb 5 '07 #6

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

Similar topics

14
23148
by: Medi Montaseri | last post by:
Hi, I think my problem is indeed "how to implement something like java's final in C++" The long version.... I have an abstract base class which is inherited by several concrete classes. I have a group of methods that I'd like to implement in the base class
2
521
by: sys | last post by:
I have a Web service and I want it to implement an interface that I specified, say IService. So my Web service class looks like this public class Service1 : System.Web.Services.WebService, IServic public Service1( InitializeComponent() [WebMethod
4
2449
by: wesley | last post by:
Hi, Why must abstract classes implements all the methods/property in the interface it implements? Since it's an abstract class it shouldn't be able to be instantiated and the child classes are those that should implement the interface. However in .Net I have to implement the interface in the abstract class as well. Why is this so?
3
1784
by: Erik Harris | last post by:
I apologize if this is a stupid question - I'm relatively new to OOP. I have a property that must exist in a class in order to be used by another class. The property, however, does not change with each instance (it returns an instance of a delegate that points to the same method no matter what the instance). I thought the best way to make sure that Class1 could be used by Class2 would be to create an interface that defined this property,...
2
1461
by: Jinsong Liu | last post by:
I have a class which serve as a base class. I want to focuses all the classes derived from it implement a interface. Make it a abstract class is not a option since some XML serialization code need to initialize the class. is it possible? thanks
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...
52
20916
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member 'UselessJunkForDissassembly.IInvocableInternals.OperationValidate(string)' C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
5
2416
by: Tony Johansson | last post by:
Hello! Assume you have the following interface and classes shown below. It is said that a class must implement all the methods in the interface it inherits. Below we have class MyDerivedClass that inherits IMyInterface but MyDerivedClass doesn't implement method DoSomething() it inherits it from the base class MyBaseClass. So the statement that a class must implement all method in an interface that
5
2659
by: Tony Johansson | last post by:
Hello! The only reason I can see interface to be implemented explicitly is when the a class implement two interface having the same method signature. In all other cases I can implement interface implicitlly. Can you agree with me about this statement ? //Tony
0
9716
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
9596
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
10604
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
10356
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
10361
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
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4316
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
3006
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.