AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Newbie: Inheritage, overloading and casting

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Zalek Bloom
Guest





PostPosted: Fri Dec 26, 2003 2:19 pm    Post subject: Newbie: Inheritage, overloading and casting Reply with quote



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?
Back to top
Ryan Stewart
Guest





PostPosted: Fri Dec 26, 2003 2:57 pm    Post subject: Re: Newbie: Inheritage, overloading and casting Reply with quote



"Zalek Bloom" <zalekbloom (AT) hotmail (DOT) com> wrote

Quote:
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.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.