Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Abstract class to contain variables?

Is it good practice to let abstract classes define instance variables?

public abstract class ExternalScript extends Script {      String source;      public abstract void setSource(String file);      public abstract String getSource(); } 

The sub class, ExternalJavaScript.class, would then automatically get the source variable but I feel it's easier to read the code if all the sub classes themselves define the source, instead of from inheritance.

What is your advice?

/Adam

like image 483
Adam Asham Avatar asked Oct 25 '08 10:10

Adam Asham


People also ask

Can Java abstract class have variables?

Abstract classes can have instance variables (these are inherited by child classes). Interfaces can't. Finally, a concrete class can only extend one class (abstract or otherwise).

Can abstract class hold variables?

An abstract class may contain non-final variables. Type of variables: Abstract class can have final, non-final, static and non-static variables. The interface has only static and final variables. Implementation: Abstract class can provide the implementation of the interface.

Can we initialize variable in abstract class?

We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used.

Can abstract class have properties 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.


2 Answers

I would have thought that something like this would be much better, since you're adding a variable, so why not restrict access and make it cleaner? Your getter/setters should do what they say on the tin.

public abstract class ExternalScript extends Script {      private String source;      public void setSource(String file) {         source = file;     }      public String getSource() {         return source;     } } 

Bringing this back to the question, do you ever bother looking at where the getter/setter code is when reading it? If they all do getting and setting then you don't need to worry about what the function 'does' when reading the code. There are a few other reasons to think about too:

  • If source was protected (so accessible by subclasses) then code gets messy: who's changing the variables? When it's an object it then becomes hard when you need to refactor, whereas a method tends to make this step easier.
  • If your getter/setter methods aren't getting and setting, then describe them as something else.

Always think whether your class is really a different thing or not, and that should help decide whether you need anything more.

like image 102
Egwor Avatar answered Sep 21 '22 15:09

Egwor


Sure.. Why not?
Abstract base classes are just a convenience to house behavior and data common to 2 or more classes in a single place for efficiency of storage and maintenance. Its an implementation detail.
Take care however that you are not using an abstract base class where you should be using an interface. Refer to Interface vs Base class

like image 41
Gishu Avatar answered Sep 21 '22 15:09

Gishu