polymorphism

Polymorphism, Autoboxing, and Implicit Conversions

Would you consider autoboxing in Java to be a form of polymorphism? Put another way, do you think autoboxing extends the polymorphic capabilities of Java? What about implicit conversions in Scala? My opinion is that they are both examples of polymorphism. Both features allow values of different data types to be handled in a uniform m...

Is this an example for parametric polymorphism?

Hi i am educating myself oop principles. I would like to know if this is a correct example of Cardellis definition of parametric polymorphism. Please enlighten me. The example is in cfml's script based syntax. <cfscript> Parent = createobject("component","webapp.model.Parent").init(); Child = createobject("component","webapp.model.Chil...

Calling an Overridden Method from a Parent-Class Constructor

I tried calling an overridden method from a constructor of a parent class and noticed different behavior across languages. C++ - echoes A.foo() class A{ public: A(){foo();} virtual void foo(){cout<<"A.foo()";} }; class B : public A{ public: B(){} void foo(){cout<<"B.foo()";} }; int main(){ B *b = new B()...

Performance of C# method polymorphism with generics

I noticed in C#, unlike C++, you can combine virtual and generic methods. For example: using System.Diagnostics; class Base { public virtual void Concrete() {Debug.WriteLine("base concrete");} public virtual void Generic<T>() {Debug.WriteLine("base generic");} } class Derived : Base { public override void Concrete() {Debug...

Few Basic Questions in Overriding

I have few problems with my basic and would be thankful if someone can clear this. 1: What does it mean when I say base *b = new derived; Why would one go for this? We very well separately can create objects for class base and class derived and then call the functions accordingly. I know that this base *b = new deriv...

JPA polymorphic oneToMany

I couldn't figure out how to cleanly do a tag cloud with JPA where each db entity can have many tags. E.g Post can have 0 or more Tags User can have 0 or more Tags Is there a better way in JPA than having to make all the entities subclass something like Taggable abstract class? Where a a Tag entity would reference many Taggables. Edi...

Derived template override return type of member function C++

I am writing matrix classes. Take a look at this definition: template <typename T, unsigned int dimension_x, unsigned int dimension_y> class generic_matrix { ... generic_matrix<T, dimension_x - 1, dimension_y - 1> minor(unsigned int x, unsigned int y) const { ... } ... } template <typename T, unsigned int dimension> class ...

Rails - single ID for multiple models

I'm building an app which will allow a user to scan the barcode on a 'shelf', 'box' or 'product' which will then bring up that particular item or all the associated items. As these are all separate models with their own ID's, I need a global ID table. I was thinking of a polymorphic table called 'barcodes' barcodes id barcode_numbe...

Virtual functions and templates in C++ - can they be replaced with other (existing in C++) operations?

For example concept of Templates in C++ are for comfort as compiler generates some additional code, for your class or for your function, isn't it? So we could live without template by doing some additional (manual work). What about virtual functions??? Are there any situations where both of them are irreplaceable? ...

Problem overridding virtual function

Okay, I'm writing a game that has a vector of a pairent class (enemy) that s going to be filled with children classes (goomba, koopa, boss1) and I need to make it so when I call update it calls the childclasses respective update. I have managed to create a example of my problem. #include <stdio.h> class A{ public: virtual vo...

XML and XSD - use element name as replacement of xsi:type for polymorphism

Taking the W3C vehicle XSD as an example: <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://cars.example.com/schema" xmlns:target="http://cars.example.com/schema"&gt; <complexType name="Vehicle" abstract="true"/> <complexType name="Car"> <complexContent> <extension base="targe...

jpa join query on a subclass

I have the following relationships in JPA (hibernate). Object X has two subclasses, Y and Z. Object A has a manyToOne relationship to object X. (Note, this is a one-sided relationship so object X cannot see object A). Now, I want to get the max value of a column in object A, but only where the relationship is of a specific subtype, ie...

c++ polymorphism of operator overloading

How can I make pure virtual function a operator+(); function. wheh ı do like this in base class int operator+()=0; compiler gives error . in derive class operator+() function compiler say that derive class cannot make . because following class is abstract I know that I cannot create object of abstract classes but now I try to make der...

join same rails models twice, eg people has_many clubs through membership AND people has_many clubs through committee

Models: * Person * Club Relationships * Membership * Committee People should be able to join a club (Membership) People should be able to be on the board of a club (Committee) For my application these involve vastly different features, so I would prefer not to use a flag to set (is_board_member) or similar. I find myself wanting ...

base destructor called twice after derived object?

hey there, why is the base destructor called twice at the end of this program? #include <iostream> using namespace std; class B{ public: B(){ cout << "BC" << endl; x = 0; } virtual ~B(){ cout << "BD" << endl; } void f(){ cout << "BF" << endl; } virtual void g(){ cout << "BG" << endl; } private: int x; ...

Rails routing/polymorphism issue - how to model the following

Hi all, I have an app where a 'user' belong to a 'client' or a 'vendor' (and client and vendor has_many users). In the admin namespace, I want to administer these users - so an admin would choose a client or a vendor, then nav to that client's or vendor's users. My question is, short of making the user model polymorphic, how could I mod...

Python - Polymorphism in wxPython, What's wrong?

I am trying to write a simple custom button in wx.Python. My code is as follows, an error is thrown on line 19 of my "Custom_Button.py" - What is going on? I can find no help online for this error and have a suspicion that it has to do with the Polymorphism. (As a side note: I am relatively new to python having come from C++ and C# any h...

How to have Moose return a child class instance instead of its own class, for polymorphism

I want to create a generic class, whose builder would not return an instance of this generic class, but an instance of a dedicated child class. As Moose does automatic object building, I do not get to understand if this something possible, and how to create a Moose class with Moose syntax and having this behaviour. e.g.: The user asks:...

Why can't GHCi resolve the kind of [[]] ?

Why can't Haskell resolve the kind of [[]] (A list of lists)? Why isn't it simply * -> *, as I can give it a type like Int, and get [[Int]], which is of kind *. ...

Explicit method tables in C# instead of OO - good? bad?

Hi! I hope the title doesn't sound too subjective; I absolutely do not mean to start a debate on OO in general. I'd merely like to discuss the basic pros and cons for different ways of solving the following sort of problem. Let's take this minimal example: you want to express an abstract datatype T with functions that may take T as inp...