inheritance

C# - Problem with generics and inheritance

Hi, I've got a problem with inheritance and generics. This is the code that illustrates my problem: namespace TestApplication { public class MyClass<T> { private T field; public MyClass(T field) { this.field = field; } } public class MyIntClass : MyClass<int> { pu...

Protected derived class

#include <iostream> using namespace std; class Base { public: Base(){cout <<"Base"<<endl;} virtual ~Base(){cout<<"~Base"<<endl;} virtual void foo(){ cout<<"foo base"<<endl;} }; class Derived: private Base { public: Derived(){cout<<"Derived"<<endl;} virtual ~Derived(){cout<<"~Derived"<<en...

IEnumerable interface

I don't understand why IList implements IEnumerable taking in consideration that IList implements ICollection that implements IEnumerable also. ...

Java not inheriting accessor methods?

Given a class "Bar" that extends class "Foo" which implements interface "DeeDum" public interface DeeDum { public String getDee(); public String getDum(); } public class Foo implements DeeDum { public String dee = "D"; public String dum; public String getDee() { return dee; } public String getDum() { return dum...

Can you require multiple types at once?

Basically I want to do this: public interface A { void a(); } public interface B { void b(); } public class SomeClass { public SomeClass(<A&B> e) { // Note the type here e.a(); e.b(); } } What I did on the commented line is obviously illegal. I know I can just require the passed object to implement inte...

Force calling the base method from outside a derived class

I have two classes: public class MyBase { public virtual void DoMe() { } } public class MyDerived:MyBase { public override void DoMe() { throw new NotImplementedException(); } } And I have the following code to instantiate MyDerived: MyDerived myDerived=new MyDerived(); The thing is how to...

Is there any way to call the parent version of an overridden method? (C# .NET)

In the code below I tried in two ways to access the parent version of methodTwo, but the result was always 2. Is there any way to get the 1 result from a ChildClass instance without modifying these two classes? class ParentClass { public int methodOne() { return methodTwo(); } virtual public int methodTwo() ...

Inheritance instead of typedef

C++ is unable to make a template out of a typedef or typedef a templated class. I know if I inherit and make my class a template, it will work. Examples: // Illegal template <class T> typedef MyVectorType vector<T>; //Valid, but advantageous? template <class T> class MyVectorType : public vector<T> { }; Is doing this advantageous s...

Does Visual Inheritance work with User controls in VS2008

I have a base User Control. I place Ok and Cancel buttons on the bottom right of the control and anchor them Bottom and Right. I then create another user control that inherits from the base user control. I resize the inherited control (e.g. increase height or width). Throw the inherited control onto the form. Run. The inherited contro...

Is type checking ever OK?

Is type checking considered bad practice even if you are checking against an interface? I understand that you should always program to an interface and not an implementation - is this what it means? For example, in PHP, is the following OK? if($class instanceof AnInterface) { // Do some code } Or is there a better way of altering ...

Handling the Same Class Definition From Multiple Web Services

The situation: We have a library project that houses much of our code for the various integrations we work on. Many of the integrations consume web service apis, and my supervisor doesn't want 5 gazillion web service references added to the project. What we generally do, then, is add a reference to a new project and copy the Reference...

Use new keyword if hiding was intended

I have the following snippet of code that's generating the "Use new keyword if hiding was intended" warning in VS2008: public double Foo(double param) { return base.Foo(param); } The Foo() function in the base class is protected and I want to expose it to a unit test by putting it in wrapper class solely for the purpose of unit tes...

Stopping inheritance without using final

Is there any other method of stopping inheritance of a class apart from declaring it as final or by declaring its constructor as private? ...

Making a method private in a python subclass

Is it possible to make a public method private in a subclass ? I don't want other classes extending this one to be able to call some of the methods . Here is an example : class A: def __init__(self): #do something here def method(self): #some code here class B(A): def __init__(self): A.__init__(self...

Can someone give a better example of fragile base class issues?

Fragile base class is one of the most common point that gets popped up in every discussion where reusability through implementation inheritance is discussed. Has anyone faced any real issue with these apart from the common square, rectangle examples. Everytime I need to explain this to someone I am stuck with some real world cases wher...

Inheritance or composition: Rely on "is-a" and "has-a"?

When I design classes and have to choose between inheritance and composition, I usually use the rule of thumb: if the relationship is "is-a" - use inheritance, and if the relationship is "has-a" - use composition. Is it always right? Thank you. ...

How do you design a class for inheritance?

I've heard it said that it is "difficult" to "design for inheritance", but I've never found that to be the case. Can anyone (and by anyone, I mean Jon Skeet) explain why this is allegedly difficult, what the pitfalls/obstacles/issues are, and why mere mortal programmers should not attempt it and just make their classes sealed to protect ...

How to inherit a class from an assembly without forcing users to reference two assemblies

I have two classes A and B in two different .NET assemblies: AssemblyA and AssemblyB. I want class B to inherit class A but I want that however uses class B to only need to reference AssemblyB (and not both AssemblyA and AssemblyB). Due to the project constraints I need to keep the two assemblies separate so I cannot use merge tool to ...

Diamond inheritance and pure virtual functions

Imagine a standard diamond inheritance. Class A defines pure virtual function fx, class B defines implementation for fx, classes C and D do nothing with fx. When trying to call fx on instance of class D you'll get 'ambiguous function call' error although there is only one implementation of fx. This can be solved by B and C inheriting fro...

How do I get the subclass Type object in a static superclass function in .net, using reflection?

Ok, so I'm trying to make a nice superclass for data-access objects that can generate a tsql query to search all of the subclass's public string properties. I want to use reflection to get the type of the subclass and then iterate through all of the public string properties on the object, since those property names are the same as the d...