shayyy7
shayyy7

Reputation: 93

How to force a subclass to use a superclass variable?

I am using java and I would like to force all of my subclasses to use the variables of the superclass. The superclass is abstract and the subclasses implement all of its methods but not the variables. Is there any way I can force the subclasses to use also the variables of the superclass?

thanks

Upvotes: 2

Views: 1965

Answers (5)

thebeast
thebeast

Reputation: 108

Like someone said here you can use the template design pattern which will ensure that steps of an algorithm is given in a method so this way you can have the variable used in a method that can be used in a sub class.

The other approach would be to set the access modifier as "protected" which ensures that the subclasses that inherits from the super class can use the variables in the super class

Upvotes: 0

Ritesh Shakya
Ritesh Shakya

Reputation: 585

Just make the field protected, meaning that it should be visible to all derived classes and can use them in subclasses. If that is what you are tying to imply by saying force.

Upvotes: 0

user140547
user140547

Reputation: 8200

You cannot "force" the subclass to use your variables. But maybe you are looking for the Template Pattern. This way, you can define a template method in the abstract superclass which defines the structure and the general working of your class. It calls the hook methods which are abstract and have to be provided by implementing classes. You can make it final to prevent subclasses from overriding the template method. Then the subclasses have to implement the abstract hook methods provided, but cannot override the template method.

Upvotes: 2

Archie Azares
Archie Azares

Reputation: 135

Simple. All you need is to let the subclass inherit the superclass through inheritance. this would allow your subclass implement functions of your superclass.

For a more detailed reference kindly visit this link: http://docstore.mik.ua/orelly/java-ent/jnut/ch03_04.htm

Upvotes: 0

Radu Ionescu
Radu Ionescu

Reputation: 3532

The keyword super is most of the times used in conjunction to accessing superclass methods, most of the times a constructor from the parent class .

However, you can also use super to access fields of the superclass.

Here is an example of the usage for accessing fields.

public class Base {

    public int a = 1;
    protected int b = 2;
    private int c = 3;

    public Base(){

    }
}

public class Extended extends Base{

    public int a = 4;
    protected int b = 5;
    private int c = 6;

    public Extended(){

    }

    public void print(){
        //Fields from the superclass
        System.out.println(super.a);
        System.out.println(super.b);
        System.out.println(super.c); // not possible

    }
}

public static void main(String[] args) {
        Extended ext = new Extended();
        ext.print();
    }

You can always rename the fields in your subclasses not to conflict, but if you want to distinguish a method parameter or local variable from the superclass field, use super as you would use this

Upvotes: 1

Related Questions