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
2 answers

Cleanest way to fix this castings behavior

Imagine I have a list with 50 different type of a certain subclasses of Node which I expect to be the same type or get a ClassException if not. I have a method which receives this list and a node holding and expeting a certain type I would like to…
Whimusical
  • 6,401
  • 11
  • 62
  • 105
0
votes
1 answer

Python (django) generic module development - Storing a dynamic queryset in the database? Model inheritance

I have an interesting design decision to be made in the context of a Python Django model that I'm planning to eventually release. The classes model an ApprovalRequest, which represents a question / request asked for by a user that can be voted on by…
EB.
  • 3,383
  • 5
  • 31
  • 43
0
votes
1 answer

Java: Reflection against casting when you know superclass

I don't know exactly how to define my doubt so please be patient if the question has already been asked. Let's say I have to dynamically instantiate an object. This object will surely be instance of a subclass of a known, immutable class A. I can…
Ema
  • 104
  • 11
0
votes
5 answers

How do I cast a List from a subclass generic to a parent class generic?

I'm modifying open JDK to add features and I've run into this twice with no good solution. There's a class named JCStatement which extends JCTree. Issue: I want to cast a List into a List. It's clear that a class can reference…
Adriano
  • 389
  • 3
  • 11
0
votes
5 answers

Downcasting gives ClassCastException. How can I fix this?

I'm trying to implement a private message system. Let me know if this is bad design but I have two classes User and Recipient. Recipient is a User so it inherits User. Recipient has additional properties like messageId, readDate, keepMessage. My…
Jonathan
  • 3,016
  • 9
  • 43
  • 74
-1
votes
1 answer

JSON.stringify - downcasting an object

Is there a way to tell the stringify method to convert an object to a primitive datatype? class Bar { constructor() { this.name = 'bar'; } } const obj = {foo: new Bar}; JSON.stringify(obj); // output: '{foo: {name:…
Slev7n
  • 343
  • 3
  • 14
-1
votes
1 answer

While Upcasting I called method of base class(with virtual keyword) but after upcasting its calling override method from derived class

I am trying to understand Upcasting and DownCasting. In the code shown below, I upcast Circle object to Shape and after upcasting methods available are Draw and Duplicate, but when I executed shape.Draw() it is showing output from derived class -…
baudi27
  • 25
  • 5
-1
votes
1 answer

C++: How to iterate over a list of class types for typeid verification and downcasting?

I would like to perform a down casting at execution time. For what I read, if I want to do it, I need to compare the typeid of my polymorphic pointer with those of my derived classes, then do the casting in the correct type. Plus, let's assume that…
R. N
  • 707
  • 11
  • 31
-1
votes
1 answer

Calling object method passing superclass where subclass expected

For example i have three classes class Shape { static bool collide(Box * a, Box * b) static bool collide(Box * a, Sphere * b) static bool collide(Sphere * a, Sphere * b) }; class Box : public Shape{}; class Sphere : public Shape{}; and for…
-1
votes
2 answers

c# best way to assemble multiple lists' elements to sole list

i'm trying to make data container class which has multiple list, i want to make property that offers gather these list's elements to one list should i just code GatherAllCards() like below or are there any good way to compress the code. classes…
haeinsa
  • 57
  • 4
-1
votes
1 answer

Why it is not possible to cast the result of Arrays.asList() to ArrayList?

Return type of Arrays.asList is List and casting it to ArrayList is throwing an error. ArrayList is a child implementation class of List.So, Casting List to ArrayList will be downcasting. Then why below 1st line is throwing Run time…
-1
votes
3 answers

How to access child class method using parent class refrence?

I am getting error while accessing Child class method by using Parent class reference variable. Please help me. How can I access that method? class Parent { public void show() { System.out.println("Show method in Parent class"); …
-1
votes
1 answer

Why upcasted ab.a + ab.b produce result 1 and 1.0?

I don't get how we managed to invoke constructor without parameters in class A at all. How upcasting works in this particular example? When we produce A ab = bb; what exactly ab refers to? public class A { public Integer a; public Float…
cs_student
  • 23
  • 5
-1
votes
1 answer

What is the purpose of downcasting and upcasting at the same time?

I have a parent class Animal and a child class Dog extends Animal. Dog dog = new Dog(); Animal animal1 = (Dog) dog; Animal animal2 = (Animal) dog; I just learned about downcasting and I'd like to ask what is the purpose of doing downcasting and…
lotan
  • 53
  • 5
-1
votes
2 answers

Downcasting from parent to child c#

So I can't figure this out for the life of me. And I have tried casting in multiple forms. This is the parent. /// /// Colour Parent /// public class Colour { #region Public Fields public…
Offnix
  • 27
  • 6