polymorphism

Inheritance question

I'm a newbie to C#. I tried C++ and but found it too convoluted. Here's my question. If I use inheritance, and later realize that a subclass needs a method or field that is not applicable to the other parts, should I declare that in the base class and not have it implemented in the child classes, or should I declare that method or prope...

How to call overridden methods in a subclass? Potential candidate for refactoring

Originally I had a design problem where I needed five subclasses of a superclass, where all but two would use the same generic method of doing things and the other two classes would need special handling. I wanted to avoid writing the method five times; two special cases and three identical ones. So I had every class inherit SuperClass,...

Java Polymorphism problem

I've learned all these programming terms in Swedish so please bear with me.. I'm having problems calling a method in a subclass which should override a method in the superclass. Here is the class structure with removed code: public interface Movable { public void move(double delta); } public abstract class Unit implements Movable,...

How do I iterate and perform some arbitrary operation on each item?

I have a Abstract Iterator class which has this function void iterate(){ while(this.hasnext()){ ..this.next().. } } How do I pass in any arbitrary function that will be applied to the next element. For example, is there a way to do iterate(print)? ...

Advanced PHP Polymorphism question

Hey guys! I have a class which has a function to retrieve children elements from a database. The following code will be rather pseudo cause I want to keep it as easy as possible. abstract class SomeHostObject extends SomeObject { function getChild($identifier) { global $database; $id = $database->select('Some MySQL Quer...

How dynamic casts work?

Let's say I have type A, and a derived type B. When I perform a dynamic cast from A* to B*, what kind of "runtime checks" the environment performs? How does it know that the cast is legal? I assume that in .Net it's possible to use the attached metadata in the object's header, but what happen in C++? ...

How to make WPF Pages have common interface

We develop a wizard-like WPF application that has a Frame that navigates to various Pages. I define each Page in a separate xaml and xaml.cs file, and then have the application make the frame navigate between the page. All of my pages currently inherit from Page. I would like to have a common interface to all my pages, so I can access t...

Mapping a polymorphic relationship onto 2 models simultaneously

I need to relate a Comments model with two ids at the same time but can't figure out how. Here' my situation. I am building an on-line school grading system and need to be able let the teacher make a comment on a particular student in a particular course for a particular term (grading period). class Course has_many :course_terms h...

xcode compiler see a class as abstract but it's not!

Hi, I'm working on a C++ command tool project that depends on a third party architecture called ACE (adaptive communication environment). I'm new to Xcode and this is what I've done to have my command tool project "sees" the ACE library. compile the ACE library so that I have a bunch of dynamic libraries: xxx.dylib add the libraries a...

Problem with one-to-many relationship with Single Table Inheritance (Rails)

I have problem with STI and relationship in ActiveRecord. I think I missed something in the class methods, but I don't know for sure. Below is my models: class User < ActiveRecord::Base has_many :advertisements end class Advertisement < ActiveRecord::Base belongs_to :user end class FreeAdvertisement < Advertisement end class Paid...

Choosing the right subclass to instantiate programatically

Ok, the context is some serialization / deserialization code that will parse a byte stream into an 'object' representation that's easier to work with (and vice-versa). Here's a simplified example with a base message class and then depending on a 'type' header, some more data/function are present and we must choose the right subclass to ...

Copying a class that inherits from a class with pure virtual methods?

I've not used C++ in a while, and I've become far too comfortable with the ease-of-use of real languages. At any rate, I'm attempting to implement the Command pattern, and I need to map a number of command object implementations to string keys. I have an STL map of string to Command, and I'd like to copy the Command. Essentially, Com...

Abstract class in c++

Hi, Let's say I've got class: class Bad_Date { private: const char* _my_msg; public: const char* msg() const { return _my_msg; } }; And I would like to not be able to create any object of this class but I don't really want to put anything else there and make it pure virtual fnc. Is there any other way to make this class abstract or ...

c++ problem with polymorphism and vectors of pointers

Consider the following example code: class Foo { }; class Bar : public Foo { }; class FooCollection { protected: vector<shared_ptr<Foo> > d_foos; }; class BarCollection : public FooCollection { public: vector<shared_ptr<Bar> > &getBars() { // return d_foos won't do here... } }; I have a problem like this in ...

Polymorphism and shadowing inherited members

I have a couple of small classes to represent parts in a search filter. If the searched value equals NonValue the filter is supposed to do nothing. This is defined in a Base Class: Private Class BaseFilter Protected NonValue As Object Protected sQueryStringBase As String = "AND {0} {1} {2} " Public Sub Check...

Static Abstract methods in C#

I know it's a tautology to have a static abstract method, but how can I do something like this: Base, abstract class: abstract class QTimerDatabaseObject { public static abstract QTimerDatabaseObject createFromQTimer(DataRow QTimerRow); public abstract void saveRow(); } Sample Implementation (Inside a User class that extends...

Hibernate - apply locks to parent tables in polymorphic queries

I have two objects: public class ParentObject { // some basic bean info } public class ChildObject extends ParentObject { // more bean info } Each of these tables corresponds to a differnet table in a database. I am using Hibernate to query the ChildObject, which will in turn populate the parent objects values. I have defined my m...

How to structure a Genetic Algorithm class hierarchy?

I'm doing some work with Genetic Algorithms and want to write my own GA classes. Since a GA can have different ways of doing selection, mutation, cross-over, generating an initial population, calculating fitness, and terminating the algorithm, I need a way to plug in different combinations of these. My initial approach was to have an abs...

C# Generics and polymorphism: an oxymoron?

I just want to confirm what I've understood about Generics in C#. This has come up in a couple code bases I've worked in where a generic base class is used to create type-safe derived instances. A very simple example of what I'm talking about, public class SomeClass<T> { public virtual void SomeMethod(){ } } public class DeriveFr...

Dynamic method dispatch based on value of variable

Hi there, Long switch statments are often frowned upon. The solution is to use polymorphism. However what if the thing I'm switching on is not a type code? What I would like to do is replace the switch statement with something like this... public void HandleString(string s = "Hello") { ... } public void HandleString(string s = "Goodb...