0

Here is my base class:

@Service
public class BasicUserManagerService implements UserManager {
    @Autowired
    private UserRepository UserRepository;

    private Logger logger = LoggerFactory.getLogger(UserManagerPasswordController.class);

    @Override
    public void createUser(User User) {
        if (UserRepository.findByEmail(User.getEmail()) != null)
            throw new InvalidDataException("User already registered with this email address");
        UserRepository.save(User);
        logger.info("Created user: {}", User.getEmail());
    }
}

I am trying to extend this class as follows:

@Service
public class UserManagerService extends BasicUserManagerService implements UserManager {
    @Override
    public void createUser(User User) {
        super().createUser(User);
    }
}

But I keep getting the error that the call to super() must be the first statement in the constructor body. As you can see I don't have a constructor and even when I add it, I get the same error. Why is this happening and how can I avoid it?

conquester
  • 1,082
  • 2
  • 21
  • 44

3 Answers3

5

Change this super usage. super() is parent's constructor. super is a reference to the parent class.

@Service
public class UserManagerService extends BasicUserManagerService implements UserManager {
    @Override
    public void createUser(ProxyCircuitUser proxyCircuitUser) {
        super.createUser(proxyCircuitUser);
    }
}
drowny
  • 2,067
  • 11
  • 19
3

super() is a call to the parent class constructor; which is not at all what you want.

Instead, you want to call the parent class implementation of the createUser method. The code for that is: super.createUser(user)

DwB
  • 37,124
  • 11
  • 56
  • 82
1

Here are various uses of Super Keyword in Java:

  1. Use of super with variables

This scenario occurs when a derived class and base class has same data members. In that case there is a possibility of ambiguity for the JVM.

/* Base class vehicle */
class Vehicle 
{ 
    int maxSpeed = 120; 
} 

/* sub class Car extending vehicle */
class Car extends Vehicle 
{ 
    int maxSpeed = 180; 

    void display() 
    { 
        /* print maxSpeed of base class (vehicle) */
        System.out.println("Maximum Speed: " + super.maxSpeed); 
    } 
} 

/* Driver program to test */
class Test 
{ 
    public static void main(String[] args) 
    { 
        Car small = new Car(); 
        small.display(); 
    } 
} 

Output:

Maximum Speed: 120
  1. Use of super with methods

This is used when we want to call parent class method. So whenever a parent and child class have same named methods then to resolve ambiguity we use super keyword.

/* Base class Person */
class Person 
{ 
    void message() 
    { 
        System.out.println("This is person class"); 
    } 
} 

/* Subclass Student */
class Student extends Person 
{ 
    void message() 
    { 
        System.out.println("This is student class"); 
    } 

    // Note that display() is only in Student class 
    void display() 
    { 
        // will invoke or call current class message() method 
        message(); 

        // will invoke or call parent class message() method 
        super.message(); 
    } 
} 

/* Driver program to test */
class Test 
{ 
    public static void main(String args[]) 
    { 
        Student s = new Student(); 

        // calling display() of Student 
        s.display(); 
    } 
} 

Output:

This is student class
This is person class
  1. Use of super with constructors

super keyword can also be used to access the parent class constructor. One more important thing is that, ‘’super’ can call both parametric as well as non parametric constructors depending upon the situation.

/* superclass Person */
class Person 
{ 
    Person() 
    { 
        System.out.println("Person class Constructor"); 
    } 
} 

/* subclass Student extending the Person class */
class Student extends Person 
{ 
    Student() 
    { 
        // invoke or call parent class constructor 
        super(); 

        System.out.println("Student class Constructor"); 
    } 
} 

/* Driver program to test*/
class Test 
{ 
    public static void main(String[] args) 
    { 
        Student s = new Student(); 
    } 
}

Output:

Person class Constructor
Student class Constructor

As super() will call the constructor of parent class, it should be the first statement to be executed in child class's constructor. If you want to call a method of parent class use super instead of super().

For More Info please read: super in java

Dinesh Kumar
  • 545
  • 4
  • 17