0

Can you declare an abstract variable type in an abstract class? I am receiving an error when I put this line of code in. I can declare a variable that is final and not final but I am not sure if I should be able to declare a variable that is abstract. What would be the real advantage between an interface and an abstract class?

  Error Code:

       abstract int myScore = 100; <-- Causes an error

Code:

public abstract class GraphicObject {
       int home = 100;
       String myString = "";
       final int score = 0;

       abstract void draw();
       abstract void meMethod1();
       abstract void meMethod2();

       int meMethod3() {
           return 0;
       }
    }
2
  • 4
    What does an abstract field mean to you? What does an abstract method mean to you? Commented Feb 16, 2014 at 0:37
  • What is it you want to reach? Commented Feb 16, 2014 at 0:39

6 Answers 6

2

"Can you declare an abstract variable type in an abstract class?"

No, according to the JLS (http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1).

Sign up to request clarification or add additional context in comments.

Comments

2

Why should it be abstract if you can't implement variables? Abstract methods only mean that they must be implemented further in your code (by a class that extends that abstract class). For variable that won't make any since they keep being the same type. myscore will always be an int.

You may be tinking about override the value of myscore by the class that extends that abstract class.

Comments

2

An abstract method is a method that doesn't have a body. This is because it's meant to be overridden in all concrete (non-abstract) subclasses and, thanks to polymorphism, the abstract stub can never be invoked.

Given the above, and since there is no polymorphism for fields (or a way to override fields at all), an abstract field would be meaningless.

If what you want to do is have a field whose default value is different for every subclass, then you can assign its default value in the constructor(s) of each class. You don't need to make it abstract in order to do this.

8 Comments

I did notice that you can create a variable of an abstract class. myAbstractClass myAbstract = null; What would be the exact purpose of doing this. I can access the variables and methods but I what would be the need to call an empty method such as meMethod1().
You can create a variable of an abstract class, but not an object! And if you try to invoke any method from a null reference, you'll get a NullPointerException anyway.
The purpose of declaring a variable of an abstract class is that you can store in it any object of a subclass - as long as the subclass isn't abstract and therefore you have an actual object of it.
You can create it but You cant instanciate it. Imagine you have two classes dog and cat. Both are animals. And You want to force both the cat and the dog to talk() (a function). imagine that You want to create other animals in the future.
So You haver the abstract class Animal with the abstract method talk() so that all animals can talk. But this abstract class dont really knows how the dog or the cat talk.
|
2

No, abstract is used so that methods can only be implemented in subclasses. See http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

Comments

1

You cannot declare an abstract variable in Java.

If you wish to declare a variable in a super-class, which must be set by its sub-classes, you can define an abstract method to set that value...

For example:

public abstract class Foo {

    Object obj;

    public Foo() {
        init();
    }

    protected void init() {
        obj = getObjInitVal();
    }

    abstract protected Object getObjInitVal();

}


public class Bar extends Foo {

    @Override
    protected Object getObjInitVal() {
        return new Object();
    }

}

Comments

0

Base on "AlonL" reply :

You can do this too

public abstract class Foo {
    Object obj;
    public Foo() {
        init();
    }
    abstract protected void init();
}

public class Bar extends Foo {
    @Override
    protected void init() {
        obj = new Object();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.