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 6 of 6

Thread: Late binding problem ?

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Late binding problem ?

    Hello to everyone. I have a problem to which i cannot find any answers. I tried to google it but i didn't found anything which could help me.

    So i have this small sequence of code:
    class class1 {
     
    	private int value;
     
    	public class1(){ 
    		this.value = 10;
    	}
     
    	public int getValue(){ 
    		return this.value;
    	}
     
    	public int getAnotherValue(auxiliar aux) { 
    		return aux.visit(this);
    	}
     
    	public void setValue( int value) { 
    		this.value = value;
    	}
    }
     
    class class2 extends class1 { 
     
     
    	public class2() {
    		this.setValue(20);
    	}
     
     
    }
     
    class auxiliar { 
    	public int visit ( class1 c1) { 
    		System.out.println("not here");
    		return c1.getValue();
    	}
     
    	public int visit ( class2 c2 ) { 
    		System.out.println("here");
    		return c2.getValue();
    	}
    }
     
    public class Main	{ 
    	public static void main ( String[] args) { 
    		class2 c2 = new class2();	
     
    		auxiliar aux = new auxiliar();
    		System.out.println(c2.getAnotherValue(aux));
    	}
    }

    the output is the following:
    not here
    20

    I would like it to be:
    here
    20

    I noticed that when i copy the getAnotherValue(...) to class2 everything works as i want. But i don't think that's a smart approach. Also i think the problem comes from using 'this' in class1, getAnotherValue(...). But i don't know how it should be better to solve it.

    What amazes me the most is the fact that if i add System.out.println(this.getClass().getName()); in the getAnotherValue(...) method the output is class2.
    Last edited by sanguinarius; December 27th, 2011 at 04:19 PM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Late binding problem ?

    What amazes me the most is the fact that if i add System.out.println(this.getClass().getName()); in the getAnotherValue(...) method the output is class2.
    Why does this really amaze you? You are calling it from class C2's object, also you have never referred C1 to C2.
    c2.getAnotherValue(aux)
    And it's calling
    public int visit ( class1 c1) { 
    		System.out.println("not here");
    		return c1.getValue();
    	}
    this as you are calling it inside the class1.

    I know i am not that descriptive but i don't know how to explain it any further. Any one else can explain here too please. Thanks

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Late binding problem ?

    So how can i make it to be called from where i want without using the copy-pasting method of the same method in class2 ?

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Late binding problem ?

    Quote Originally Posted by sanguinarius View Post
    So how can i make it to be called from where i want without using the copy-pasting method of the same method in class2 ?
    As far as i know, you misunderstood the concept. Let's make it easy. What do you want to do? You are passing the value of class2 object and you want program to call method accepting class1's object? How do you really think, compiler knows, what you want to do unless you specify it. Just pass it the class1's object and it will do what you want it to do.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Late binding problem ?

    I want to pass a value of class2 to the method getAnotherValue() and i want it to make a call to public int visit ( class2 c2 ) and not public int visit ( class1 c1) as it does now. I believe that is the normal way how things should work. But they don't.

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Late binding problem ?

    Okay, so the reason might be , as you are using value variable of class1, as you can see class2 only extends and sets this value , it doesn't have it's own variable. So, i guess (I am not pretty sure) that when you call
    aux.visit(this);
    it takes "this" as type Class1 not Class2 and that's somehow seems you weird. Why don't you try creating a variable for class2 named value2 and set it's value and then try calling the same function.

Similar Threads

  1. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  2. Replies: 3
    Last Post: December 27th, 2011, 07:55 AM
  3. key binding: VK_ALT and VK_SHIFT not working
    By gib65 in forum AWT / Java Swing
    Replies: 5
    Last Post: November 25th, 2010, 05:54 PM
  4. Extracting the BINDING element from WSDL file
    By Sai in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2010, 02:56 AM