Newbie: Inheritage, overloading and casting

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?
 
R

Ryan Stewart

Zalek Bloom said:
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?

First, it's inheritance, not inheritage. Second, yes classes will upcast
automatically. Take java.util.Vector for instance. It's a collection that
has an add(Object o) method. You can use that to add any object to the
Vector like so:
String blah = "blah";
someVector.add(blah);
Graphics g = someImage.getGraphics();
someVector.add(g);

No casting is required because every object is a descendant of Object.
However, when getting objects back out of a Vector, you have to cast them
appropriately because the get and remove methods of Vector return Objects,
and Java doesn't automatically downcast. Consider casting primitives, like
int and long. A long is bigger than an int, so casting from int to long will
never cause a problem, so it is done automatically if needed. However,
casting from long to int could lose data, therefore it is never done
automatically. It works basically the same way for objects. Any cast to a
more general object will be done automatically. Any cast to a more specific
object must be done manually.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top