polymorphism

Segfaulting polymorphism (Suspected stack corruption - Code on github)

It's a little difficult to explain the offending piece of code, as there seems to be some degree of stack corruption, and all the code's pretty linked together (it's a game). If you're looking for a headache and some debugging, please check out the segfault branch on git://github.com/RobotGymnast/Gingerbread.git and see if you can find ...

How to allow Class Property be of multiple/flexible types in c#?

In C# I have three classes: Person, Cat, and Dog. Both the Cat and Dog classes have the method Eat(). I want the Person class to have a property ‘Pet’. I want to be able to call the Eat method of both the Cat and Dog via the Person via something like Person.Pet.Eat() but I can’t because the Pet property needs to be either of type C...

Extending a class in Java

Hi I got a list of objects like this ArrayList <Page> pageList = aForeignObject.getAllPages(); And a child class class MyPage extends Page { public void newFunction() { // A new Feature } } Is it possible somehow to convert the Page objects into MyPage objects? I would love to do sth like this: MyPage page =...

polymorphism for properties specified by interfaces.

Why doesnt this work? public class ClassOptions {} public interface Inode { ClassOptions Options {get;} } public class MyClass : Inode { public ClassOptions Options { get; set; } } public class ClassDerivedOptions : ClassOptions { } public class MyDerivedClass : Inode { public ClassDerivedOptions...

Late Binding vs. Polymorphism - what is the difference?

I've seen both used interchangebly but do they really mean the same? From my understanding, Polymorphism stretches the fact that you could exchange an instance of a class by an instance of a subclass, and Late Binding means that when you call a method of an instance, the type decides which method (subclass/superclass) gets called. ...

Can removing final from a class definition break backwards compatibility?

I'm currently reading Effective Java by Joshua Bloch and Item 17 is 'Design and document for inheritance or else prohibit it'. The author suggest to prohibit inheritance by default. Is it safe to declare classes final by default and in a later release remove the final keyword if there is a need to extend the class? Will it break backw...

Problem with model inheritance and polymorphism

Hi, i came with new django problem. The situtaion: i have a model class UploadItemModel, i subcallss it to create uploadable items, like videos, audio files ... class UploadItem(UserEntryModel): category = 'abstract item' file = models.FileField(upload_to=get_upload_directory) i subclass it like this: class Video(UploadItem)...

Does anybody have any tips for managing polymorphic nested resources in Rails 3?

in config/routes.rb: resources posts do resources comments end resources pictures do resources comments end I would like to allow for more things to be commented on as well. I'm currently using mongoid (mongomapper isn't as compatible with rails3 yet as I would like), and comments are an embedded resource (mongoid can't yet ha...

Upcasting without retaining reference to derived type

I have a class called Resource, this is inherited by a class called ResourceMeta I need to upcast ResourceMeta to Resource without it still thinking it is a type of ResourceMeta. When I try to save my object using entity framework, it will complain about mappings not existing, rightly so because it will be trying to save ResourceMeta r...

Hibernate Polymorphism (Extending Parent Class)

Question regarding Hibernate Polymorphism and extending a parent class (which I can not modify directly). My parent class is called Contact: @Entity @Table(name="contact") @Inheritance(strategy=InheritanceType.JOINED) public class Contact { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public long id; public String name;...

C# oops concept query

I have a question related to OOPS concept. I have a base class public class BaseClass { public int i = 10; public int x = 30; public string str = "Hello"; public virtual string Hello() { return "Hello of base class called"; } } I have a child class public class ChildClass : BaseClass { public int i = 20; ...

Practical example of Polymorphism

Can anyone please give me a real life, practical example of Polymorphism? My professor tells me the same old story I have heard always about the + operator. a+b = c and 2+2 = 4, so this is polymorphism. I really can't associate myself with such a definition, since I have read and re-read this in many books. What I need is a real world e...

Bitfields vs. Polymorphism for game map object attributes

This optimzation question has been bugging me for the last day. In my program (a simple roguelike game), I use bitwise flags to store the attributes of map objects, such as if they are solid, or if they are rendered. However, I could accomplish the thing using polymorphism to return the appropriate value. My question is, is either way...

What would be the best way to wrap up this void pointer?

Alright, I have library I wrote in C that reads a file and provides access to its' data. The data is typed, so I'm using void pointer and a few accessor functions: typedef struct nbt_tag { nbt_type type; /* Type of the value */ char *name; /* tag name */ void *value; /* value to be casted to the corresponding type */ ...

Relational container in c++ and polymorphism issues

Hi everyone. I would appreciate any help with this. I have a simple container template class Dataset. Also there is a specialization to allow a different implementation when a Dataset of Datasets is instantiated. Since a Dataset of Datasets would be a heterogeneous container, there is a base abstract class List wich must declare a com...

How was one method chosen over another in this code?

This is another of those SCJP questions. The code below prints Alpha:fooBeta:fooBeta:barBeta:bar, and I don't understand why the first foo call picked Alpha's foo instead of Beta's. If the Alpha.foo parameter is changed to String instead of String..., then the output is Beta:fooBeta:fooBeta:barBeta:barwhich makes sense. My understanding...

Java: using polymorphism to avoid if-statements?

I'm attempting to write a java program that initializes certain layouts based on what the user selects. What I want to do is try to avoid writing a bunch of if-statements so the code can be scalable for future use if more layouts need to be added. I heard the best way to implement this is using polymorphism but my understanding of polymo...

c++ factory and casting issue

I have a project where I have a lot of related Info classes and I was considering putting up a hierarchy by having a AbstractInfo class and then a bunch of derived classes, overriding the implementations of AbstractInfo as necessary. However it turns out that in C++ using the AbstractInfo class to then create one of the derived objects ...

Design choices to remove if-is statements

Hi, Say i have a class hierarchy of domain objects with one base class and a couple of child classes, one level. Let say I have a list of those objects (list of the base class) and I want to apply some logic to the classes that I feel don't really belong to the classes (eg. design/UI specific code). What are my alternatives ? If-is ...

Can I get polymorphic behavior without using virtual functions?

Because of my device I can't use virtual functions. Suppose I have: class Base { void doSomething() { } }; class Derived : public Base { void doSomething() { } }; // in any place { Base *obj = new Derived; obj->doSomething(); } the obj->doSomething() will call just the Base::doSomething() Is there a way with Base *o...