Results 1 to 5 of 5
  1. #1
    becky is offline Member
    Join Date
    Jan 2009
    Posts
    13
    Rep Power
    0

    Default question about polymorphism

    hi

    I've got a question regarding polymorphism in java:
    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
    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()"?
    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?

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Houston ... this is the Eagle calling...

    OK... some comments...
    ... although i'm calling the constructor "Test2()"?
    The Test2 class has no constructor. It only has an output() method.
    why do i have no access from "Test1" to the member variable "value1"
    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).
    Java Code:
    Test3 t3=new Test2();
    Shouldn't this be:
    Java Code:
    Test2 t2=new Test2();
    ... or am I missing something here?

    Did I make sense ? It can get very confusing sometimes.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    becky is offline Member
    Join Date
    Jan 2009
    Posts
    13
    Rep Power
    0

    Default

    The Test2 class has no constructor. It only has an output() method.
    sure it has! the compiler generates a default-constructor automatically if there is no constructor available.

    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).
    well, actually i did this in my main method :) take another look

    Shouldn't this be:
    Java Code:
    Test2 t2=new Test2();
    ... or am I missing something here?
    no, it should be the way i've written it. this is what is called polymorphism!

    thx anyway

  4. #4
    becky is offline Member
    Join Date
    Jan 2009
    Posts
    13
    Rep Power
    0

    Default

    i've already found answers to my questions:
    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 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.
    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?
    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.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default ooopppsss....

    um...uh... how do I start?...
    sure it has! the compiler generates a default-constructor automatically if there is no constructor available.
    I had heard of this, but never really paid that much attention to what that meant... So I learned somthing today. Thanks!
    well, actually i did this in my main method take another look
    I really have no excuse for this this blooper. Sorry about that... you're completly right...the main was calling Test1(). :o
    no, it should be the way i've written it. this is what is called polymorphism!
    I don't think I've ever seen this particular twist of polymorphism... or I don't remember seeing it like that.

    So... sorry for the my misunderstandings and erronious statements. On the bright side, I learned a couple of things... not all was lost :)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. inheritance and polymorphism
    By tester in forum Eclipse
    Replies: 1
    Last Post: 12-21-2008, 04:58 AM
  3. what is polymorphism
    By Nari in forum New To Java
    Replies: 5
    Last Post: 04-04-2008, 03:14 AM
  4. Relation between Polymorphism and Inheritance
    By janakiram.attuluri in forum Advanced Java
    Replies: 1
    Last Post: 12-26-2007, 11:32 PM
  5. what's polymorphism?
    By christina in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 10:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •