The blog of Jafar Aliyev

Archive

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, then remember, that all instance methods are implicitly public abstract. Hence if you implemet 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

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

Wrapper objects

Static and instance initilization blocks

Overloading

Java access and nonaccess modifiers
Look at this cheat sheet for Java access and nonaccess modifiers

Implicit typecasting

Protected details

Static methods can not be overriden
Here I explain, why static methods can not be overriden

Implicit typecasting

Java Tips

Variable hiding

Postfix operators

Tricky Polymorphism

Pattern Matching

Default values

String equality

© 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.