Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any abstract variables in java?

Can variables be abstract in Java? Do constructor support abstract variables? I am not sure but I think the constructor supports static variables. Please clarify my doubt.

like image 884
Vinoth Kumar Avatar asked Aug 25 '10 04:08

Vinoth Kumar


People also ask

Can there be abstract variables?

No you cannot. When you declare a variable you declare an instance of the class/struct you specified. Abstract classes cannot be directly instantiated.

Why variable is not abstract in java?

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.

Can you have abstract attributes java?

Java doesn't support abstract properties, if you try to mark a class property as abstract, you get a compilation error. In this tutorial, we introduce 2 ways for defining abstract properties which are set by subclasses without using the abstract keyword.


1 Answers

In java only classes and methods can be abstract. Variable declarations cannot. However you can have variable declarations whose types are abstract. See example:

public abstract class MyClass { // allowed
   public abstract myMethod(); // allowed
   public MyClass instance; // allowed

   public abstract MyClass instance; // NOT ALLOWED!!
}
like image 63
naikus Avatar answered Oct 26 '22 22:10

naikus