Skip to content

aviyehuda.com

Menu
  • Open Source
  • Android
  • Java
  • Others
  • Contact Me
  • About Me
Menu

Java Interface rules

Posted on 15/01/2010

Let’s start with a short Java question:

Bellow you can see the interface ‘Test’.
Which lines in that interface will be rejected by the compiler?



public interface Test{

	//1
	public static final int x1 = 3; 
	
	//2
	public static int x2 = 3; 

	//3
	static int x3 = 3; 
	
	//4
	int x4 = 3; 

	//5
	public int f5();

	//6
	int f6(); 
	
	//7
	public static int f7();
		
	//8
	private void f8();

	//9
	public final void f9();

	//10
	private static final int x5 = 3; 
}


The answer is:

lines: 7,8,9,10

I am sure that even many of the experienced java developers will not have a 100% success answering this question because it can be confusing.

1, 2, 3 and 4 are actually all the same – only constants are allowed and by default they are. For that reason, 10 is not allowed.
5, 6 are the same – only public and protected methods are allowed. By default they are public.

In short these are the rules for interfaces:
Member variables
Can be only public and are by default.
By default are static and always static
By default are final and always final

Methods
Can be only public and are by default.
Can NOT be static
Can Not be Final

5 thoughts on “Java Interface rules”

  1. Dave Cheney says:
    16/02/2010 at 16:39

    Are you sure that protected methods are permitted in Interface definitions ?

    Consider the following code

    public interface I {

    public void p1();

    protected void p2(); // won’t compile
    }

    Reply
  2. admin says:
    17/02/2010 at 09:06

    Hi Dave,
    You are definitely right.
    I have changed the article.
    Thanks very much for being alert.
    🙂

    Reply
  3. santhu says:
    02/11/2011 at 17:42

    HI you said you updated the article in a reply to “Dave Cheney”. But i can still see in the description saying public and protected methods are allowed in the interfaces. Can you please update the description to avoid confusion. If i am wrong please correct me.

    Thanks

    Reply
  4. Avi says:
    03/11/2011 at 21:58

    You are right, I have fixed it in one place but forgot the other place.
    Thank you also for being alert.
    🙂

    Reply
  5. Fabian says:
    22/10/2014 at 11:20

    The java doc says: “the interface body can contain abstract methods, default methods, and static methods.”
    So methods in interfaces can be static, but have to be followed by braces.

    Reply

Leave a Reply to santhu Cancel reply

Your email address will not be published. Required fields are marked *


About Me

REFCARD – Code Gems for Android Developers

Categories

  • Android
  • AWS
  • AWS EMR
  • bluetooth
  • Chrome extension
  • ClientSide
  • Clover
  • Coding Coventions
  • Data Lake
  • General
  • GreaseMonkey
  • Hacks
  • hibernate
  • hibernate validator
  • HTML5
  • HtmlUnit
  • Image Manipulation
  • Java
  • Java Technologies
  • JavaScript
  • Java_Mail
  • JEE/Network
  • Job searching
  • Open Source
  • Pivot
  • projects
  • Pure Java
  • software
  • Spark
  • Trivia
  • Web development

Archives

  • March 2022 (1)
  • January 2022 (1)
  • January 2021 (1)
  • December 2018 (1)
  • August 2018 (1)
  • October 2013 (1)
  • March 2013 (1)
  • January 2013 (2)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • December 2011 (1)
  • July 2011 (1)
  • June 2011 (1)
  • May 2011 (2)
  • January 2011 (1)
  • December 2010 (1)
  • November 2010 (3)
  • October 2010 (4)
  • July 2010 (1)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (2)
  • January 2010 (5)
  • December 2009 (10)
  • September 2009 (1)
 RSS Feed
e479022ca85569ce1314015afb7168ea-332
©2023 aviyehuda.com | Design: Newspaperly WordPress Theme