Z
Zalek Bloom
I am learning about heritage and have some questions about it.
Here is a simple example:
class A {
void m1(A a) {
System.out.print("A");
}
}
class B extends A {
void m1(B b) {
System.out.print("B");
}
}
class C extends B {
void m1(C c) {
System.out.print("C");
}
}
class D {
public static void main(String[] args) {
A a = new A();
C c = new C();
a.m1(c);
}
}
As I understand the statement a.m1(c) takes a method m1, defined in a
class A. Now the method m1 defined in the class A works on variable
type A only. I know that class C extends the class A, so type C is
also a class A, but how Java knows it? For example - Container class
extends Component class, that means I can apply all methods that work
on Component class on the Container without any casting?
Here is a simple example:
class A {
void m1(A a) {
System.out.print("A");
}
}
class B extends A {
void m1(B b) {
System.out.print("B");
}
}
class C extends B {
void m1(C c) {
System.out.print("C");
}
}
class D {
public static void main(String[] args) {
A a = new A();
C c = new C();
a.m1(c);
}
}
As I understand the statement a.m1(c) takes a method m1, defined in a
class A. Now the method m1 defined in the class A works on variable
type A only. I know that class C extends the class A, so type C is
also a class A, but how Java knows it? For example - Container class
extends Component class, that means I can apply all methods that work
on Component class on the Container without any casting?