Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Method overriding error with base class as an argument

I have the following code:

interface Entity {
    
}
class Student implements Entity{
    
}
class Course implements Entity{
    
}

interface BaseRepository {
    public void save(Entity entiy);
}

class StudentRepository implements BaseRepository {
    @Override
    public void save(Student student) {
        // student validation code
        // save the entity
    }
}
class CourseRepository implements BaseRepository {
    @Override
    public void save(Course course) {
        // course validation code
        // save the entity
    }
}

When I try to compile it, gives me the following error:
StudentRepository is not abstract and does not override abstract method save(Entity) in BaseRepository

Doesn’t java accept a ‘Base’ class as an argument? What is the reason?
Is there any alternate way to write the code?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

An overriding method must:

  • have the exact same name
  • have the exact same argument types; sub types won’t work!
  • have a visibility that is the same or broader (so protected -> public is allowed, protected -> private isn’t)
  • have a return type that is the same or a sub type

You’re violating the second rule here. Fortunately, you can use generics to fix this:

interface BaseRepository<E extends Entity> {
    public void save(E entiy);
}

class StudentRepository implements BaseRepository<Student> {
    @Override
    public void save(Student student) {
        // student validation code
        // save the entity
    }
}
class CourseRepository implements BaseRepository<Course> {
    @Override
    public void save(Course course) {
        // course validation code
        // save the entity
    }

}

Now, the method that a BaseRepository<Student> should override is not public void save(Entity) but public void save(Student). Similarly, the method that a BaseRepository<Course> should override is not public void save(Entity) but public void save(Course).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading