Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

super() in Java two levels up

Options
  • 13-11-2009 4:10am
    #1
    Closed Accounts Posts: 267 ✭✭


    I started off with this:
    public class FirstClass extends MouseAction {
      public void MouseReleased (MouseEvent e) {
        ...
        ...
        ...
        super.mouseReleased(e);
      }
    }
    

    But want to move this code into a derived class:
    public class FirstClass extends MouseAction {
      public abstract void MouseReleased (MouseEvent e);
    }
    
    public class SecondClass extends FirstClass {
      public void MouseReleased (MouseEvent e) {
        ...
        ...
        ...
        super.mouseReleased(e);
      }
    }
    

    This code worked fine in the first example, because calling super () called the method in MouseAction. But the second example's parent is no longer MouseAction, so how do I call it?

    Thanks.


Comments

  • Registered Users Posts: 1,916 ✭✭✭ronivek


    It's late and I've been twatting about with C and IA-32 assembly ****e all night so I'm not exactly in OO-mode but two things immediately spring to mind;

    1) I'd look at your design and make sure what you're trying to do makes sense in terms of your Classes; and also that it makes sense in terms of using inheritance to accomplish your goal. Without understanding the wider picture I can't really give you more than that but something doesn't look right.

    2) If your abstract Class implements an Interface with the same method signature as a Superclass; your Subclasses will be forced to implement the method and you will have access to the instance method of the closest match in the inheritance hierarchy.


  • Registered Users Posts: 2,800 ✭✭✭voxpop


    Id agree with ronivek in regards to the design and what you are trying to achieve.

    Why are you extending MouseAction in the first class if you dont want to use its functionality ? Why not just extend it in the second class ?


  • Closed Accounts Posts: 267 ✭✭waitinforatrain


    voxpop wrote: »
    Id agree with ronivek in regards to the design and what you are trying to achieve.

    Why are you extending MouseAction in the first class if you dont want to use its functionality ? Why not just extend it in the second class ?

    Can't extend it in the second class as Java is only single inheritence.

    Basically I need to do different mouse actions depending on which type of object is selected by the user (it's a graph drawing library), so there will be a few of these SecondClass things, all a bit different. I could probably write special methods in FirstClass to call the MouseAction commands, but it seems like there could be a better way to do it.


  • Registered Users Posts: 2,800 ✭✭✭voxpop


    I didnt mean extend FirstClass and MouseAction , just MouseAction.

    If im understanding this right I would have:
    public class MouseActionListener extends MouseAction{
    	public void MouseReleased (MouseEvent e) {
    		ActionFactory f = ActionFactory.getInstance();
    		Action a =f.createAction(e);
    		a.doStuff();
    	}
    }
    
    


    Then your different types of action all extend Action and overrride the doStuff method. Also because you are using a factory, you can add new actions down the line very easily.


  • Closed Accounts Posts: 267 ✭✭waitinforatrain


    I can't extend it in the second class as I wouldn't be able to use the methods in the first class then.

    I'm going to have to do away with the generalised example I gave to better explain my problem:

    What I had first was this:
    public class MyMarqueeHandler extends BasicMarqueeHandler {
      //A lot of methods here
    
      public void mouseReleased(MouseEvent e) {
        ...
        ...
        super.mouseReleased (e);
      }
    }
    
    Main code:
    graph.setMarqueeHandler( new MyMarqueeHandler() );
    

    Because of the way JGraph is written, I have to give it a whole class with listening methods. This class must extend BasicMarqueeClass.

    Depending on certain conditions I need a few different implementations of the mouseReleased() method, so I created this:
    public abstract class DependencyMarqueeHandler extends AbstractMarqueeHandler {
    
      public void mouseReleased(MouseEvent e) {
        ...
        ...
        //Problem is here
        super.mouseReleased (e);
      }
    }
    
    public abstract class AbstractMarqueeHandler extends BasicMarqueeHandler {
      public abstract void mouseReleased(MouseEvent e);
    }
    
    Main code:
    graph.setMarqueeHandler( new MyMarqueeHandler() );
    

    So you see, I need the other methods in AbstractMarqueeHandler, but just want to implement this one method in a derived class. This is all fine except that I can't use super().


  • Advertisement
  • Closed Accounts Posts: 1,397 ✭✭✭Herbal Deity


    super.super?


  • Closed Accounts Posts: 267 ✭✭waitinforatrain


    My first thought :p doesn't work unfortunately!

    Ended up just making this method:
    	
    	public void callMouseReleased (MouseEvent e) {
    		super.mouseReleased(e);
    	}
    

    Definitely not ideal but it will have to do


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    It is hard to say for sure without being more familiar with your code, however something like the strategy pattern may be a good fit for you.

    http://www.dofactory.com/patterns/patternstrategy.aspx#_self2

    Something like this perhaps.
    public interface MouseStrategy {
      public abstract void mouseReleased(MouseEvent e);
    }
    
    class DependancyMouseStrategy implements MouseStrategy{
         void mouseReleased(MouseEvent e){
          .........
        }
    
    }
    
    class OtherMouseStrategy implements MouseStrategy{
         void mouseReleased(MouseEvent e){
          .........
        }
    
    }
    
    
    
    public abstract class AbstractMarqueeHandler extends BasicMarqueeHandler {
    
    MouseStrategy ms;
    
      public void mouseReleased(MouseEvent e){
             super.mouseReleased(e);
             if(ms != null) ms.mouseReleased(e);
      }
    
       public void setStrategy(MouseStrategy ms)
             this.ms = ms;
       }
    }
    
    //Changed from abstract class for illustrative purposes :)
    public class DependencyMarqueeHandler extends AbstractMarqueeHandler {
    
        DependencyMarqueeHandler (){
           void setStrategy(new DependencyMouseStrategy() )
        }
        //No need to overide mouseReleased just drop in a new strategy class.
    }
    
    //Changed from abstract class for illustrative purposes :)
    public class OtherMarqueeHandler extends AbstractMarqueeHandler {
    
        OtherMarqueeHandler (){
            setStrategy(new OtherMouseStrategy() )
        }
        //No need to overide mouseReleased just drop in a new strategy class.
    }
    
    


Advertisement