Questions tagged [upcasting]

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

244 questions
0
votes
2 answers

Will upcasting classes affect the method call? Why? Security issues?

Let's have the specified code: class Foo { public void doSomething(){ System.out.println("Foo"); } } class Bar extends Foo{ @Override public void doSomething(){ System.out.println("Bar"); } } public class doFoo{ …
acrastt
  • 104
  • 1
  • 7
0
votes
2 answers

In scala 3, is it possible to make covariant/contravariant type constructor to honour coercive subtyping?

This is a simple example: object CoerciveCovariance { trait Cov[+T] def cast[A, B](v: Cov[A])( implicit ev: A <:< B ) = { v: Cov[B] } } It doesn't compile: CoerciveCovariance.scala:11:5: Found: (v :…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
0
votes
1 answer

Using protected method of the parent class in another package

I have Tadpole class that extends Frog, as follows: 1: package animal; 2: public class Frog { 3: protected void ribbit() { } 4: void jump() { } 5: } 1: package other; 2: import animal.*; 3: public class Tadpole extends Frog { 4: public static…
motodev
  • 13
  • 5
0
votes
2 answers

Can't cast from a child ref to a parent ref in C#. Why not?

I am new to using C#. I am most familiar with C++ and Java when it comes to Object-Oriented Programming and polymorphism. Can someone tell me why this upcast does not work in C#, and what I can do to get the upcast to work with ref. using…
Raisintoe
  • 201
  • 1
  • 4
  • 11
0
votes
1 answer

Java accesing properties with same name in both classes

I have 2 classes, A and B which B inherits from A. Both classes have a property of type int called w. In class A w is public and in class B w is private. I made an object of type A using B constructor - A a = new B() Yet when i tried to access B's…
dudex198
  • 25
  • 5
0
votes
2 answers

Why does my child class object use the parent classes variable but not methods when upcasting?

Why does "beagle" use the "Dog" execution of speak() but uses the Animal classes legs variable (seems inconsistent)? And why do the constructors get run twice each if I only create 2 objects? (seen in output below) This was an upcasting/downcasting…
ZaneK
  • 301
  • 2
  • 14
0
votes
1 answer

Creating class fields in the constructor

I recently started learning Java. I got to the point of upcasts and downcasts and this is what I encountered. I understand how type conversion works, but this moment keeps me awake. If you create class fields outside the constructor and assign…
0
votes
2 answers

Casting a struct to a trait object with associated types in Rust

In rust, you can have a trait, implement it into a struct, and upcast your struct to a trait object : trait T {} struct S {} impl T for S {} fn main() { let s: S = S {}; let s_as_t: &dyn T = &s; } This is an incredibly useful feature,…
0
votes
0 answers

Can't upcast interface to extended interface

I have 2 interfaces public interface A { method1(); method2(); } public interface B extends A { method3(); method4(); : } I am in need of converting (upcasting) B to A in one of class. But so far I could not figure out how do I do…
0
votes
2 answers

How are we being able to access constructor of child in upcasting

public class Main { public static void main(String[] args) { Parent obj=new Child(1,2,3); //child constructor accessed during object creation System.out.println(obj.p); …
0
votes
2 answers

When upcasting in Java, what is the breakdown of what each term means?

So say I have a class Vehicle and subclass car that have instantiated objects in the main (Java) Vehicle v = new Vehicle(); Car c = new Car(); When we do... Vehicle c1 = new Car(); What does the word vehicle represent and what does the word car…
ZaneK
  • 301
  • 2
  • 14
0
votes
2 answers

UpCasting and DownCasting Java accessing methods

I am unable to understand upcasting and the accessing of methods. I have read through multiple other questions on a similar understanding problem I am having but I still can not understand. I am hoping that a more personalized answer to my question…
0
votes
1 answer

C++ derived-class members after downcasting

I recently learned about upcasting and downcasting in C++. However I came up with a few questions during reading about downcasting. Say I have two classes class Base { public: virtual void foo() {} }; class Derived : public Base { public: …
jleng
  • 55
  • 1
  • 7
0
votes
1 answer

Upcasting to "parent type" in factories in C#

This is the code I would like to write at the end: var notification = NotificationFactory.FromJson(jsonString); if (notification is UserUpdateProfileNotification) { // notification is of type UserUpdateProfileNotification so I can access…
Titouan56
  • 6,932
  • 11
  • 37
  • 61
0
votes
0 answers

Upcasting in spring boot

Does spring boot support upcasting like we have in Core Java program. For example: Parent p = new Child(); How do we achieve this in spring boot? Note: As name suggest here, Child class extends Parent class.
Nishant
  • 53
  • 5