This discussion is archived
9 Replies Latest reply: Nov 16, 2009 11:36 PM by 843789 RSS

java inheritance - please explain

843789 Newbie
Currently Being Moderated
<code>class A {

public void talk(){
this.sayIt();
}

private void sayIt(){
System.out.println("class A says...");
}


}

class B extends A {

private void sayIt(){
System.out.println("class B says...");
}


}


B b = new B();
b.talk()
//output
class A says...


</code>I cannot get this since:

Class B inherits from class A the public member and cannot see/inherit the private function.

So in class B we could call talk() //since it is inherited by the parent class.

Now in the talk() method there is a call to sayIt() and since say it is defined in class B
i would expect a call to B.sayIt() to be made when this.sayIt() is executed.

Doesn't "this" refer to the B class ?

If you have some time please explain..


  • 1. Re: java inheritance - please explain
    gimbal2 Guru
    Currently Being Moderated
    private methods are not inherited, and thus you cannot override them. Make the sayIt() method protected and see your luck change.
  • 2. This Thread is now moved
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Note: This thread was originally posted in the [Java Programming|http://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment.
  • 3. Re: java inheritance - please explain
    YoungWinston Expert
    Currently Being Moderated
    Andreaspag wrote:
    I cannot get this...
    Strictly speaking, what you are talking about here is polymorphism, which is a by-product of inheritance, but not quite the same thing.
    Class B inherits from class A the public member and cannot see/inherit the private function.
    So in class B we could call talk() //since it is inherited by the parent class.
    All right so far.
    Now in the talk() method there is a call to sayIt() and since say it is defined in class B
    i would expect a call to B.sayIt() to be made when this.sayIt() is executed.
    Doesn't "this" refer to the B class ?
    It can be a little confusing to start with.
    The answer to your basic question is 'Yes it does'; but the talk() method it inherits comes from A (the superclass), and that method knows only about A's methods (after all, class B might not exist).

    As gimbal said, the answer is to make A's sayIt() method protected. What this does is cause B's sayIt() method to override (basically, this means to hide) A's, so that now, when talk() executes sayIt(), it will execute the method for the subclass. Basically, A's sayIt() method becomes a 'default implementation' that is only used if the subclass doesn't define one for itself.

    An interesting little test you might want to try (after making sayIt() protected):
    A obj1 = new A();
    A obj2 = new B();
    obj1.talk();
    obj2.talk();
    What happens and why?

    When you understand that, you'll be most of the way to understanding inheritance and polymorphism. Good luck.

    Winston
  • 4. Re: java inheritance - please explain
    800387 Newbie
    Currently Being Moderated
    When you understand that, you'll be most of the way to understanding inheritance and polymorphism. Good luck.
    I wish that it could be that easy.

    - Saish
  • 5. Re: java inheritance - please explain
    YoungWinston Expert
    Currently Being Moderated
    Saish wrote:
    When you understand that, you'll be most of the way to understanding inheritance and polymorphism. Good luck.
    I wish that it could be that easy.
    Well, let's say 50% then, but I can definitely understand OP's original "misconception"; I had it myself.

    @OP: Great minds think alike. Hopefully, we've helped put put you on the shining path.

    Winston
  • 6. Re: java inheritance - please explain
    800387 Newbie
    Currently Being Moderated
    IMO, here's another part of the equation. (Can't remember if it was PaulCW or GeorgeMC who first posted this, but it changed the way I look at OO). http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html

    - Saish
  • 7. Re: java inheritance - please explain
    843789 Newbie
    Currently Being Moderated
    Is this problem Still there?
    Were you able to understand your problem?
  • 8. Re: java inheritance - please explain
    YoungWinston Expert
    Currently Being Moderated
    Saish wrote:
    IMO, here's another part of the equation. (Can't remember if it was PaulCW or GeorgeMC who first posted this, but it changed the way I look at OO). http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html
    Thanks for that Saish (and georgemc or paulcw).
    It answered a thorny little problem I've been having with a set of classes I've been developing, and I think I may be pointed on the right path now.

    Cheers

    Winston
    .
  • 9. Re: java inheritance - please explain
    800387 Newbie
    Currently Being Moderated
    My bad. It was Alex.P.

    I loved that article. There's another one similar here: www.pragprog.com/articles/tell-dont-ask

    I ended up refactoring a ton of code, and it turned out, at least 80% of my accessors did not in fact need to exist. I ended up writing something like:
    public interface Adapter<T> {
        /**
         * Converts the current object into the specified type.
         * 
         * @return  T                           Result of conversion
         */
        
        public abstract T adapt();
    }
    Or in other cases:
    public interface Transformer<M, T> {
        /**
         * Maps the type T to the type M.  
         * 
         * @param   target                      Type to map
         * @return  M                           Mapped type
         * @throws  ValidationException
         */
    
        public abstract M map(final T target)
            throws ValidationException;
    }
    Depending on whether I needed to pass additional information in to the POJO with the accessors removed. If I need multiple adapters, I simply create two public static nested classes, each of which implements Adapter (or Transformer). If I need to maintain state, I remove the static modifier. If I need to pass in arguments that are not suited to a Transformer, I initialize them in the ctor of the nested public class. Works exceptionally well so far.

    - Saish