Questions tagged [downcast]

Downcasting permits an object of a superclass type to be treated as an object of any subclass type.

589 questions
0
votes
3 answers

Preventing excessive downcasting

I have a class TreeNode, a class LeafNode which extends TreeNode and a class ParentNode that extends TreeNode as well. In the class ParentNode I have a method getChild that returns a TreeNode. Whenever I call getChild successively, I have to do a…
755
  • 2,991
  • 3
  • 20
  • 34
0
votes
3 answers

Why must I typecast in when I get the following?

First off, I asked this question in a different way yesterday. After more experimenting I'm still unclear as to the "WHY." I understand what I need to do but want to get clear on what's happening. Here's the scenario… I'm starting with a clean…
0
votes
3 answers

Calling methodA of Animal class by casting

I m Beginner in Java, so someone please say how to call methodA() in Animal Through Mammal object.i cant understand why we need Downcasting and Upcasting clearly. class Animal{ void methodA(){ System.out.println("I m Animal"); } } class Mammal…
0
votes
2 answers

how expensive is this php downcasting workaround

This question is related to downcasting in php5 How expensive is this php downcasting workaround? Is this php downcasting workaround too expensive? I've echoed microtimes and It seems that it takes like 0.001. I wonder if It could be a problem in…
cort
  • 1,088
  • 1
  • 11
  • 20
0
votes
1 answer

Best way to determine proper downcast

I have an abstract superclass called C and multiple subclasses of it: SubC1, SubC2, ... Now I have a collection of the superclass C and I want to iterate over it, but do something different depending on which subclass it is. Here is the…
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
0
votes
1 answer

Practical uses of downcasting

I've finished reading the chapters on polymorphism and inheritance and done all the exercises in my Java books. But I still don't understand why I would need to use downcasting in practice. Could you please give some examples from work or projects…
Sing Sandibar
  • 714
  • 4
  • 15
  • 26
0
votes
2 answers

Java -- downcast to an arbitrary class?

I am trying to implement this code: public void ActionTypeOne { doSomething(ActionType1A action) { ... } doSomething(ActionType1B action) { ... } } ... public void register(Action action) { if…
Jeremy
  • 5,365
  • 14
  • 51
  • 80
0
votes
1 answer

Avoid down-casting value along parallel hierarchies

I have two class hierarchies: BaseUnit <- S1Unit , BaseUnit <- S2Unit, ..S3Unit, S4Unit and Base <- S1 , Base <- S2 In Base I stored the unit's value. class Base { protected: BaseUnit unit; // actually this is an upcast of S1Unit, S2Unit // of…
Horst Walter
  • 13,663
  • 32
  • 126
  • 228
0
votes
2 answers

Accessing methods from subclass stored as superclass Java (downcasting?)

I'm working on an inventory program in Java. I have each object in the inventory stored as a relavent class type in a DefaultListModel and JList for each location; for example, if I have a video called "Mulan" and a generic thing called "Ball" in a…
LMNOP
  • 151
  • 6
0
votes
2 answers

how to get the implementation of a superclass from a subclass using upcasting or by other methods?

i just wanted to know how to get the implementation of a superclass using a subclass, for example. class Animal { void poo() { System.out.println("general poo"); } } class Horse extends Animal{ void poo() { …
chip
  • 3,039
  • 5
  • 35
  • 59
0
votes
2 answers

Create Class-Object from Generic

In a generic method, I don't seem to be able to access the generic type of the method at runtime (error: cannot select from a type variable). public A get(Animal a) { Class ac = a.getClass(); if(ac.isAssignableFrom(A.class)) { // <- not…
klamann
  • 1,697
  • 17
  • 28
0
votes
2 answers

Call a method that requires a derived class instance typed as base class in VB.NET or C#

I have two objects - "Spaceship" and "Planet" derived from a base "Obj". I have defined several classes - Circle, Triangle, Rectangle, etc. which all inherit from a "Shape" Class. For collision detection purposes, I want to give Obj a "shape": Dim…
Gravitate
  • 2,885
  • 2
  • 21
  • 37
0
votes
1 answer

MongoDB BsonDocument Down-Casting

I have a collection in MongoDB which I'm trying to "FindAndModify" using C# driver. This collection holds types of a base class and its derived classed, as follows: [BsonDiscriminator(RootClass = true)] public class Father { …
nirpi
  • 715
  • 1
  • 9
  • 24
0
votes
5 answers

using *void as a buffer for static_cast

So I go this: class A; class B : public A; class C : public B; vector *vecA; vector *vecC; And I want to cast a vectC into a vecA. vector *_newA = static_cast< vector* >(vecC); //gives an error So I used void pointer as a…
nahpr
  • 619
  • 7
  • 15
0
votes
3 answers

Downcasting Exception

I made this code: protected Lala lala; private Oyeha oyeha; public void setLala(Lala lala) { this.lala = lala; } this.oyeha = (Oyeha) this.lala; executeHostBean = this.oyeha.updateMethod(a, b, c, d, e); Lala is the parrent…
Cintz
  • 29
  • 2
  • 5