T.Giorgos
T.Giorgos

Reputation: 9

how can i use a superclass method in a subclass java

public void setName (String n){}
public void setAfm (String a){}

These are the Superclass methods i need to call.

SalariedEmployee (){
    name = super.setName(String n);
    afm = super.setAfm(String a);
    salary = payment();

And thats the constructor in the subclass. How can i call the methods properly. I don't want to use any parameters in SalariedEmployee, i want to set the name and afm with the superclass methods. But my methods are void. So i guess i have to change that right ? Or am I missing something else? Thanks in advance!

Upvotes: 1

Views: 559

Answers (2)

Lajos Arpad
Lajos Arpad

Reputation: 76508

There are two cases when you want to call a super method. The first case is that the method was not overriden by the subclass. If that is the case, you can call those methods simply by calling

setName("Dick Aceman"); 

or

setAtf("Acebook");

It is more descriptive if you call them like this:

this.setName("Dick Aceman");
this.setAtf("Acebook");

The bulletproof way to call them is this:

super.setName("Dick Aceman");
super.setAtf("Acebook");

This last one works even if the methods were overriden, but in general it is considered to be too descriptive, so this kind of method call should be used only when there is no alternative. Note, that since your methods are public, they are inherited by subclasses.

The problems with your try were that:

  • you tried to assign the return value of the methods to variables, when the methods do not return values
  • you declared the type at method call, which is invalid
  • you used the undefined variables of a and n

You should watch a few tutorial videos, you will get the basics then. After you watch such a video or two, you should return to this answer.

Upvotes: 0

Benoit Vanalderweireldt
Benoit Vanalderweireldt

Reputation: 2989

EDIT : You can also use setters. The "super" keyword is mandatory only if you want to call a method from the superclass that you have overridden in the subclass.

You should use constructors to set initial values but using setters is a possible solution too :

class Employee {
    String name;
    String afm;

    public Employee() {
    }
    public Employee(String name, String afm) {
        super();
        this.name = name;
        this.afm = afm;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAfm(String afm) {
        this.afm = afm;
    }

}

class SalariedEmployee extends Employee {
    //Using constructors 
    public SalariedEmployee(String name, String afm) {
        super(name, afm);
        salary = payment();
    }
    //using setters
    public SalariedEmployee() {
        setAfm("afm");
        setName("name");
        salary = payment();
    }
}

Also a setter method like 'setName' should be void because you don't expect it to return anything unlike a getter method like 'getName' for example.

Upvotes: 2

Related Questions