inheritance

C# - is it possible to mark overriden method as final

In C#, is it possible to mark an overridden virtual method as final so implementers cannot override it? How would I do it? An example may make it easier to understand: class A { abstract void DoAction(); } class B : A { override void DoAction() { // Implements action in a way that it doesn't make // sense for chi...

When is it Appropriate to use Generics Versus Inheritance?

What are the situations and their associated benefits of using Generics over Inheritance and vice-versa, and how should they be best combined? Thanks for the answer guys. I'm going to try to state the motivation for this question as best I can: I have a class as shown below: class InformationReturn<T> where T : Info { InformationR...

Constructor initializer does not allow me to use 'this'

Compiler Error Keyword 'this' is not available in the current context delegate void CallBack(int i); class A { public A(CallBack cb) { } } class B : A { public B() : base(new CallBack(this.f)){} private void f(int i) { } } Why is this error ? As a solution I thought of providing a parameterless protected ctor ...

NHibernate joined-subclass

Hello, I'm trying to implement class inheritance hieararchy with NHibernate and SQL server. I made it work fine with the table per hierarchy strategy (one table and several subclass elements in the mapping file). However the table per subclass strategy (N + 1 tables and N joined-subclass elements in one mapping file) makes more sense i...

How To Implement Shared Behavior Between Classes (Without Multiple Inheritance Of Course) in C#

UPDATE: So pretty much everyone here told me that i just need to start over again on how i designed my classes (thank you folks for your excellent answers by the way!). Taking the hint, i started doing extensive reading on the strategy pattern. I want to create behavior classes (or strategy classes) that inherit from an abstract base c...

Can I inherit from a generic class without specifying a type?

I have the following sample code in a VB.NET console application. It compiles and works, but feels like a hack. Is there a way to define EmptyChild so that it inherits from Intermediate(Of T As Class) without using the dummy EmptyClass? Module Module1 Sub Main() Dim Child1 = New RealChild() Child1.Content = New RealClass() ...

How do I extend a java class that is instantiated by another class?

Given two java classes, A and B, where A is usually instantiated via B, such as: A myA = B.createA(); Can I create a subclass of A (let's call it SubA) and somehow have it be instantiated by the B.createA() method? (Note that I cannot modify A and B....) I know that not all instances of A are instances of SubA, thus I cannot do ...

How to call a parent class's method from child class in python?

Stackers, I apologize for this question in advance. It must be a FAQ, but I don't seem to be able to find the answer. When creating a simple object hierarchy in python, I'd like to be able to invoke methods of the parent class from a derived class. In Perl and Java, there is a keyword for this (super). In Perl, I might do this: pa...

Restrict inheritance to desired number of classes at compile-time

We have a restriction that a class cannot act as a base-class for more than 7 classes. Is there a way to enforce the above rule at compile-time? I am aware of Andrew Koenig's Usable_Lock technique to prevent a class from being inherited but it would fail only when we try to instantiate the class. Can this not be done when deriving itse...

determine if a wpf dependencyproperty value is inherited

Does anyone know how to determine if the value of a WPF property is inherited? In particular, I'm trying to determine if the DataContext of a FrameworkElement was inherited from the parent or set directly on the element itself. Thanks, - Mike ...

Making a Point class in c++

Right now I am using std::pair to represent a 2d point in c++. However, I am getting annoyed with having to write typedef std::pair<double, double> Point; Point difference = Point(p2.first - p1.first, p2.second - p1.second); instead of being able to overload operator+ and operator-. So, my question is, to ma...

css selector rules --ruleset for all elements within a selector?

With at-rules, it's possible to have a ruleset for elements during that at-rule event (like printing, mobile devices, etc.) But what about just for a specific selector? I am working on a sub page of a larger site, and I have to use the master stylesheet for any of my pages. Sometimes a style rule just gets trumped by a rule from the ma...

How to make the Asp.Net/WSE asmx page generator add the base class properties in a derived class

I have a simple base class B with 2 public properties. This class is inherited by another class D that adds another public property. The derived class is returned by a web service call. The page generated by ASP.Net looks like: '''<remarks/> <System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3074"), _ Sys...

prototype based vs. class based inheritance

In javascript, every object is at the same time instance and class. To do inheritance, you can use any object instance as a prototype. In python, C++, etc.. there are classes, and instances, as separate concepts. In order to do inheritance, you have to use the base class to create a new class, which can then be used to produce derived i...

Why won't a derived class work in an array? (C++)

I've created a class, called vir, with a function move: class vir { public: vir(int a,int b,char s){x=a;y=b;sym=s;} void move(){} }; (It's derived from a class with variables int x, int y, and char sym) I have derived a class from this, called subvir: class subvir:public vir { public: subvir(int a,int b,char s){x=a;y=b...

Adding a collection of a subclass with AddRange

If I have these two classes: class A {} class B : A {} and I make a List<A> but I want to add a List<B> to it by calling List<A>.AddRange(List<B>) but the compiler refuses: Argument '1': cannot convert from 'System.Collections.Generic.List<A>' to 'System.Collections.Generic.IEnumerable<B> which I completely understand because IEnum...

Why to use class based OOP style inheritance in javascript?

If I'm not completely wrong every framework/library/approach in javascript tends today to mimick class based OOP style inheritance. Reasons for this seem to be people thinking class based OOP inheritance is far easier to understand and that most programmers know OOP. In my experience I don't find evidence for either of this opinions. I...

Accessing private instance variables of parent from child class?

Let's say we have a class foo which has a private instance variable bar. Now let us have another class, baz, which extends foo. Can non-static methods in baz access foo's variable bar if there is no accessor method defined in foo? I'm working in Java, by the way. ...

Derived Functor with any return type and any parameters

I have a class that uses functors as units of work. It accepts a reference to a functor in its Run() method. To allow this class to operate on any functor, all these functors must derive from my base functor class which looks like this: class baseFunctor{ public: virtual void operator()()=0; virtual baseFunctor Clone()=0; }; ...

Windows Form Inheritance

I want to create a bunch of forms that all have the same properties and initialize the properties in the forms constructor by assigning the constructor's parameters. I tried creating a class that inherits from form and then having all of my forms inherit from that class, but I think since I couldn't call InitializeComponent(), that I w...