Questions tagged [downcast]

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

589 questions
1
vote
1 answer

Terse way to downcast container contents

I'm frequently finding myself doing something like: val json:Map[String,Any] = getJSON(...) val v = json.get("username") val uname = if ( v!=null ) v.asInstanceOf[toString] ) else null whereas what I'd much prefer to write is: val uname =…
user48956
  • 14,850
  • 19
  • 93
  • 154
1
vote
4 answers

Downcasting safety

Here's the code: Base b = new Derived(); // Upcasting // Some actions Derived d = (Derived)b; // Downcasting As I understand the reference is like stencil through which you're looking at some chunk of memory. And upcasting just narrows that…
Paul Kyrejto
  • 1,285
  • 3
  • 12
  • 19
1
vote
7 answers

Downcasting objects in Java

class A{ } public class Demo { public static void main(String s[]) { Object o=(Object) new Demo(); if (((A)(o)) instanceof Object) { System.out.println("true"); } } } I am getting Exception…
Akhilesh Dhar Dubey
  • 2,152
  • 2
  • 25
  • 39
1
vote
2 answers

public interface introduced in derived class

Two classes, D1 and D2 derive from an abstract base class B. Each of them share common public interface declared in B but each of them might also have their own specific public interface (e.g. D2 has D2.Bar() which makes sense only for D2…
Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
1
vote
4 answers

Calling ungeneric methods from generic method in java

Consider the following: public class Doer { public static void doStuff(A a) { System.out.println("a"); } public static void doStuff(B b) { System.out.println("b"); } } Where B extends A And a generic class like this: public…
1
vote
1 answer

systemverilog cast peculiarity

My question is regarding using $cast in SV. If you search for the word cast in the code below, I have on purpose added a '!' to check for unsuccessful casting. In the event of unsuccessful cast, I wanted to see what happens when a call is made to…
V from QC
  • 11
  • 1
1
vote
2 answers

Java downcasting with reflection

I hava a model in Java 1.6 similar to this example: public class Animal { public String color; } public class Dog extends Animal { public Float height; } public class Bird extends Animal { public Integer wings; } Now I want cast from Animal…
earnaz
  • 335
  • 1
  • 4
  • 19
1
vote
1 answer

downcasting in php5

I've realized that there's no downcasting in php5. Is there a common pattern to achieve it?
cort
  • 1,088
  • 1
  • 11
  • 20
1
vote
2 answers

scala downcasting with generics

I'm looking for help with the following problem: case class A(val name: String) class B(name: String) extends A(name) class Base[T <: A](param: T) class SubClass[T <: B](param: T) object Factory { def create[T <: A](param: T) = { param.name…
user1467422
  • 121
  • 8
1
vote
6 answers

java use object as double without explicit cast

Say I have this: Object obj = new Double(3.14); Is there a way to use obj like a Double without explicitly casting it to Double? For instance, if I wanted to use the .doubleValue() method of Double for calculations.
Mastergeek
  • 429
  • 1
  • 6
  • 16
1
vote
6 answers

Down Casting in Java

Can anyone here please explain to me why I get a java.lang.ClassCastException when downcasting a Parent to a Child? public class Child extends Parent{ public static void main(String[] args){ new Child().go(); } void go(){ go2(new Parent(),…
user1369311
1
vote
3 answers

How to avoid too many downcasts in a function

I have too many downcasts in my code. In c++ I can use templates to avoid downcasting. But what is the best implementation of the following example in c#? class Pet { bool mIsDog; } class Dog : Pet // mIsDog = true class Cat : Pet // mIsDog =…
Fan
  • 315
  • 3
  • 11
1
vote
2 answers

how i can reduce the digits after the decimal point?

I need only up to two decimal points. Dim v1, v2, v3, v4, v5, tv, rp1, rp2, rp3, rp4, rp5 As Double Dim Per1, Per2, Per3, Per4, per5 As Double Per1 = v1 / tv * 100 Per2 = v2 / tv * 100 Per3 = v3 / tv * 100 Per4 = v4 / tv * 100 …
sangeen
  • 304
  • 2
  • 3
  • 14
1
vote
2 answers

Java: Casting ParentClass and ChildClass (Downcast Runtime Error)

public class InheritanceDemo { public static void main(String[] args) { ParentClass p = new ParentClass(); ChildClass c = new ChildClass(); //Casting ChildClass to ParentClass ParentClass pc = new…
SpiritOfDragon
  • 1,404
  • 2
  • 15
  • 27
1
vote
1 answer

Implicit downcast from 'System.IO.Stream' to 'System.IO.MemoryStream'

function SerializeObject(pObject : Object) { var XmlizedString : String = null; var memoryStream : MemoryStream = new MemoryStream(); var xs : XmlSerializer = new XmlSerializer(typeof(XmlData)); var xmlTextWriter : XmlTextWriter = new…
smeddles24
  • 101
  • 1
  • 1
  • 8