Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't an interface have fields?

Tags:

java

interface

Ignore everything up until the second edit

I am trying to do something like this:

public interface IModifier{
   public String nameTag;
   public void foo();
}

My reason for trying to do this is this: I have a class SpecificModifier implements IModifier and there are many very similar class that also implement IModifier. I want every class that implements IModifier to have a public String nameTag.

Edit: I have gotten confirmation that I cannot do this, but can someone explain WHY an interface cannot require a field?

Edit two:

My understand of the purpose of abstract classes vs interfaces. An interface is used purely to declare necessary parts of whatever implements it, so that all the objects have common parts that can be referenced. While an abstract class is used to provide common functionality to multiple classes.

That is a little bit of an over simplification but regardless, I still see no reason, other than an oversight by the language designers, that an interface cannot have an abstract field.

Can anyone provide a reason why?

like image 766
UberAffe Avatar asked Aug 26 '15 00:08

UberAffe


People also ask

Can you have fields in an interface?

An interface can't contain instance fields, instance constructors, or finalizers. Interface members are public by default, and you can explicitly specify accessibility modifiers, such as public , protected , internal , private , protected internal , or private protected .

Can a Java interface have fields?

A Java interface is a bit like a Java class, except a Java interface can only contain method signatures and fields. A Java interface is not intended to contain implementations of the methods, only the signature (name, parameters and exceptions) of the method.

Why interface does not have fields in C#?

By default all the members of Interface are public and abstract. The interface will always defined with the help of keyword 'interface'. Interface cannot contain fields because they represent a particular implementation of data. Multiple inheritance is possible with the help of Interfaces but not with classes.

Can C# interface have fields?

In C#, an interface can be defined using the interface keyword. An interface can contain declarations of methods, properties, indexers, and events. However, it cannot contain fields, auto-implemented properties.

What is the difference between an interface and a field?

Fields are an implementation detail, as fields do not describe how a class should "act as." For instance, Interfaces are commonly used as a declared type and a concrete implementation is used as an actual type. Consider the java.util.Map Interface for a moment. It describes how a Map should act, via its set of methods.

What is the use of interface in a class?

A class that implements interface must implements all the methods in interface. All the methods are public and abstract. And all the fields are public, static, and final. It is used to achieve multiple inheritance. It is used to achieve loose coupling.

Can an interface extend another interface in Java?

An interface can extends another interface or interfaces (more than one interface) . A class that implements interface must implements all the methods in interface. All the methods are public and abstract. And all the fields are public, static, and final.

Why interface variables are static in a class?

A class that implements an interface adheres to the protocol defined by that interface. Interface variables are static because java interfaces cannot be instantiated on their own. The value of the variable must be assigned in a static context in which no instance exists.


3 Answers

An Interface specifies a contract, which a concrete class that implements the Interface must adhere to. This contract describes how an implementation should act. In the Interface specification, there should be clear comments that describe what the purpose of each method is. The use of an Interface decouples the contract from the actual implementation. Fields are an implementation detail, as fields do not describe how a class should "act as."

For instance, Interfaces are commonly used as a declared type and a concrete implementation is used as an actual type.

    Map<Key,Value> m = new HashMap<>();

Consider the java.util.Map Interface for a moment. It describes how a Map should act, via its set of methods. There are several different implementations of the Map interface that allow users to choose the correct implementation for their needs.

Specifying that a field must be used by several sub classes implies that there is some semblance of a class hierarchy. In this case an abstract class could do the trick.

   abstract class IModParent implements IModifier{
      protected String nameTag;
   }

Now you can have a concrete class.

   class SpecificModifier extends IModParent{

      SpecificModifier(String nameTag){ this.nameTag = nameTag; }

      @Override
      public void foo(){ System.out.println(nameTag); }
   }

And a declaration.

    IModifier imod = new SpecificModifier("MyName");

This gives you the flexibility of using an Interface type while still being able to share implementation details via a non-instantiable abstract class across the group of concrete classes that you want.

like image 59
bombe Avatar answered Nov 08 '22 08:11

bombe


No, you can't sadly. Interfaces in java can only contain methods and constants. But, there is an alternative to this. Add a method like this:

String getNameTag();

See? that way, the implementations must contain a nameTag field, or they can just do some other stuff to return a string.

Also, as far as I know, you don't need to add access modifiers to interface methods.

like image 42
Sweeper Avatar answered Nov 08 '22 08:11

Sweeper


Interface is made for methods and constants.

You need to use abstract class as per your requirements.

public abstract class IModifier{
   public String nameTag;
   public abstract void foo();
}

Now answer to your question WHY an interface cannot require a field? Ans: Because that is the feature of abstract class. It would be almost no difference between interface and abstract class.

I hope that I answered your tricky question.

like image 38
seahawk Avatar answered Nov 08 '22 10:11

seahawk