Results 1 to 3 of 3
  1. #1
    Luckyboy is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Default how to address the top method?

    In the following code, I want to address the master.druk() method.
    However the super.druk() gives me middle.druk()


    import java.lang.*;

    class master {
    public void druk()
    {
    System.out.println("master!");
    }
    }

    class middle extends master{
    public void druk()
    {
    System.out.println("middle!");
    }
    }

    class lower extends middle {
    public void druk()
    {
    System.out.println("lower!");
    super.druk();
    }
    }

    public class Inheritence {
    public static void main(String[] args) {
    lower layer=new lower();
    layer.druk();
    }
    }


    Many TnX,
    Luc

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default Re: how to address the top method?

    It can't be done with overridden methods; you have to add another method that acts as a 'trampoline' to the superclass implementation of the method:

    Java Code:
    protected void superDruk() { super.druk(); }
    Put that method in the Middle class and call it in the Lower class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: how to address the top method?

    In essence, if you find yourself doing this then you've probably got your design wrong somewhere.

Similar Threads

  1. set IP address
    By yolimau in forum Networking
    Replies: 10
    Last Post: 05-30-2010, 02:37 AM
  2. Address bar not visible
    By Akashchopra521 in forum New To Java
    Replies: 0
    Last Post: 12-03-2008, 07:13 AM
  3. JSP getting IP address
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 09:05 AM
  4. Getting host name/address
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-19-2007, 04:34 PM
  5. I am not able to print Address
    By Albert in forum New To Java
    Replies: 1
    Last Post: 07-13-2007, 03:37 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
  •