Reputation: 145
I have a superclass and two subclasses that extend it. I'd like to initialize a variable called radius which can be set freely in subclass A, but is always the same for subclass B. I could initialize the variable every time I create an object B, but I was wondering if it's possible to make the variable final and static in subclass B, while still being able to implement a getter in my superclass. I might implement more subclasses which have a radius that's either final or variable. If not I'm wondering what would be the most elegant solution for my problem. Here is some sample code which works, but isn't very great.
abstract class SuperClass {
public double getRadius() {
return this.radius;
}
protected double radius;
}
class A extends SuperClass{
public void setRadius(double radius) { // Would like to put this setter in SuperClass for future subclasses.
this.radius = radius;
}
}
class B extends SuperClass{
public B() {
radius = 0.2; //Want to make this static and final only for this subclass.
}
}
Upvotes: 2
Views: 88
Reputation: 2756
Another approach would be to have the base class in its own package, with a protected setter:
package sandbox.radius.base;
public class Radius {
private double radius;
public double getRadius() {
return radius;
}
protected void setRadius(double radius) {
this.radius = radius;
}
}
Then implementations in a different package, exposes the setter only where appropriate, delegating to super:
package sandbox.radius;
import sandbox.radius.base.Radius;
public class FixedRadius extends Radius {
public FixedRadius() {
setRadius(0.2);
}
}
and
package sandbox.radius;
import sandbox.radius.base.Radius;
public class MutableRadius extends Radius {
public void setRadius(double radius) {
super.setRadius(radius);
}
}
So the API is cleaner:
public class RadiusApp {
public static void main(String[] args) {
FixedRadius fr = new FixedRadius();
fr.getRadius();
// fr.setRadius(1.0); //compile error
MutableRadius mr = new MutableRadius();
mr.getRadius();
mr.setRadius(1.0);
}
}
Upvotes: 1
Reputation: 508
Why dont you try this:
Make the Variable private in the superclass. Have a getter and setter method in the superclass.
Override the setter method in the subclass so that is is not possible to change the variable.
Like that:
abstract class SuperClass {
private double radius;
public double getRadius() {
return this.radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
class A extends SuperClass{
}
class B extends SuperClass{
public B() {
super.setRadius(0.2d);
}
@Override
public void setRadius(double radius) {
throw new UnsupportedOperationException("My Info Text");
}
}
Upvotes: 1
Reputation: 131326
Making a variable final and static in one subclass, while keeping it variable in others in Java
Technically, you cannot do it.
A field is either a static field or an instance field, not both.
As alternative, override getRadius()
in the B
class where you want to provide a different value :
@Override
public double getRadius() {
return 0.2;
}
And to make this constant respected, you should also override the setter in this B
class:
@Override
public void setRadius(double radius) {
throw new UnsupportedOperationException();
}
Upvotes: 2