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 1 of 4.

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.
(2)

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.
(1)

Dileep Kumar M B said:   1 decade ago
Static means the variable is not belonging to any specific class. It can be accessible without creating an object. (can also be accessed along with the class name). And static methods cannot be overridden by non-static methods.

Non-static variables cannot be accessed in static methods (context).

A C Department said:   1 decade ago
"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 ".

Who can get it instantly? Please give examples w/description, not only theories.

Nikhil said:   1 decade ago
Why public, static, final is only used in interface method?
>> Because if you try to define other access modifier then you will get the compile time exception.

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?

Sandeep said:   1 decade ago
Interface contains only abstract methods in it so no need of again declaring as abstract. And protected used for inheritance relationship.

Avinash said:   1 decade ago
Interface Contains only abstract methods. Then it has to be declared as abstract. Therefore, we might even get option 4.

Amit said:   1 decade ago
Any variables you write in interface like (int i=30;).

Java compiler trite it as (public static final int i=30;).

Reshma Wadhavane said:   9 years ago
An interface is just like a class but difference is that interface contains only final & static variable.


Post your comments here:

Your comments will be displayed after verification.