Java Programming - Declarations and Access Control - Discussion
Discussion Forum : Declarations and Access Control - General Questions (Q.No. 9)
9.
Which three are valid method signatures in an interface?
- private int getArea();
- public float getVol(float x);
- public void main(String [] args);
- public static void main(String [] args);
- boolean setFlag(Boolean [] test);
Answer: Option
Explanation:
(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.
Discussion:
14 comments Page 1 of 2.
Harshal said:
7 years ago
With java 9 even private methods are allowed in interface.
(2)
Saif Ali said:
7 years ago
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.
in the interface but it only works as a starting point of that particular interface.
Not as a class psvm both are different.
(1)
Anonymous said:
7 years ago
@Suganthisakthivel.
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'.
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'.
Suganthisakthivel said:
8 years ago
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.
(1)
Ravi Mourya said:
9 years ago
An interface can have a static method, but with a body.
Apurva said:
9 years ago
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)
{
Hello.fun();
}
}
//we can declare static method in java 8 you need to update your answer
{
static void fun()
{
System.out.println("This is static fun method in interface");
}
}
public class Check implements Hello
{
public static void main(String[]args)
{
Hello.fun();
}
}
//we can declare static method in java 8 you need to update your answer
Qwerty said:
9 years ago
Yes, you are right @Tastybean. We can use the static methods in interfaces from Java 8.
TastyBean said:
10 years ago
I thought we can use static methods in interface in java 8 now, you need to update this answer if I am right.
(1)
Amaziane said:
1 decade ago
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.
Amaziane said:
1 decade ago
We can't have a protected interface in java, but default yes, and default means implicitly public access.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers