class A
{
int i=10;
void show()
{
System.out.println("show()");
}
}
class B extends A
{
int j=20;
void show1()
{
System.out.println("show1()");
}
public static void main(String ar[])
{
A a1=new B();//What happened internally here.please give me answer.
a1.show();
a1.show1();
}
}
相关问题
- Delete Messages from a Topic in Apache Kafka
- how to define constructor for Python's new Nam
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
What happened is that you created an instance of
B
and assigned the reference to an variable of typeA
. That's OK, because aB
instance is aA
.In the next line you called one of the
A
methods on theB
instance. That's OK.In the last line you attempted to call a
B
method. But since the static type ofa1
isA
that results in a compilation error. However, if you had written the following, it would have compiled and run just fine.If this doesn't answer your question, please rephrase it so that we can understand it better.
Happened that you shouldn't be able to call:
because even if a1 instance type is B (new B()), you are treating it like an A object, which hasn't the show1 method defined.