Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not abstract fields?

Why can't Java classes have abstract fields like they can with abstract methods?

For example: I have two classes that extend the same abstract base class. These two classes each have a method that is identical except for a String constant, which happens to be an error message, within them. If fields could be abstract, I could make this constant abstract and pull the method up into the base class. Instead, I have to create an abstract method, called getErrMsg() in this case, that returns the String, override this method in the two derived classes, and then I can pull up the method (which now calls the abstract method).

Why couldn't I just make the field abstract to begin with? Could Java have been designed to allow this?

like image 756
Paul Reiners Avatar asked Feb 05 '10 22:02

Paul Reiners


People also ask

Should abstract classes have fields?

Class MembersAn abstract class may have static fields and static methods. You can use these static members with a class reference (for example, AbstractClass. staticMethod() ) as you would with any other class.

Why variables Cannot be abstract?

Abstract implies that the abstract methods within abstract classes must be coded. A variable cannot be coded because it is a primitive or reference that can only be assigned a value.

Why we Cannot use abstract and static?

A static method belongs to class not to object instance thus it cannot be overridden or implemented in a child class. So there is no use of making a static method as abstract.

Can a data field be abstract?

An abstract class can be used as a data type. A data field can be declared abstract.


2 Answers

You can do what you described by having a final field in your abstract class that is initialised in its constructor (untested code):

abstract class Base {      final String errMsg;      Base(String msg) {         errMsg = msg;     }      abstract String doSomething(); }  class Sub extends Base {      Sub() {         super("Sub message");     }      String doSomething() {          return errMsg + " from something";     } } 

If your child class "forgets" to initialise the final through the super constructor the compiler will give a warning an error, just like when an abstract method is not implemented.

like image 157
rsp Avatar answered Oct 22 '22 20:10

rsp


I see no point in that. You can move the function to the abstract class and just override some protected field. I don't know if this works with constants but the effect is the same:

public abstract class Abstract {     protected String errorMsg = "";      public String getErrMsg() {         return this.errorMsg;     } }  public class Foo extends Abstract {     public Foo() {        this.errorMsg = "Foo";     }  }  public class Bar extends Abstract {     public Bar() {        this.errorMsg = "Bar";     } } 

So your point is that you want to enforce the implementation/overriding/whatever of errorMsg in the subclasses? I thought you just wanted to have the method in the base class and didn't know how to deal with the field then.

like image 26
Felix Kling Avatar answered Oct 22 '22 21:10

Felix Kling