inheritance

Container<ImplementerOfIInterface> is not Container<IInterface>. Why not?

Why wouldn't DoesntWork() work below? The error is: Cannot implicitly convert type 'List' to 'IEnumerable'. An explicit conversion exists (are you missing a cast?). I know this is something about generic/templates I'm not getting, but List is IEnumerable and Implementer is an IInterface. I don't see why this needs to be casted (or if ...

How to properly mix generics and inheritance to get the desired result?

My question is not easy to explain using words, fortunately it's not too difficult to demonstrate. So, bear with me: public interface Command<R> { public R execute();//parameter R is the type of object that will be returned as the result of the execution of this command } public abstract class BasicCommand<R> implements Command<R> ...

constructor function's object literal returns toString() method but no other method

I'm very confused with javascript methods defined in objects and the "this" keyword. In the below example, the toString() method is invoked when Mammal object instantiated: function Mammal(name){ this.name=name; this.toString = function(){ return '[Mammal "'+this.name+'"]'; } } var someAnimal = new Mammal('Mr. Biggles'); alert...

what would be the output?

Please explain me below situation What would be the output? interface A{} class B implements A{} class C extends B{} class D extends C{} class E extends D{ public static void main(String args[]){ C c = new C(); B b = c; A a = (E)c; a = (B)c; c = (C)(B)c; } } ...

Is there any difference these two pieces of code?

#include<stdio.h> class A {public: int a; }; class B: public A {private: int a;}; int main(){ B b; printf("%d", b.a); return 0; } #include<stdio.h> class A {public: int a; }; class B: private A {}; int main(){ B b; printf("%d", b.a); return 0; } I ask because I get different errors: error: 'int B::a' ...

C++ inheritance: scoping and visibility of members

Can you explain why this is not allowed, #include <stdio.h> class B { private: int a; public: int a; }; int main() { return 0; } while this is? #include <stdio.h> class A { public: int a; }; class B : public A{ private: int a; }; int main() { return 0; } In both the cases, we have one public and one pri...

Inheritance inside a template - public members become invisible?

I'm trying to use inheritance among classes defined inside a class template (inner classes). However, the compiler (GCC) is refusing to give me access to public members in the base class. Example code: template <int D> struct Space { struct Plane { Plane(Space& b); virtual int& at(int y, int z) = 0; Space& s...

Reducing template bloat with inheritance

Does anyone have experience reducing template code bloat using inheritance? i hesitate rewriting our containers this way: class vectorBase { public: int size(); void clear(); int m_size; void *m_rawData; //.... }; template< typename T > class vector : public vectorBase { void push_back( const T& ); //......

Rails object inheritence with belongs_to

I have a simple has_many/belongs_to relationship between Report and Chart. The issue I'm having is that my Chart model is a parent that has children. So in my Report model I have class Report < ActiveRecord::Base has_many :charts end And my Chart model is a parent, where Pie, Line, Bar all inherit from Chart. I'm not sure where the...

Model inheritance and RSS Feed framework

I'm using model inheritance to manage a multiple models queryset: from django.db import models from django.contrib.sites.models import Site from django.contrib.auth.models import User from imagekit.models import ImageModel import datetime class Entry(models.Model): date_pub = models.DateTimeField(default=datetime.datetime.now) ...

Composition vs Inheritance and GUI toolkits

It's said that composition is preferred over inheritance. Every single open source GUI toolkit however uses inheritance for the drawn widgets (windows, labels, frames, buttons, etc). I checked Qt, wxWidgets, and GTK+. Is there an example of a GUI toolkit (written in any language) that uses composition instead of inheritance to separat...

Cannot inherit from generic base class and specific interface using same type with generic constraint

Sorry about the strange title. I really have no idea how to express it any better... I get an error on the following snippet. I use the class Dummy everywhere. Doesn't the compiler understand the constraint I've added on DummyImplBase? Is this a compiler bug as it works if I use Dummy directly instead of setting it as a constraint? Err...

Class Methods Inheritence

I was told that static methods in java didn't have Inheritance but when I try the following test package test1; public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { TB.ttt(); TB.ttt2(); } } package test1; public class TA { static publi...

Interface for method that returns its own type

I have a situation where i have a class class Foo { Foo Bar() { return new Foo(); } } Now i wan tot create an interface for it class IFoo { ??? Bar(); } What should be in place of the question marks? Each class should return it's own type, not Foo. The solutions below work but do not looks clean. I don't ...

non-scalar type requested

can somebody please help me with an error conversion from `A' to non-scalar type `B' requested I have class A and derived from it B, but I have problems with these rows: A a(1); A *pb = new B(a); B b = *pb; //here I have an error thanks in advance for any help class A { protected: int player; public: A(int initPlayer =...

C++ private inheritance and static members/types

I am trying to stop a class from being able to convert its 'this' pointer into a pointer of one of its interfaces. I do this by using private inheritance via a middle proxy class. The problem is that I find private inheritance makes all public static members and types of the base class inaccessible to all classes under the inheriting cla...

error in C++, within context

I received homework to make program without casting using constructors so this is my code, I have two classes: class Base { protected: int var; public: Base(int var = 0); Base(const Base&); Base& operator=(const Base&); virtual ~Base(){}; virtual void foo(); void foo() const; operator int(); }; class Der...

Apples, oranges, and pointers to the most derived c++ class

Suppose I have a bunch of fruit: class Fruit { ... }; class Apple : public Fruit { ... }; class Orange: public Fruit { ... }; And some polymorphic functions that operate on said fruit: void Eat(Fruit* f, Pesticide* p) { ... } void Eat(Apple* f, Pesticide* p) { ingest(f,p); } void Eat(Orange* f, Pesticide* p) { peel(f,p); ingest...

Getting functions of inherited functions to be called

Let's say I have a base class Animal from which a class Cow inherits, and a Barn class containing an Animal vector, and let's say the Animal class has a virtual function scream(), which Cow overrides. With the following code: Animal.h #ifndef _ANIMAL_H #define _ANIMAL_H #include <iostream> using namespace std; class Animal { public: ...

Understanding the concept of inheritance in Java

Hello All.... I am just refreshing the oops features of the java. So, I have a little confusion regarding inheritance concept. For that I have a following sample code : class Super{ int index = 5; public void printVal(){ System.out.println("Super"); } } class Sub extends Super{ int index = 2; public void pr...