Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force subclasses to set a variable in java?

I have a class which defines all of the basic parameters for a given screen. From here every screen in the application is a subclass of this. I need every screen (i.e. subclass) to set the value of a variable in its implementation (namely, each screen must define what level it is in a navigation tree).

Also, ideally, this variable should be final when it is set in the sub classes (I realise this probably isn't possible).

What is the best way to go about this? Is there a way to correctly enforce this type of behaviour in Java?

like image 333
btalb Avatar asked Feb 22 '13 02:02

btalb


People also ask

How do you declare a subclass in Java?

To create a subclass of another class use the extends clause in your class declaration. (The Class Declaration explains all of the components of a class declaration in detail.) As a subclass, your class inherits member variables and methods from its superclass.

Do subclasses automatically inherit methods?

them yourself. A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Do subclasses share variables?

A class variable exists only once in a class and its subclasses and is shared by the class and all of its subclasses. A class instance variable exists for a given class and each of its subclasses: thus its data is not shared.

How do you change the superclass variable in a subclass Java?

You can try to convert the super class variable to the sub class type by simply using the cast operator. But, first of all you need to create the super class reference using the sub class object and then, convert this (super) reference type to sub class type using the cast operator.


Video Answer


1 Answers

@pst's comment lead to this solution.

This can't be done with a variable. But an abstract class can require that a particular method is implemented: this method could return the applicable value

From declaring an abstract function to set or return the variable, you can force any subclass to implement it correctly.

Next, the function must be called by every single subclass of the outer class. This implies that it must be done somewhere in the outer class. This can be done in the no-argument constructor of the outer class without having to worry about subclasses calling super:

Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem. (Java docs: Super)

Based on that, this solution will hold up and correctly force the variable to be set as long as either:

  1. No other constructor is created in the superclass (hence super can't be used in a subclass to call a different constructor instead)
  2. All other constructors in the superclass still call the default constructor internally

The code:

Superclass:

public abstract class SuperClass {     // Variable all inner classes must set     static int myVar = 0;      public SuperClass() {         myVar = giveValue();     }      public abstract int giveValue(); } 

Subclass:

public class SubClass extends SuperClass {      @Override     public int giveValue() {         return 5; // individual value for the subclass     }  } 
like image 163
btalb Avatar answered Sep 22 '22 14:09

btalb