Results 1 to 12 of 12
  1. #1
    viking90 is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default Inherited method returning bad value

    I have the following code:

    Java Code:
    class A {
    int n = 0;
    int foo(){
      return n;
    }
    }
    
    class B extends A {
    int n = 5;
    }
    and now when I write
    Java Code:
    A b = new B();
    b.foo();
    , I expect it to return a value of 5, but it returns 0.

    I think the problem lies somewhere in the upcasting of "b" to an "A" class, but I do not want to cast it like this:
    Java Code:
    ((B)b).foo()
    because besides the B class I have also another similar classes that extend the "A" class, so that would be solvable only by using multiple ifs, which is of course against the basic principles of OOP (polymorphism).

    I hope you understand what I mean. So the question again is: How do I get b.foo() to return 5 ?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think you may have a misunderstanding about polymorphisms in Java. While methods can be polymorphic, variables cannot. Your output is the expected output in this situation.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    A solution:
    Java Code:
    class A2 {
      int n;
      A2(int n) {
        this.n = n;
      }
      
      int foo() {
        return n;
      }
    }
    
    class B2 extends A2 {
     B2(int n) {
       super(n);
     }
    }

  4. #4
    viking90 is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default

    Ah, I didn't know that variables cannot be polymorphic. But then how do I make the classes extending the A class to use their "own variable n" in the foo() method? Because now it seems that since foo() is defined in the A class, it uses the A's "n" variable.

    Oh, I see you posted the solution first. I didn't want to solve this by using parameters, but anyway, thanks :)
    Last edited by viking90; 04-04-2010 at 10:01 PM.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,851
    Rep Power
    19

    Default

    Since, in a "proper" implementation, a child class shouldn't access the attributes of a parent directly, then you won't get this problem...

  6. #6
    viking90 is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default

    What do you mean by "proper implementation" ? Using setters/getters and private/protected fields? I tried this, but it didn't help.
    And you also mentioned child classes accessing their parent's attributes, but I think in my case it's quite the opposite: parent (the foo method) is accessing its child's variable (b.n)

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,851
    Rep Power
    19

    Default

    Which a parent cannot do...which is how OO works. A parent cannot know about a child.
    Which is why your parent does not know about the "n" attribute declared in the child...how can it?

  8. #8
    viking90 is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default

    OK, this is how I solved it:
    Java Code:
    abstract class A {
    	int n = 0;
    
    	protected int foo(int value){
    		return value;
    	}
    	
    	abstract int foo();
    }
    
    
    class B extends A {
    	int n = 5;
    
    	int foo(){
    		return super.foo(this.n);
    	}
    }
    It looks a bit more complicated, but the main thing is, that it works now :)

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It looks FUBAR to me. Sorry. :(

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,851
    Rep Power
    19

    Default

    Quite.
    Why does B need the same attribute as A?
    If it's supposed to represent the same attribute then why not simply use the one in A?

  11. #11
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Yeah, it is kinda overcomplicated. Fubarables solution is a pretty standard way to deal with inheritance and is simpler and easier to understand.

  12. #12
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by viking90 View Post
    What do you mean by "proper implementation" ? Using setters/getters and private/protected fields?

    you're right. if you access member variables directly this result in a high coupling and poor encapsulation which is not wanted in oo. to avoid this declare your class variables private and implement public getters and setters to get and changing the variables.

Similar Threads

  1. returning an object from a method
    By bigj in forum New To Java
    Replies: 7
    Last Post: 01-08-2010, 12:39 PM
  2. Returning complex data types from a web method
    By Tshegofatsom in forum Advanced Java
    Replies: 6
    Last Post: 05-15-2009, 03:30 PM
  3. Replies: 3
    Last Post: 12-10-2008, 01:38 AM
  4. Need help. Method won't returning proper value..
    By zlwilly in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 09:44 PM
  5. hiding inherited methods
    By java_fun2007 in forum New To Java
    Replies: 1
    Last Post: 01-05-2008, 02:16 PM

Tags for this Thread

Posting Permissions

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