imacake
imacake

Reputation: 1713

Final variables in abstract classes

In Java, I can't create instances of abstract classes. So why doesn't eclipse scream about the following code?

public abstract class FooType {
    private final int myvar;

    public FooType() {
        myvar = 1;
    }
}

Upvotes: 15

Views: 19043

Answers (6)

die
die

Reputation: 67

You definitely can declare final variable in abstract class as long as you assign value to it either in the constructor or in declaration. The example that guy gave makes no sense.

Upvotes: 0

chamzz.dot
chamzz.dot

Reputation: 775

No you can't declare final variables inside an Abstract class. Check Below example.

public abstract class AbstractEx {
    final int x=10;
    public abstract void AbstractEx();
}

public class newClass extends AbstractEx{

    public void AbstractEx(){
        System.out.println("abc");
        }
    }  

public class declareClass{
   public static void main(String[] args) {
       AbstractEx obj = new newClass ();
       obj.AbstractEx();
      // System.out.println(x);
    }
}

This code runs correct and produce output as

abc

But if we remove comment symbol of

System.out.println(x);

it will produce error.

Upvotes: -1

aps
aps

Reputation: 2472

Ok. See, an abstract class can have a constructor. It's always there-implicit or explicit. In fact when you create an object of a subclass of an abstract class, the first thing that the constructor of the subclass does is call the constructor of its abstract superclass by using super(). It is just understood, that's why you don't have to write super() explicitly unless you use parameterized constructors. Every class even if it is abstract, has an implicit constructor which you cannot see. It is called unless you create some constructor of your own. so long you created abstract classes without creating any custom constructor in it, so you didn't know about the existence of the implicit constructor.

Upvotes: 0

Hendrik Brummermann
Hendrik Brummermann

Reputation: 8312

The code is fine, the final variable is initialized in the constructor of FooType.

You cannot instantiate FooType because of it being abstract. But if you create a non abstract subclass of FooType, the constructor will be called.

If you do not have an explicit call to super(...) in a constructor, the Java Compiler will add it automatically. Therefore it is ensured that a constructor of every class in the inheritance chain is called.

Upvotes: 17

yankee
yankee

Reputation: 40750

You can have constructors, methods, properties, everything in abstract classes that you can have in non-abstract classes as well. You just can't instantiate the class. So there is nothing wrong with this code.

In a deriving class you can call the constructor and set the final property:

public class Foo extends FooType
{
  public Foo()
  {
    super(); // <-- Call constructor of FooType
  }
}

if you don't specify a call to super(), it will be inserted anyway by the compiler.

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533472

You can create concrete sub-classes of FooType and they will all have a final field called myvar.

BTW: A public constructor in an abstract class is the same as a protected one as it can only be called from a sub-class.

What is your doubt?

Upvotes: 0

Related Questions