Questions tagged [upcasting]

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

244 questions
2
votes
1 answer

Are RxJava2 Observables covariant? or: is it safe to narrow an Observable (upcast its value type)?

Given a class Foo and a class Bar extends Foo, it seems as though it should be safe to cast an Observable to an Observable — Observables are read-only, aren't they? But there doesn't seem to be any out-of-the box way to do it without…
David Moles
  • 48,006
  • 27
  • 136
  • 235
2
votes
4 answers

How does upcasting works in Java?

I was going with the concept of upcasting and downcasting in java, which are also referred as widening and narrowing. UpCasting (Widening) happens automatically from derived class to base class. i.e if it has a is-a relationship. Downcasting has to…
Ritt
  • 3,181
  • 3
  • 22
  • 51
2
votes
1 answer

Why throws upcasting checked exception

I notice that methods in some sophisticated libraries, including the standard JDK, tend to throws upcasting exception. At first I found this phenomenon in the source code of Apache POI, later see it again in java.io.FileWriter like this: public…
SedriX
  • 500
  • 3
  • 10
2
votes
1 answer

No implicit upcasting with function pointers arguments?

This is kind of a strange situation I've run into. I expected for the pointer to be implicitly upcast: struct BaseClass {}; struct DerivedClass : BaseClass {}; void baseClassArgFunc(BaseClass* arg) {} // Function taking BaseClass…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
2
votes
2 answers

Extend a Java Class and force existing libraries to use the extended class

The situation is as follows: I'm using some libraries that use a specific class to load and access configuration parameters. The config loader class is implemented in one of the libraries. What I did is: to extend the config loader class so that it…
MiH
  • 115
  • 2
  • 13
2
votes
6 answers

Cant copy construction be done without creating an explicit function in the pure virtual base class?

My objective is to do a deep copy of a class, but a virtual class is causing trouble. #include using namespace std; class Vir//pure virtual class { public: virtual void hi()=0; }; class Handler:public Vir { public: int…
Nav
  • 19,885
  • 27
  • 92
  • 135
2
votes
2 answers

Variant return type Upcasting Overriding c++

I have problem with clone() method in the following example. I have expected that sizeof(*((*ptr1).clone())) would be the same as b1 and sizeof(*(ptr2.clone())) would be the size of c1 but they are not. They are sizeof(a1). What am I missing? Here…
dodoptak
  • 69
  • 1
  • 6
2
votes
1 answer

Upcasting to interfaces on Moq not working

I'm using the moq for my tests. I upcast the object to its interface and found a problem. Below are tests which show the problem. OBJECTS public interface IVehicle { string Model { get; } } public interface ICar : IVehicle { } public class Golf :…
lsancho
  • 43
  • 1
  • 6
2
votes
6 answers

Upcasting a subclass reference in Java

I'm doing the following exercise in Thinking in Java 4th Edition by Bruce Eckel: Exercise 16: (2) Create a class called Amphibian. From this, inherit a class called Frog. Put appropriate methods in the base class. In main(), create a Frog and…
TheDobriy
  • 309
  • 2
  • 10
2
votes
2 answers

c++ function argument, automatic upcasting of multiple templated class

I have a function taking, as an argument, a template class with multiple parameters (see 'func2' in the following code). I'd like the function to be able to take as argument a class inheriting from the agument type, and automatically resolve…
brahmin
  • 568
  • 1
  • 5
  • 17
2
votes
1 answer

PHP upcasting object

can I upcast an object to it's parent object? Example Code class ClassA { public function foo(){ echo get_class($this); } } class ClassB extends ClassA { public function foo(){ echo get_class($this); // prints ClassB :) …
igaster
  • 12,983
  • 6
  • 26
  • 27
2
votes
1 answer

Java upcasting and downcasting by interfaces

This is probably a dumb question but I need to know. I have an interface as import com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync; public interface AsyncClient extends AmazonDynamoDBAsync{ } And I have a ClientCreator class which has the…
ufucuk
  • 475
  • 9
  • 21
2
votes
1 answer

How expensive is downcasting in Java 6?

There is a method which receives an argument of type Collection and it needs to use some of the methods that are found in the List class when it does work with that argument. Is up-casting expensive in terms of speed? List list =…
stackular
  • 1,431
  • 1
  • 19
  • 41
2
votes
1 answer

Android fragment framework: up-casting does not work

I am working on a Listfragment inside an activity. I have a nested class in an activity. public static class DummySectionFragment extends ListFragment { /** * The fragment argument representing the section number for this * fragment. …
1
vote
1 answer

Fix hyperlink issue in Word-to-XML conversion

We use a two stage process to convert a client's Word documents to XML. The first stage uses upCast to create XML files, then a custom XSLT is run to convert the output to the client's DTD. This process has run successfully on thousands of…
VolcanoLotus
  • 155
  • 5