Java Programming - Declarations and Access Control - Discussion


Which three are valid method signatures in an interface?

  1. private int getArea();
  2. public float getVol(float x);
  3. public void main(String [] args);
  4. public static void main(String [] args);
  5. boolean setFlag(Boolean [] test);

[A]. 1 and 2
[B]. 2, 3 and 5
[C]. 3, 4, and 5
[D]. 2 and 4

Answer: Option B


(2), (3), and (5). These are all valid interface method signatures.

(1), is incorrect because an interface method must be public; if it is not explicitly declared public it will be made public implicitly. (4) is incorrect because interface methods cannot be static.

Deepak said: (Jul 7, 2013)  
What a difficult language is this java?

Ashvin said: (Aug 15, 2013)  
Can any one tell that do we generally put psvm in an interface?

Adithya said: (Nov 6, 2013)  

Let me know if we can declare main method in an interface ?

Mohammed Refeekh said: (Jun 14, 2014)  
Can have a interface method protected or default?

Amaziane said: (Jun 28, 2014)  
We can't have a protected interface in java, but default yes, and default means implicitly public access.

Amaziane said: (Jun 28, 2014)  
Main method in this example doesn't mean the main method of class, it means just a simply and normally method with name main because main isn't a reserved keywords.

TastyBean said: (Sep 28, 2015)  
I thought we can use static methods in interface in java 8 now, you need to update this answer if I am right.

Qwerty said: (Apr 13, 2016)  
Yes, you are right @Tastybean. We can use the static methods in interfaces from Java 8.

Apurva said: (Dec 3, 2016)  
interface Hello
static void fun()
System.out.println("This is static fun method in interface");
public class Check implements Hello
public static void main(String[]args)
//we can declare static method in java 8 you need to update your answer

Ravi Mourya said: (Jan 23, 2017)  
An interface can have a static method, but with a body.

Suganthisakthivel said: (Aug 8, 2017)  
Why is the Boolean type method correct? I think the interface methods all have a public type but the 5 have Boolean, I want a full explanation.

Anonymous said: (May 30, 2018)  

Boolean is the return type of the method, not the access specifier.

The public is an access specifier and members of the interface should be public. If access specifier is not mentioned explicitly, it is considered as public here.

In the 5th method, no access specifier is mentioned explicitly, and since it is a method declared in an interface, the access specifier will default to 'public'.

Saif Ali said: (Jul 13, 2018)  
Yes, you can use public static void main(string ar[])
in the interface but it only works as a starting point of that particular interface.
Not as a class psvm both are different.

Harshal said: (Aug 9, 2018)  
With java 9 even private methods are allowed in interface.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.