Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - General Questions (Q.No. 10)
10.
Which is the valid declarations within an interface definition?
public double methoda();
public final double methoda();
static void methoda(double d1);
protected void methoda(double d1);
Answer: Option
Explanation:

Option A is correct. A public access modifier is acceptable. The method prototypes in an interface are all abstract by virtue of their declaration, and should not be declared abstract.

Option B is wrong. The final modifier means that this method cannot be constructed in a subclass. A final method cannot be abstract.

Option C is wrong. static is concerned with the class and not an instance.

Option D is wrong. protected is not permitted when declaring a method of an interface. See information below.

Member declarations in an interface disallow the use of some declaration modifiers; you cannot use transient, volatile, or synchronized in a member declaration in an interface. Also, you may not use the private and protected specifiers when declaring members of an interface.

Discussion:
20 comments Page 1 of 2.

Anand said:   1 decade ago
Please explain me. How it is?

Utpal said:   1 decade ago
Is it possible that an interface can contain normal method ? please explain.

Venkat said:   1 decade ago
@Utpal.

Interface may contain declaration of method but, not its implementation. That method have to be necessarily implemented in subclass.

Laltu banerjee said:   1 decade ago
Why static method is not allowed in interface?

Vikas grover said:   1 decade ago
Because static method are only concerned with name of the class itself. Means they are accessible through the name of the class. So that is why method in interfaces can not be static. Because which class implements that interface has to create object of that class and this is not in case of static.

Bishal said:   1 decade ago
As it is clear from the option only a can be use to declare d interface.

b & c can't be because of final & static keyword respectively as they use in case of class but in interface.

Gourav said:   1 decade ago
I still not understand why final and static options are not right as we always said methods of interface should public static and final then why these options: option B and option C are wrong.

Anki said:   1 decade ago
No its not about methods, this line is for variables or can say data members, they must be declared public static and final.

Mantu said:   1 decade ago
@Vikas grover.

Could you please elaborate your answer?

I.S. said:   10 years ago
Java 8 adds static methods to interfaces.


Post your comments here:

Your comments will be displayed after verification.