overriding method in interface

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    overriding method in interface

    Code:
    public interface IQResStack {
            void push(IAbstractQueryResult a);
        
    }
    
    public class QResStack implements IQResStack{
       
     public void push(AbstractQueryResult a){
     }
    it says it does not overide the method in interface?
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Well, it doesn't. The interface defines a function that overrides a function that takes an IAbstractQueryR esult which, judging by the name, is an interface. The class however has a function taking an AbstractQueryRe sult which is a class. That won't work.

    Also when overriding a method it is good practice to use the @Overrides annotation; that way the IDE you're using will tell you when something goes wrong with overriding.

    So, the end result would look like this:
    [code=java]public class QResStack implements IQResStack {

    @Override
    public void push(IAbstractQ ueryResult a) {
    }
    }[/code]

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      what about pop()
      it says that it does not override the method in the supertype

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        I see no pop() function in your code. However if you're getting that error I guess you've added the @Override annotation to it? You can only do that legally if your function is actually overriding a function from a supertype, i.e. parent class or implemented interface. And there are some limits; the signatures of the functions have to match and you can't override private or final functions and you can't override anything but methods.

        Comment

        • oll3i
          Contributor
          • Mar 2007
          • 679

          #5
          so it's like that? that works

          Code:
          @Override
          public IAbstractQueryResult pop () {
          return aR;
            }
          @Override
          public void push(IAbstractQueryResult a) { 
              }

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            I'm guessing your interface looks like this:
            Code:
            public interface IQResStack {
            
                    IAbstractQueryResult pop();
            
                    void push(IAbstractQueryResult a);
            }
            correct? If so then yes, your use of the @Override annotation should be valid.

            Comment

            Working...