This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What is a differenace here? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is a differenace here?" Watch "What is a differenace here?" New topic

What is a differenace here?

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
I have a very basic question.
see if I have two classes say class A and class B and a extends B. I can create the objects in following fashion:
1. a x= new a()
2. b y = new a()
I was just wondering what is the differance here? Does some has advantange over other?
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
I recommend reading How My Dog Learned Polymorphism.
Good Luck,
-Dirk Schreckmann

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
check out the code below:

When you run this you'll see that there is a big difference in the way you declare and create your instance variables. Notice the difference in the way the instance variables behave as opposed to the way the methods behave. Depending on the how you declare your variables you can get different behavior based on the inheritance heirarchy.
Your class A extends class B. So every A object is also a B object and can access the B objects non private variables and methods. When you create the variable x your saying that x will reference an A object and then you create an A object for it to hold. When you create the variable y you tell the compiler that y will reference a B object then you create an A object for it to reference - this is ok because an A object 'is a' B object by inheritance.
When you actually use the variables you will get different behavior. For example, instance variables (the variable var in both A and B) are accessed according to the type of the variable. So x will access the var variable from class A while y will access the variable from class B.
Methods, on the other hand, (along with class variables) are asssociated (at run time) with the type of the class that the reference points to. The variable x actually references an A object so the method called is the one declared in the class A. The variable y also points to an A object so it also calls the class method - regardless of the type the variable was declared to hold. this can all be a little confusing untl you play with it a little bit and read some different explainations on it.
For a good explaination check out the polymorphism camfire story. The java tutorial also has some good info on it.
hope that helped

I agree. Here's the link:
subject: What is a differenace here?
It's not a secret anymore!