Questions tagged [downcast]

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

589 questions
4
votes
4 answers

Upcasting and Downcasting confusion in java

Okay. So if... int x=3; int y=5; x=y; That'll make x=5, right? Okay, so if B is a subclass of A... A a=new A(); B b=new B(); a=b; ^^^Why is this considered upcasting? Isn't the "a" supposed to become the "b" and not the other way around? Can…
ABCD123
  • 137
  • 1
  • 6
4
votes
1 answer

Automatic downcast of a pointer to a derived object

Good morning, I have a templatized class and I want to manipulate objects by a vector of pointers. To use a vector of pointers to a templatized class I need this class to be derived from a non-templatized class, and I did it. Here's my problem: to…
pietrom79
  • 41
  • 1
  • 4
4
votes
7 answers

Downcasting a Graphics instance - Why is it allowed?

I'm trying to figure out why it is allowed to downcast a Graphics instance to a Graphics2D instance. It's usually against the rules to downcast a reference type that isn't inheriting the target type. In the graphic-oriented classes the hierarchy is…
Birdman
  • 5,244
  • 11
  • 44
  • 65
4
votes
4 answers

Why Downcasting throws Exception?

In java: Base b = new Base(); Derived d = (Derived)b; throws ClassCastException. Why? Why downcasting throws Exception here? I could not figure out the reason.
mavis
  • 3,100
  • 3
  • 24
  • 32
4
votes
1 answer

Problems with instanceOf when creating new objects from other objects

I have a class that is responsible for creating Formation objects from Shape objects. Shapes are just what the name says, shapes that are being drawn on canvas (TriangleShape, RectangleShape and so on). Formations are similar to shapes, but I plan…
ioreskovic
  • 5,531
  • 5
  • 39
  • 70
4
votes
6 answers

Downcasting/Upcasting error at compile time & runtime?

Please check the below program. I have doubt when compiler will issue casting exception at compiler level and when it will be at runtime? Like in below program, expression I assumed (Redwood) new Tree() should have failed at compiler time as…
sHAILU
  • 165
  • 1
  • 10
4
votes
3 answers

Separation of algorithms and data in a geometry library (triple-dispatching needed?)

I am having trouble designing the part of my application that deals with geometry. In particular, I would like to have a hierarchy of classes and separate methods for intersections. The problem The hierarchy would be something like…
jmeseguerdepaz
  • 332
  • 2
  • 7
4
votes
3 answers

Why can we cast a Java interface to *any* non-final class?

import java.util.Collection; public class Test { public static void main(String[] args) { Collection c = null; Test s = null; s = (Test) c; } } In the code sample above, I am casting a collection object to a…
ares
  • 709
  • 6
  • 13
3
votes
2 answers

Downcasting with Entity Framework

I have a project where I've defined in EF an Employer as a derived class of User. In my process I create a user without knowing whether it will eventually be an employer (or other kinds of users) and later I need to convert it. At first I tried…
ekkis
  • 9,804
  • 13
  • 55
  • 105
3
votes
1 answer

Dart downcasting

I need to make a downcasting in dart. It is possible for example from Object to int, but I'm not being able to do it with my own classes. Am I doing something wrong? or how is the correct way to do it? class Person { final String name; final int…
Mark Watney
  • 307
  • 5
  • 13
3
votes
6 answers

How to change this design to avoid a downcast?

Let's say I have a collection of objects that all inherit from a base class. Something like... abstract public class Animal { } public class Dog :Animal { } class Monkey : Animal { } Now, we need to feed…
basscinner
  • 323
  • 1
  • 5
  • 13
3
votes
4 answers

Objective-C inheritance; downcasting/copying from parent class to derived class

In my program I have a class, say ClassA. I'd like to create a derived class, say ClassB. My program has functions returning instances of ClassA and in certain cases I'd like to use these returns to create an instance of ClassB. Doing the naive…
3
votes
3 answers

If two values have different data types, Java will automatically promote one of the values to the larger of the two data types?

The rule says that if two values have different data types, Java will automatically promote one of the values to the larger of the two data types? In the code below, the value assigned to y is larger than the value of x. Therefore, taking the rule…
kaka
  • 597
  • 3
  • 5
  • 16
3
votes
0 answers

Typecasting causing struct values to change (Swift)

After downcasting an array of structs, my Variables View window shows that all of the values in my struct have shifted "down" (will explain in a second). But when I print(structName), the values are fine. However, when I run an equality check on the…
connorvo
  • 761
  • 2
  • 7
  • 21
3
votes
1 answer

Polymorphic method return type down-casting in java

So I don't know if this is possible I've tried searching it but maybe my search terms are off. Basically I'm wondering, is there a way to create a generic function/method in a super class that returns the downcast object. class A { public…
Hex Crown
  • 753
  • 9
  • 22