Question:
Question about static fields and methods in abstract classes (Java)?
2009-11-12 04:19:49 UTC
Hi. I'm working on a project and I have an abstract class which provides a static hashmap and some static methods to it's subclasses, the code is below.

If I have ClassA and ClassB that both extend the Permissible class - will calling ClassA.addPermission(param1, param2) have the same effect as calling ClassB.addPermission(param1, param2)?

If this is the case - how do I modify it so that the hashmap ClassA.classPermissions is different from ClassB.classPermissions? I'd like to keep this functionality in the Permissible class if possible since it will be used in a lot of different places through my project.

Thanks for any help :)

package uk.ac.stand.cs.jh2009d.model.permission;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
*

The superclass of any object associated with permission restrictions.
* Permissable objects contain mappings from Accessor classes to sets
* of Permission objects, each set describing the permissions that
* Accessor has (or does not have by omission) over the Permissable
* object.


* @author Pete Martin
*/
public abstract class Permissible {

private static HashMap,Set> classPermissions;

/**
*

Adds a permission for a particular Accessor class.


*
* @param accessorClass The accessor class to set the permission for
* @param permission The permission to set
*/
public static void addPermission(Class accessorClass,Permission permission) {

if (!classPermissions.containsKey(accessorClass))
classPermissions.put(accessorClass, new HashSet());

classPermissions.get(accessorClass).add(permission);

}

/**
*

Removes a permission for a particular Accessor class.


*
* @param accessorClass The accessor class to remove the permission for
* @param permission The permission to remove
*
* @return true if the permission has been removed
*/
public static boolean removePermission(Class accessorClass, Permission permission) {

if (!classPermissions.containsKey(accessorClass))
return false;
return classPermissions.get(accessorClass).remove(permission);

}

/**
*

Verifies whether the specified accessor class has the specified permission
* for this permissable object.


*
* @param accessorClass The accessor class to remove the permission for
* @param permission The permission to remove
*
* @return true if the accessor class has the permission
*/
public static boolean hasPermission(Class accessorClass, Permission permission) {

if (!classPermissions.containsKey(accessorClass))
return false;
return classPermissions.get(accessorClass).contains(permission);

}

}
Three answers:
Blackcompe
2009-11-12 04:52:22 UTC
Both children call the super class method addPermisions() and since it it's static they have to share access to that method. I don't think any changes need to be made. Have you tried running that code w/ some output statements?
deonejuan
2009-11-12 04:35:29 UTC
static makes a member a CLASS member, therefore only one copy in memory no matter how many Objects are floating around. Any of the Objects can access the member. take out static if you want ClassB to have an Object member and the same for ClassA. not sure what you are trying, but maybe a distinct HashMap class and maybe an interface is what you need. I use FontHashtable all the time as a font repository if I load custom fonts.
?
2016-10-17 03:38:02 UTC
An precis classification is a classification the has unimplemented approaches which would be applied in a derived classification. precis instructions won't be able to be instantiated. An interface isn't a classification: "interface classification" does no longer mean something. An interface specifies a sequence of approaches a classification utilising it -- called imposing in Java -- will define. An interface is a settlement between the class and the shopper specifying a minimum set of approaches it implements. for greater tips, Google is your chum. HTH


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...