Results 1 to 5 of 5
Thread: question about polymorphism
- 02-11-2009, 07:45 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 13
- Rep Power
- 0
question about polymorphism
hi
I've got a question regarding polymorphism in java:
why do i have no access from "Test1" to the member variable "value1" which is initialized in class "Test2" although i'm calling the constructor "Test2()"?Java Code:public class Test1 { public Test1() { Test3 t3=new Test2(); t3.output(); t3.value1=9; //not possible } public static void main(String[] args) { new Test1(); } } public class Test2 extends Test3 { public int value1=5; @Override public void output() { System.out.println("the value is "+value1); } } public class Test3 { public void output(){ System.out.println("class: Test3"); } } output of the program: the value is 5
and why is the output of the program "the value is 5" although I've got no access to "value1" from "Test1" like i said before?
- 02-11-2009, 08:37 PM #2
Houston ... this is the Eagle calling...
OK... some comments...
The Test2 class has no constructor. It only has an output() method.... although i'm calling the constructor "Test2()"?
Because when you execute the Test1 class, the main method gets executed (not the constructor). If you want the Test1() constructor to execute, you have to instantiate it from another class (or call it within the same class).why do i have no access from "Test1" to the member variable "value1"
Shouldn't this be:Java Code:Test3 t3=new Test2();
... or am I missing something here?Java Code:Test2 t2=new Test2();
Did I make sense ? It can get very confusing sometimes.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-11-2009, 09:07 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 13
- Rep Power
- 0
sure it has! the compiler generates a default-constructor automatically if there is no constructor available.The Test2 class has no constructor. It only has an output() method.
well, actually i did this in my main method :) take another lookBecause when you execute the Test1 class, the main method gets executed (not the constructor). If you want the Test1() constructor to execute, you have to instantiate it from another class (or call it within the same class).
no, it should be the way i've written it. this is what is called polymorphism!Shouldn't this be:
... or am I missing something here?Java Code:Test2 t2=new Test2();
thx anyway
- 02-11-2009, 09:59 PM #4
Member
- Join Date
- Jan 2009
- Posts
- 13
- Rep Power
- 0
i've already found answers to my questions:
Because the type of t3 is reference to Test3, and Test3 does not have a value1 variable. The compiler doesn't know that at runtime that reference is going to point to a subclass of Test3 that does have that variable.why do i have no access from "Test1" to the member variable "value1" which is initialized in class "Test2" although i'm calling the constructor "Test2()"?
Because that's how Java's runtime polymorphism works. When you call an overridden method, it's the runtime class of the object that determines whose version of that method gets called. Since the object is a Test2, it's Test2's implementation of the method that gets called. The caller and type of reference don't have to know about the internal details of that class. Only that it's a subclass of Test3, and therefore exposes all the same public instance members as Test3.and why is the output of the program "the value is 5" although I've got no access to "value1" from "Test1" like i said before?
- 02-11-2009, 10:59 PM #5
ooopppsss....
um...uh... how do I start?...
I had heard of this, but never really paid that much attention to what that meant... So I learned somthing today. Thanks!sure it has! the compiler generates a default-constructor automatically if there is no constructor available.
I really have no excuse for this this blooper. Sorry about that... you're completly right...the main was calling Test1(). :owell, actually i did this in my main method take another look
I don't think I've ever seen this particular twist of polymorphism... or I don't remember seeing it like that.no, it should be the way i've written it. this is what is called polymorphism!
So... sorry for the my misunderstandings and erronious statements. On the bright side, I learned a couple of things... not all was lost :)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
inheritance and polymorphism
By tester in forum EclipseReplies: 1Last Post: 12-21-2008, 04:58 AM -
what is polymorphism
By Nari in forum New To JavaReplies: 5Last Post: 04-04-2008, 03:14 AM -
Relation between Polymorphism and Inheritance
By janakiram.attuluri in forum Advanced JavaReplies: 1Last Post: 12-26-2007, 11:32 PM -
what's polymorphism?
By christina in forum New To JavaReplies: 2Last Post: 08-05-2007, 10:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks