Cast an object of super class to a sub class - down casting
Tag : java , By : Thomas Gueze
Date : March 29 2020, 07:55 AM
wish of those help A Cat is an Animal. If I give you an animal (doesn't have to be cat), how would you convert it to a cat?
|
Why can't I cast super class reference to a subclass that is extending another super class as well?
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You should avoid using namespace std as std::left and std::right clash with your own definitions. See http://ideone.com/tMa28j for a working version.
|
how to cast up to super class when there is an override function in the sub class
Tag : swift , By : eusden
Date : March 29 2020, 07:55 AM
help you fix your problem Because you're overriding the method in a subclass, you're getting dynamic dispatch. The method implementation to call will be based on the dynamic type of the instance that it's called on. Upcasting a Jaguar to a Car only changes the static type of the instance – the dynamic type is still a Jaguar, for that's the type of instance you created. Therefore upcasting has no bearing whatsoever on the dynamic dispatch of a method – nor should it, as the whole point of dynamic dispatch is to ensure that the correct method implementation for the given instance is called no matter what it's statically typed as. class Car {
static func info(for car: Car) {
print("You've got a Car")
}
}
class Jaguar : Car {
static func info(for jaguar: Jaguar) {
print("You've got a Jaguar")
}
}
let jaguar = Jaguar()
Jaguar.info(for: jaguar) // You've got a Jaguar
Car.info(for: jaguar) // You've got a Car
let car = jaguar as Car
Jaguar.info(for: car) // You've got a Car
protocol Car {}
extension Car {
func info() {
print("You've got a Car")
}
}
class Jaguar : Car {
func info() {
print("You've got a Jaguar")
}
}
let jaguar = Jaguar()
jaguar.info() // You've got a Jaguar
let car = jaguar as Car
car.info() // You've got a Car
|
Cast super class instance returned bya super class method to a subclass instance
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Consider: , Let's address the first part of the question : public class Super {
Integer state = 0;
Super f() {
Super x = new Super();
incrementState(x);
return x;
}
protected void incrementState(Super x) {
x.state = this.state + 1;
}
}
public class Sub extends Super {
Sub f() {
Sub s = new Sub();
incrementState(s);
return s;
}
}
|
Cast fails when casting a sub class equals to the super class
Date : March 29 2020, 07:55 AM
wish of those help Here's a simple example to illustrate. Imagine that PersonOverwritten looked like this: public class PersonOverwritten extends Person {
public PersonOverwritten(String nome, int idade) {
super();
}
public void pickupSticks() {}
}
Person superPerson = new Person("marc", 18);
PersonOverwritten subPerson = (PersonOverwritten) superPerson;
subPerson.pickupSticks();
Person person = new PersonOverwritten("marc", 18);
((PersonOverwritten)person).pickupSticks();
|