Java Programming - Language Fundamentals - Discussion

Discussion :: Language Fundamentals - General Questions (Q.No.10)

10. 

Which is the valid declarations within an interface definition?

[A]. public double methoda();
[B]. public final double methoda();
[C]. static void methoda(double d1);
[D]. protected void methoda(double d1);

Answer: Option A

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.


Anand said: (Nov 7, 2011)  
Please explain me. How it is?

Utpal said: (Jun 27, 2012)  
Is it possible that an interface can contain normal method ? please explain.

Venkat said: (Feb 9, 2013)  
@Utpal.

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

Laltu Banerjee said: (Aug 19, 2013)  
Why static method is not allowed in interface?

Vikas Grover said: (Sep 7, 2013)  
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: (Oct 30, 2013)  
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: (Dec 23, 2013)  
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: (Dec 25, 2013)  
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: (Oct 27, 2014)  
@Vikas grover.

Could you please elaborate your answer?

I.S. said: (Oct 11, 2015)  
Java 8 adds static methods to interfaces.

Ujjawal said: (Mar 23, 2016)  
I don't think it would hurt if we add abstract also like public. Just it is not required.

Shubham Garg said: (Jun 7, 2016)  
Option B is wrong because final means we can't inherit the method or without inheritance how can we give the definition.

Option C is wrong because static means class instance or class instance can't inherit its can't be protected.

Valenitne said: (Jun 21, 2017)  
A static method can be declared in an interface but it must have a body.

Yogesh said: (Sep 1, 2018)  
Since in Java 9, option C is right. We can declare and add behaviour to a static function in the interface.

Abhishek Podder said: (Jun 27, 2019)  
The private method is allowed in JDK 9.0 onwards but they cannot be used with abstract or default modifier.

Charan said: (Nov 4, 2019)  
Why can't we use protected?

Kathir said: (Jan 6, 2020)  
Why protect method is not declaration in interface?

Vivek Singh said: (Jun 13, 2020)  
@All.

By default interface definition has public abstract.

So even if we do not declare it inside the interface the compiler automatically convert it into public abstract.

Hence, public double methoda(); is correct answer.

public double methoda(); = public abstract double methoda(); = double methoda();

Even if we remove access modifier i.e public and abstract keyword compiler it automatically append public abstract for the above method for an interface if it is not containing at the time of compile.

Dhwanil said: (Jan 22, 2021)  
Based on JAVA 8, option 3 is correct.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.