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?
- final int k = 4;
- public int k = 4;
- static int k = 4;
- abstract int k = 4;
- volatile int k = 4;
- protected int k = 4;
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 1 of 4.
Aman Kumar said:
4 years ago
Why not be volatile and protected? Please explain.
(1)
Mouli said:
4 years ago
But for static, we must use static keyword then the answer should be D.
Mamata nayak said:
4 years ago
Can anyone please help to know about foo used in this programme?
(1)
Shubham said:
6 years ago
The interface doesn't contain public datatype it always contains static and final data then why here a public data type is also included in and?
Raima said:
6 years ago
Are all the variables declared inside an interface by-defualt final static and public or not?
Rani Mittal said:
7 years ago
The abstract is a modifier which can be used only with class and methods. we can't use an abstract modifier with a variable.
Abstract methods doesn't have the body they only have declarations. Abstract classes are the classes for which we can't create the object.
REASON FOR BEING final
Any implementations can change the value of fields if they are not defined as final. Then they would become a part of the implementation. An interface is a pure specification without any implementation.
REASON FOR BEING static
If they are static, then they belong to the interface, and not the object, nor the run-time type of the object.
Reason for being public -
If they are public they can be accessed from anywhere because some programers put all the interfaces in a different package. And to access something from a different package that thing should be public.
Abstract methods doesn't have the body they only have declarations. Abstract classes are the classes for which we can't create the object.
REASON FOR BEING final
Any implementations can change the value of fields if they are not defined as final. Then they would become a part of the implementation. An interface is a pure specification without any implementation.
REASON FOR BEING static
If they are static, then they belong to the interface, and not the object, nor the run-time type of the object.
Reason for being public -
If they are public they can be accessed from anywhere because some programers put all the interfaces in a different package. And to access something from a different package that thing should be public.
(2)
Roma said:
7 years ago
No, an interface only consists of public and abstract modifiers.
Reshma Wadhavane said:
9 years ago
Interface contains only static and final variable.
Reshma Wadhavane said:
9 years ago
An interface is just like a class but difference is that interface contains only final & static variable.
Shubham said:
9 years ago
Interface have an abstract method not variable.
We can not instantiate any object of the interface so var is static we can't make setter, getter or constructor (all methods are abstract) so can't change the value of the variable so its final or we want to use the variable outside the interface so its public.
We can not instantiate any object of the interface so var is static we can't make setter, getter or constructor (all methods are abstract) so can't change the value of the variable so its final or we want to use the variable outside the interface so its public.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers