Navigator


Archive

201139
201230
201312
20151
201633
201755
201865
201955
20234
20242

Creation date:

Interface methods are always public

Look at the following code carefully and try to find the error:

package mypackage;
 interface MyInterface{
   void go();
 }

 public class B implements MyInterface{
   void go(){
     System.out.println("running method go()");
   }

   public static void main(String[] args) {
     B b = new B();
     b.go();
   }
 }

If you can't find the erroneous code, then remember, that all interface methods are implicitly public abstract. Hence if you implement the method your implementation must have public access modifier because you can not assign weaker access privileges than in interface. Your go() method must be look like so:

   public void go(){
     System.out.println("running method go()");
   }

Author: Jafar N.Aliyev (Jsoft)

Read also

Static methods can not be overriden

Here I explain, why static methods can not be overriden

Use instanceof carefully

Usage of 'instanceof' method in some situations will give you a compilation error

Java access and nonaccess modifiers

Look at this cheat sheet for Java access and nonaccess modifiers

© Copyright

All articles in this site are written by Jafar N.Aliyev. Reproducing of any article must be followed by the author name and link to the site of origin(this site). This site also keeps the same rules relative to the articles of other authors.