polymorphism

Java: Why could base class method call a non-exist method?

class BaseClass { private void f() { System.out.println("Baseclass f()"); } public static void main(String[] args) { BaseClass dc = new DerivedClass(); dc.f(); } } class DerivedClass extends BaseClass { public void f() { System.out.println("DerivedClass f()"); } } In my opinion, the object ...

How to avoid long switch ..need help refactoring

hi guys, i needed help refactoring the following class, Following is a class Operation with variety of Operations in switch : i want to avoid the switch statement.I read few articles on using polymorphism and state pattern .but when i refactor the classes i dont get access to many variables,properties Im confused on whether to use o...

Java polymorphism question

For SCJP most of the time question such as below is asked to find valid example of polymorphic method calls. But what should one exactly look for to find is it polymorphic use or not ? abstract class A { abstract void a1(); void a2() { } } class B extends A { void a1() { } void a2() { } } class C extends B { void ...

How can I make an object in my parent class but bless it into my child class in Perl?

I have two classes: a base class, Foo::Base and a derived class, Foo::Base::Sub. I want to have Foo::Base::Sub do some type and data checking on the constructor`s argument--a hash--before blessing it. I've tried overriding Foo::Base->new's constructor, doing the checks and then calling Foo::Base->new (since the code would be exactly th...

php: polymorphism on the iterator implementation of a array object?

I don't know if a array is really a object on php ... but, what I want is to change the array behavior on a foreach loop. Something similar to this on java: for( String it : myArr = new Array_Iterator( array) implements Iterator{ public Array_Iterator( String[] arr){ this.arr = arr} /* interface implementation */ }){ /* loo...

Is it possible to project discriminator-value or class name in a polymorphic query?

Say I have abstract base class Animal then Dog and Cat classes. I'm mapping these via table-per-subclass (w/ discriminator). Is it possible to query Animals and also project the discriminator-value or class name? It seems I cannot project the discriminator column. I know when I list Animals, NHibernate creates the correct Animal type f...

Java polymorphism/abstract class help

I'm trying to set parameters for a abstract class: public abstract class NewMath { public abstract int op (int intOne, int intTwo); } Here is the extended subclass: public class MultMath extends NewMath { public int op (int intOne, int intTwo){ return intOne + intTwo; } } But when I try to instantiate an object ...

Polymorphism with different method signatures

I have a group of classes (say for validation rules). Each one returns a true or false. I use id and call a method signature for each one of the classes and get the results allowing me to dynamically create validation rules. Worked great until... I have a new class that takes an extra parameter to come up with its validation. What i...

java polymorphism

Consider the following code: public abstract class Base { public void getAnswer(); } public class Derived1 extends Base { public void getAnswer() { } } public class Derived2 extends Base { public void getAnswer() { } } public class Main { public final int DERIVED1 = 1; public final int DERIV...

Inheritance in Objective-c and Functions

I have class X, an abstract class, and classes A and B that inherit from it. Classes A and B each have their own 'return_something' function. I have another method elsewhere that calls 'return_something' on a series of objects, all of type X. 'return_something' returns something different depending on whether it is an A or a B, so I c...

CakePHP: Model that belongs to multiple others, one at a time

I have three tables: "users", "courses" and "documents". I would like to upload documents to users as well as to courses, so I would need two belongsTo-relations for the Document model. Some belong to one model, some to the other. Is there a simple solution to construct these relations? How could I set up the "add"-actions? I know I c...

Inherited class "invalid pointer error" when calling virtual functions.

As you can see in the code below, I have an Abstract Base Class "HostWindow", and class that derives from it "Chrome". All the functions are implemented in Chrome. The issue is, I can't call functions in Chrome if they're virtual. class HostWindow : public Noncopyable { public: virtual ~HostWindow() { } // Pure virtual function...

Is there a .NET Polymorphic Data Framework

I'm beginning work on a new project that's would be much easier if there was some way to make different data models polymorphic. I'm looking at using the Entity Framework 4.0 (when it's released), but have been unable to determine if it will actually be able to work. Here's the basic scenario. I'm implemented a comment system, and wou...

Calling a subclassed virtual method from a base class method

class A { public: virtual void doSomething(void) {} void doStuff(void) { doSomething(); } }; class B : public A { public: void doSomething(void) { // do some stuff here } }; B * b = new B; b->doStuff(); It gives me Segmentation fault. What am I doing wrong? It s...

virtual function question...

#include "stdafx.h" #include <iostream> #include <vector> #include <string> class Helper { public: Helper() { init(); } virtual void print() { int nSize = m_vItems.size(); std::cout << "Size : " << nSize << std::endl; std::cout << "Items: " << std::endl; for(int i=0; i<nSize; i++) { s...

What is the real significance(use) of polymorphism.

I am new to OOP. Though I understand what polymorphism is, but I can't get the real use of it. I can have functions with different name. Why should I try to implement polymorphism in my application. ...

Writing a generic API for a type 'family'

I'm working with a smallish type hierarchy, something like the following, and lets say there won't ever be any other Animal types in my sad safari-less world (I'm not at all worried about resilience to expanding): public abstract class Animal {}; public sealed class Dog : Animal {}; public sealed class Cat : Animal {}; public sealed cla...

C# inheritance issue

I'm using an API that has a "Member" class. I wish to extend this so i have create "MemberProfile" which inherits from "Member" I have some issue creating the constructor for this class. I wish to something like the following var member = Member.GetCurrentMember(); var memberProfile = new MemberProfile(member); How would the...

How functions are resolved by compiler?

How it is determined whether the below call is bound at compile time or at runtime? object.member_fn;//object is either base class or derived class object p->member_fn;//p is either base class or derived class pointer EDITED: #include <iostream> using namespace std; class Base { public: Base(){ cout<<"Constructor: Ba...

How to map a collection of abstract classes in nhibernate

I have been reading Nhibernate in Action, but the section on mapping polymorphic collections is a little too short on how to do this. I have the following code [Class] [Discriminator(Column="MachineType",TypeType=typeof(string))] public abstract class Machine { [Property] public string Name{get;set;} } [Subclass(DiscriminatorValue...