Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Please Help!!!

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Please Help!!!

    I have a problem in the following code....


    ===========================================

    package com.rex;
     
    class X
    {
    	 int i=10;
    	 void test()
    	{
    		System.out.println("X");
    	}
    }
     
    class Y extends X
    {
    	int i=20;
    	void test()
    	{
    		System.out.println("Y");
    	}
    }
    class Z extends Y
    {
    	int i=30;
    	void test()
    	{
    		System.out.println("Z");
    	}
    }
    public class Manager6 
    {
    	public static void main(String[] args) 
    	{		
    		Z z1=new Z();
    		Y y1=z1;
    		X x1=y1;
     
    		System.out.println(x1.i);    
    		System.out.println(y1.i);	
    		System.out.println(z1.i);	
     
    		x1.test();  
    		y1.test();	
    		z1.test();	
    	}
    }
    ================================================== =========


    Here the output is
    10
    20
    30
    z
    z
    z
    ================================================== ==========

    can anyone explain......
    when all the references are pointing to same object of --"Z"....the value 0f i should be 30 in all SOP's

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,146
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Please Help!!!

    Can you post what the output is supposed to look like?

    Where in your code do you print the lowercase z? All I see being printed is uppercase Z?

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Please Help!!!

    The redeclaration of class variable i in each subclass hides or shadows the declaration of i in the superclass, but a subclass will still have the superclass variables as well as its own. A particular subclass reference will see the variable for that particular subclass type.

    The method declaration works differently. Each method declaration overrides the superclass method declaration, and Java ensures that, regardless of the reference type used, the method for the 'real' type of the object is always called. This is the essence of polymorphism, as it allows you to call a method on various subclass objects using a reference to a common superclass, and have them all behave differently according to the subclass type.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help!!!

    Quote Originally Posted by dlorde View Post
    The redeclaration of class variable i in each subclass hides or shadows the declaration of i in the superclass, but a subclass will still have the superclass variables as well as its own. A particular subclass reference will see the variable for that particular subclass type.

    The method declaration works differently. Each method declaration overrides the superclass method declaration, and Java ensures that, regardless of the reference type used, the method for the 'real' type of the object is always called. This is the essence of polymorphism, as it allows you to call a method on various subclass objects using a reference to a common superclass, and have them all behave differently according to the subclass type.
    Thankx dlorde...........


    So does that mean that the thing that is happening here for methods is b'coz of dynamic binding and that for variables is b'coz of static binding...???

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Please Help!!!

    Quote Originally Posted by tushar7250 View Post
    So does that mean that the thing that is happening here for methods is b'coz of dynamic binding and that for variables is b'coz of static binding...???
    Yes.

    See Dynamic Binding vs Static Binding.