Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't compile sub class that implements abstract method from base class

Tags:

java

Having problems compiling sub classes of a base class that I've defined that has a single method and each sub class implements the abstract base method, but javac is saying that they don't even though it is quite clearly defined in the sub class.

DbModel.java (the base class)

package com.manodestra.db;

import java.sql.ResultSet;
import java.sql.SQLException;

public abstract class DbModel<T extends DbModel> extends Model {
    abstract T newInstance(ResultSet rs) throws SQLException;
}

DbModel extends Model, which only has a generic toString method.

MenuPermissions.java (the sub class)

package com.manodestra.csa.db.model.configNew;

import com.manodestra.db.DbModel;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;

public class MenuPermissions extends DbModel<MenuPermissions> {
    private final String menuId;
    private final String userLevel;

    public MenuPermissions(
        String menuId,
        String userLevel
    ) {
        this.menuId = menuId;
        this.userLevel = userLevel;
    }

    public String getMenuId() {
        return this.menuId;
    }

    public String getUserLevel() {
        return this.userLevel;
    }

    public MenuPermissions newInstance(ResultSet rs) throws SQLException {
        return new MenuPermissions(
            rs.getString("menu_id"),
            rs.getString("user_level")
        );
    }
}

Compilation Error

[javac] Compiling 487 source files to C:\Media\Code\manodestra_java\bin
[javac] C:\Media\Code\manodestra_java\src\com\manodestra\csa\db\model\configNew\MenuPermissions.java:10:
error: MenuPermissions is not abstract 
and does not override abstract method newInstance(ResultSet) in DbModel
[javac] public class MenuPermissions extends DbModel<MenuPermissions> {
[javac]        ^

Anyone see what the problem is here? I'm guessing that I'm overlooking something really simple.

Further info on requirements:

I'm building an entity framework that generates model objects from a given database. MenuPermissions above is one such model object (auto-generated by a class that I've written called GenerateModel). I want each model to have a method that will allow me to get a new instance of each objecct type based on a resultset, which will populate the object accordingly and return it. Ideally, it should be a static method, but I've tried it as a concrete method for the moment as I need to enforce its existence in each sub class of DbModel. Hope that makes sense.

like image 220
ManoDestra Avatar asked Aug 21 '16 20:08

ManoDestra


People also ask

Can you create subclass of abstract class?

Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.

Can a subclass of a non-abstract class be abstract?

13.3. An abstract class can be used just like a non-abstract class except that you cannot use the new operator to create an instance from the abstract class. An abstract class can be extended. A subclass of a non-abstract superclass can be abstract.

Can abstract class have method implementation?

An abstract class can have an abstract method without body and it can have methods with implementation also. abstract keyword is used to create a abstract class and method. Abstract class in java can't be instantiated.

Can abstract class implements interface in Java?

The instance of an abstract class can't be created. Now as all methods in an interface are abstract methods therefore we can implement it using Abstract Class.


1 Answers

Your abstract method newInstance has package access. I don't know if that was intended but if it is in a different package then you would get an error.

Edit:

So the abstract method in the parent class can not be resolved since it is not declared a public. A possible remedy is to add public to the method definition or move the child class into the same package as the parent class :-D

like image 130
Peter Chaula Avatar answered Sep 24 '22 06:09

Peter Chaula