Tamanna Iruu
Tamanna Iruu

Reputation: 301

Can we use private or protected member variables in an interface?

Is it possible to define an interface like this:

public interface Test{
   public string name1;
   private String email;
   protected pass;
}

Upvotes: 8

Views: 17694

Answers (2)

Dani
Dani

Reputation: 181

being java

only public, static & final are permitted

You should use an abstract class instead of an interface if you want to declare its fields.

Upvotes: 4

Bill Gregg
Bill Gregg

Reputation: 7147

Having private or protected members in an interface doesn't make sense conceptually. By definition something that is "private" isn't exposed and therefore, anyone consuming the interface would not care if the implementing class had a private email field or not.

Only public members matter to the code consuming the interface.

Upvotes: 5

Related Questions