Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Abstract Class And Generics

I'm trying to modify a class AuthenticatedUser to store a list of AdminRole's. AuthenticatedUser is a class used by all my applications; it gets put into a session when the user logs in successfully. Now I want to add a list of authorization roles to the user session. However each application defines the an AdminRole class a little differently. For example, my DataCenter application stores in the database:

  employee_id
  role_name
  site_name
  receive_email

Not all of my applications will need the receive_email field or may want to extend their own methods. I figured this called for an abstract class. But Eclipse is complaining about the wrong Type on the Authorized_role_list setter here in this snippet.

  DataCenterAdminRoleDAO dcAdminDao = new DataCenterAdminRoleDAO();
  try {
      List<DataCenterAdminRole> authorized_roles = dcAdminDao.getAuthorizedRoleListByBadge(authenticatedUser.getBadge());
      authenticatedUser.setAuthorized_role_list(authorized_roles);

=== Classes

public class AuthenticatedUser extends Employee implements Serializable {   
  private static final long serialVersionUID = 1L;
  private List<AdminRole> authorized_role_list;
  ...
}

public abstract class AdminRole implements Serializable {
  private static final long serialVersionUID = 1L;
  private String role_name; //regular, admin, editor, etc..
  private String site_id;  //company branches
  ...
}

public class DataCenterAdminRole extends AdminRole implements Serializable {

Obviously a fix is to return a list of AdminRole in my Datacenter implemetation but I thought by extending the abstract class I could pass the subclass. What am I missing?

like image 580
jeff Avatar asked Nov 01 '12 22:11

jeff


People also ask

Is abstract class a generic class?

An abstract class, which is just like a class, but it is declared as abstract , and some of its methods are declared as abstract , without implementation. An abstract class cannot be instantiated, and any subclass who wish to be concrete needs to implement these abstract methods.

What is the difference between abstract class and inheritance in Java?

The main difference between abstraction and inheritance is that abstraction allows hiding the internal details and displaying only the functionality to the users, while inheritance allows using properties and methods of an already existing class. Object-Oriented Programming (OOP) is a major programming paradigm.

Are generics abstraction?

Abstract and Generics are completely different things in Java syntax and semantics. abstract is a keyword, and indicates that a class does not contain a complete implementation, so cannot be instantiated.

Are abstract classes inheritance Java?

Abstract Classes and Methods Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).


1 Answers

If it's the setter that is giving you trouble you can use a lower bound with a wildcard on the authorized_roles_list

private List<? extends AdminRole> authorized_role_list;
....
public setAuthorized_role_list(List<? extends AdminRole> authorized_roles) {
...
}
like image 135
cyon Avatar answered Sep 20 '22 09:09

cyon