Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - General Questions (Q.No. 7)
7.
public interface Foo 
{ 
    int k = 4; /* Line 3 */
}
Which three piece of codes are equivalent to line 3?
  1. final int k = 4;
  2. public int k = 4;
  3. static int k = 4;
  4. abstract int k = 4;
  5. volatile int k = 4;
  6. protected int k = 4;
1, 2 and 3
2, 3 and 4
3, 4 and 5
4, 5 and 6
Answer: Option
Explanation:

(1), (2) and (3) are correct. Interfaces can have constants, which are always implicitly public, static, and final. Interface constant declarations of public, static, and final are optional in any combination.

Discussion:
31 comments Page 2 of 4.

Amit said:   1 decade ago
All the methods in interface are by default public and static. And the variables are public static final.

Raima said:   6 years ago
Are all the variables declared inside an interface by-defualt final static and public or not?

Jithin said:   1 decade ago
This why because if int k=4 in an interface implicitly means
public static final int k=4 ;

Sauravkumararya said:   1 decade ago
Abstract keyword will be not apply for variable. It is applicable only for methods, class.

Bat said:   1 decade ago
Interfaces can have constants, constants in java are public, static, final.

Mouli said:   4 years ago
But for static, we must use static keyword then the answer should be D.

Trupti said:   1 decade ago
By default in interface all the members are public, static and final.

Avi_7v said:   1 decade ago
A method which doesn't have the body is called as abstract method.

Mamata nayak said:   4 years ago
Can anyone please help to know about foo used in this programme?
(1)

Roma said:   7 years ago
No, an interface only consists of public and abstract modifiers.


Post your comments here:

Your comments will be displayed after verification.