polymorphism

C++ polymorphism not supported for pointer-to-pointer

I'm searching for a proper way to clean my pointers. Here the example code: class Parent { protected: int m_Var; public: Parent() : m_Var(0) {} virtual ~Parent() {} void PubFunc(); }; class Child : public Parent { protected: bool m_Bool; public: Child() : m_Bool(false) {} virtual ~C...

Converting from Derived* to Base*&

I was trying to answer the question mentioned here by passing the reference to the pointer instead of pointer to pointer like this: class Parent { }; class Child : public Parent { }; void RemoveObj(Parent*& pObj) { delete pObj; pObj = NULL; } int main() { Parent* pPObj = new Parent; Child* pCObj = new Child; pP...

Is what seems like polymorphism in PHP really polymorphism?

Trying to figure out whether PHP supports features like method overloading, inheritance, and polymorphism, I found out: it does not support method overloading it does support inheritance but I am unsure about polymorphism. I found this Googling the Internet: I should note that in PHP the polymorphism isn't quite the way it sh...

Checking real variable type in polymorphism (C++)

Suppose we have a class A and class B and C inherit from it. Then we create an array of references to A's, and fill it with B's and C's. Now we decided that we want to eliminate all the C's. Is there a way to check what type each field of the array really holds without doing something redundant like a returnType() function? Edit: fix...

How do I call a subclass method on a baseclass object?

I'm using a library that generates a bunch of classes for me. These classes all inherit from a common base class but that base class doesn't define a couple methods that are common to all subclasses. For example: SubClassA : BaseClass{ void Add(ItemA item) {...} ItemA CreateNewItem() {...} } SubClassB: BaseClass{ void Add(ItemB...

Java: which is faster overloading or if/else

Hi all, I have child classes, each carries a different type of value along with other members. There may be a LongObject, IntObject, StringObject, etc. I will be given a value, which can be a long, int, string, etc., and I have to create a LongObject, IntObject, StringObject, etc., respectively. Would it be faster to overload a met...

Why can derived classes only sometimes have specialized types in overridden methods?

Let's say I have this base class: abstract public class Base { abstract public Map save(); abstract public void load(Map data); } to my surprise I could do this in a derived class: public class Derived extends Base { @Override public Map<String, String> save() { //Works ... } ... } but I couldn't ...

Force invocation of base class method

The following code when run obviously prints out "B1/A2/B2". Now, is it possible for it to print "A1/A2/B2" instead (i.e. A#method2() should invoke method1() on A, not on B)? Note: I have no such need to get pass polymorphism, this question is out of curiosity only. class A { public void method1() { System.out.println("A1"...

How to instantiate a class that has a constructor requiring an interface object

My java skills are limiting me from doing what I want to do. I am trying to use the Interactive Brokers Java API to see if I can do some algorithmic trading (on paper initially). I want to call a method called ReqMktDepth() which is in a class called EClientSocket. The EClientSocket constructor requires an object of type AnyWrapper to ...

Will C#4 allow "dynamic casting"? If not, should C# support it?

I don't mean dynamic casting in the sense of casting a lower interface or base class to a more derived class, I mean taking an interface definition that I've created, and then dynamically casting to that interface a different object NOT derived from that interface but supporting all the calls. For example, interface IMyInterface { b...

Why won't a derived class work in an array? (C++)

I've created a class, called vir, with a function move: class vir { public: vir(int a,int b,char s){x=a;y=b;sym=s;} void move(){} }; (It's derived from a class with variables int x, int y, and char sym) I have derived a class from this, called subvir: class subvir:public vir { public: subvir(int a,int b,char s){x=a;y=b...

Accessing private instance variables of parent from child class?

Let's say we have a class foo which has a private instance variable bar. Now let us have another class, baz, which extends foo. Can non-static methods in baz access foo's variable bar if there is no accessor method defined in foo? I'm working in Java, by the way. ...

Problem in Interfaces (polymorphism) C#

hi. i have two classes which have some common methods like funcA(), funcB() and some methods are only related to its class... what i did is made interface of TestInterface public interface TestInterface { void funcA() void funcB() } public class ClassA : TestInterface { public void funcA() { Console.WriteLine("This...

C++ new[] into base class pointer crash on array access

When I allocate a single object, this code works fine. When I try to add array syntax, it segfaults. Why is this? My goal here is to hide from the outside world the fact that class c is using b objects internally. I have posted the program to codepad for you to play with. #include <iostream> using namespace std; // file 1 class a...

WCF -> Websphere Integration Developer and polymorphism

I have a .NET WCF service which exposes an object that uses polymorphism. Apparantly, Websphere Integration Developer is unable to handle this properly (I am not the Websphere developer), except by adding all fields of all possible polymorphisms and using a enum or such to say that it is an object of such and such type. I can't possi...

Making a drawing program

I am making a drawing program, and have a few question with regards to that. I'll need the user to have the choice of drawing a rectangle, an oval and a line. I suppose I need to make a super class they all derive from. Should I make this an interface or an abstract class? And how would I set up that all the shapes have some default val...

Generic return

Hi, there is an immutable class: Scope<Cmp extends Comparable<Cmp>> public Scope<Cmp> crop(Scope<Cmp> scope) { ... return new Scope<Cmp>(starts, ends); } it has many similar methods is extended by: Timerange extends Scope<Date> and many others (also immutable). Id like them to return object of its type. For example: time...

Java polymorphism confusion

The question below is from Java SCJP5 book by Kathy Sierra and Bert Bates. Given a method declared as: public static <E extends Number> List<E> process(List<E> nums) A programmer wants to use the method like this: // INSERT DECLARATIONS HERE output = process(input); Which pair of declarations could be placed at // INSERT DECLARATI...

Higher-kinded generics in Java

Suppose I have the following class: public class FixExpr { Expr<FixExpr> in; } Now I want to introduce a generic argument, abstracting over the use of Expr: public class Fix<F> { F<Fix<F>> in; } But Eclipse doesn't like this: The type F is not generic; it cannot be parametrized with arguments <Fix<F>> Is this possible at ...

Generics confusion

Below is a question from Kathy & Bert Bates SCJP 5 preparation CD. I have posted this elsewhere too, but have not gotten a satisfactory explanation until now.... Please help me understand this: public class BackLister { //Insert code here { List<T> output=new LinkedList<T>(); for(T t:input) output.add(0,t); ...