polymorphism

Any type mapping with different Id types

Hi there, I'm working on an event-planning application for the contacts in a phone book. Avoiding all the public virtual and protected stuff, my Contact class looks like: class Contact { //... Int32 Id { get; private set; } //primary key; String Name { get; private set; } //... } A customer asked me to handle both h...

How to determine which derived class is being pointed to when using an array of pointers

I am writing a C++ program that will roll a die and flip a coin. I need to use inheritance and polymorphism. I have my virtual functions set up correctly. In my base class(aRandomNumberGenerator) I have a virtual function generate. In main() I need to have an array of 2 base class pointers to my derived classes(aDie and aCoin). How ...

Explain Polymorphism ?

Now can anyone explain polymorphism to me? I'm not sure I am understanding it correctly. In the Python scope, what I am getting out of it is that I can define parameters as followed: def blah (x, y) without having to specify the type, as opposed to another language like Java, where it'd look more along the lines of: public void blah...

Polymorphism and forms in Ruby on Rails

I've been full of questions lately, but thanks to this awesome community, I'm learning a ton. I got all the help I needed with polymorphic associations earlier and now I have a question about tackling forms with polymorphic models. For instance I have Phoneable and User, so when I create my form to register a user, I want to be able to...

Why does Wikipedia say "Polymorphism is not the same as method overloading or method overriding."

I have looked around and could not find any similar question. Here is the paragraph I got from Wikipedia: Polymorphism is not the same as method overloading or method overriding. Polymorphism is only concerned with the application of specific implementations to an interface or a more generic base class. Method overloading refers to me...

What is the benefit of polymorphism using Collection interface to create ArrayList object?

Hi, I studied polymorphism and understand that it can do dynamic method binding like below. Assuming that class Animal is abstract class. public class AnimalReference { public static void main(String args[]) Animal ref // set up var for an Animal Cow aCow = new Cow("Bossy"); // makes specific objects Dog aDog =...

Dynamic instantiation in C++

I want my code to be extensible, in a way where at runtime I create the objects. For example, let's say I have a Grocery class which has an array of fruits and I want to fill this array of fruits with objects which derives from fruits. class Fruit{ }; class Grocery{ std::vector<Fruit> m_fruits; }; class Apple: Fruit{ }; class...

Can you write a polymorphic class to disk and survive?

Firstly, I know that writing a class to disk is bad, but you should see some of our other code. D: My question is: can I write a polymorphic class to disk and then read it in later and not get undefined behaviour? I am going to guess not because of vtables (I think these are generated at runtime and unique to the object?) I.e. class A...

method factories which take class attributes as parameters

I'm finding it useful to create "method factory functions" that wrap a parametrized object attribute in some logic. For example: """Fishing for answers. >>> one().number_fisher() 'one fish' >>> one().colour_fisher() 'red fish' >>> two().number_fisher() 'two fish' >>> two().colour_fisher() 'blue fish' """ class one(object): def n...

Rails activerecord composite key Id and type

Say I have a table animals that has a column parent_id Say also that I have a type_id on the table to indicate species So in my Animal class, Animall.all would return all animals. Say that now I add two new animal types, dog and cat in the animal_types table. So now some animals have type_id of 1 and some 2. I could write a named_sco...

How can I pass a Class as parameter and return a generic collection in Java?

I am designing a simple Data Access Object for my Java application. I have a few classes (records) that represents a single row in tables like User and Fruit. I would like to have a single method for getting all records of a specific type. For the moment I have it like this: public List<User> getAllUsers() { ... } public List<Fruit>...

Polymorphism and casting problem

In order to explain my problem here is an example namespace CheckAbstarct { class Program { static void Main(string[] args) { myAbstarctClass mac1 = ObjectFactory.ObjectCreator("aaa"); myAbstarctClass mac2 = ObjectFactory.ObjectCreator("bbb"); mac1.changeMyString(); mac2.changeMyString(); ...

how do I polymorph two classes into one

hi i have two classes, example: ftp1 (handles normal ftp stuff), ftp2 (handles secure ftp connections, and stuff). based on a configuration setting I need to instantiate a type that can be one of these two types as a class level variable. Is this possible, if so, can someone point me in the right direction. I've tried to apply polymorphi...

Is it possible to *transform* the type of a polymorphic Hibernate/JPA entity?

UPDATE: after posting, I discovered this is a duplicate of this older question. I have an odd business requirement: my application supports users getting started as "Guests", then later registering to become "Registered" users. Guests are feature-restricted: they are not allowed to access some functionality that is available once the...

C++ polymorphism - Auto detect derived type

I have certain code that I want to optimize. It looks like this: function abc( string format ) { if (format == "a") { // this is a string, I shouldn't have used single quote, sorry for the confusion classx::a t; doit(t); } if (format == "b"){ classx::b t; doit(t); } if (format == "c"){ classx::c t; doit...

.net generic constraints and object inheritance compiling issue

I have the following situation below. This code will throw a compiler error for Test2 The type 'InheritedChild' cannot be used as type parameter 'T' in the generic type or method 'panelGenericIOGrid'. There is no implicit reference conversion from 'InheritedChild' to 'SerializerBase'. public class SerializerBase<T> { } public class...

Best Practises for Polymorphic JPA via Annotations

I'm trying to set up polymorphic behaviour using Hibernate with JPA annotations. It seems sensible (maybe even necessary) to create an (abstract) class encapsulating the state and behaviours required for the inheritance hierarchy to participate in persistence; for example I need to annotate an Id property, which I cannot do in an inte...

Can I write an interface for a template class in C++

I have an interface and a couple of implementations of a class that stores serialized objects. I'd like to make the implementation classes into template classes so I can use them with more than one type of object, but I'm getting compiler errors. #include <iostream> template<typename T> class Interface{ public: virtual void func...

an instance hot-swapping itself with a new instance

I have some actions... View, Edit, Checkout, etc. Business logic dictates that if a document is already checked out, all Views turn into Edits. is there a neat OO way to do something like this: class Action { public: Document document; virtual void execute(); }; class View, Edit, Checkout : public Action; View::execute() { if (d...

what exactly does runtime polymorphism mean ?

Hi, i'm slightly confused about this run time polymorphism. correct me if i am wrong, run time polymorphism means, function definitions will get resolved at run time. take this example.. class a { a(); ~a(); void baseclass(); } class b: class a { b(); ~b(); void derivedclass1(); } class c: class a { c(); ~c(); void derivedclass2(); ...