[英]Calling methodA of Animal class by casting
I m Beginner in Java, so someone please say how to call methodA() in Animal Through Mammal object.i cant understand why we need Downcasting and Upcasting clearly. 我是Java的初学者,所以有人请说如何在Animal Through Mammal对象中调用methodA()。我不明白为什么我们需要清楚地向下转换和向上转换。
class Animal{
void methodA(){
System.out.println("I m Animal");
}
}
class Mammal extends Animal{
void methodA(){
System.out.println("I m Mammal child class of Animal");
}
void methodAB(){
System.out.println("Two child class Dog and Cat I have");
}
}
public class UpDownCast {
public static void main(String[] args) {
Animal a = new Animal();
a.methodA();
Mammal m = new Mammal();
m.methodA();
Animal A = new Mammal();
if (A instanceof Mammal){
A.methodA();
}
Animal A1= new Animal();
Mammal M1 =(Mammal)A1;
M1.methodA();
}
}
Thanks in Advance. 提前致谢。
If your instance is Mammal
and if you are calling methodA()
(which is overriden in Mammal
), java will call Mammal
's methodA()
. 如果您的实例是
Mammal
并且正在调用methodA()
(在Mammal
中被覆盖 ),则Java将调用Mammal
的methodA()
。 This is called Dynamic Polymorphism 这称为动态多态
To call methodA()
of Animal
from Mammal
class, you need to invoke it using super
keyword, within the Mammal
class, something like below 要从
Mammal
类调用Animal
methodA()
,需要在Mammal
类中使用super
关键字调用它,如下所示
class Mammal extends Animal{
void methodA(){
super.methodA();// --> calling Animal's methodA()
}
void methodAB(){
System.out.println("Two child class Dog and Cat I have");
}
}
Also, below statement will cause ClassCastException
as Animal
IS NOT A Mammal
(where as a Mammal IS A Animal ) 另外,以下语句将导致
ClassCastException
因为Animal
不是 Mammal
(其中Mammal 是 Animal)
Animal A1= new Animal();
Mammal M1 =(Mammal)A1;
M1.methodA();
how to call methodA() in Animal Through Mammal object.
For that you have to ovveride the methodA
in Mammal
and if you have a Mammal
object with you,you can directly call it.In that case no need of casting. 为此,您必须在
Mammal
检查methodA
,如果您有Mammal
对象,则可以直接调用它。在这种情况下,无需强制转换。
First, you must understand , that by casting you are not actually changing the object itself, you are just labeling it differently. 首先,您必须了解 ,通过投射,您实际上并没有更改对象本身,而只是对其进行了不同的标记。
You cannot call the method of superclass through the subclass object if you have overriden the method. 如果您已重写该方法,则无法通过子类对象调用超类的方法。 As in your case Mammal is the subclass of Animal, hence when you use Mammal object, JVM will look for the methodA implmentation in the Mammal class first, if exists then it will call the Mammal class method, otherwise Animal class method will be called.
由于您的情况是Mammal是Animal的子类,因此当您使用Mammal对象时,JVM会首先在Mammal类中查找methodA实现,如果存在则将调用Mammal类方法,否则将调用Animal类方法。
The idea is quite logical in terms of inheritence, if child has a behavior inherited from Parent and someone calls that behavior on child, then child method will execute. 就继承而言,这种想法是很合乎逻辑的,如果child具有从Parent继承的行为,并且有人在child上调用了该行为,则将执行child方法。 If child does not have a behavior of its own then it will simply inherit it from Parent and hence Parent's method will be called.
如果child没有自己的行为,则它将简单地从Parent继承它,因此将调用Parent的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.