"Blocking" inherited  methods

I have a class, Inventory which I want to extend HashMap. However, I only want objects of certain types added to it, and in a certain way.
To do this, I envisaged having public methods like :
public Object put(MyType1 key, MyType2 value)...but then I want to prevent the use of the standard put method of the HashMap (ie. to "enforce" the above method). I attempted this by overriding it and declaring it private, but that isn't allowed by Java. Is there a way I can extend the class and not allow inherited methods to be used?
I can see two (less than satisfactory) ways around this :
1) Override the methods, but implement no functionality for general arguments. This has the drawback of not being picked up as an error if the method is used.
2) Make the HashMap an instance variable. But that would involve a lot more rewriting of other code, and re-implementing methods I want to inherit.
(There are a other methods I want to override, not just put, btw).

2) Make the HashMap an instance variable. But that
would involve a lot more rewriting of other code,
and re-implementing methods I want to inherit.It shouldn't require much rewriting for existing client code as you could put helper methods into your new class. E.G.
public int size()
  return myHashMap.size();
}Theres only a few methods in HashMap so it wouldn't take too long to do. This also has the advantage that you are now able to have your own typesafe methods. E.G.
public MyType2 put(MyType1 key, MyType2 value)
  return (MyType2)myHashMap.put(key, value);
}Col

Similar Messages

  • How can I access the protected inherited method

    Hi guys:
    I know I can use getDeclaredMethod to get some methods,but my question how can I get a protected inherited method? for example
    class A{
    protected void test(){}
    class B extends A{
    public String getValue(){}
    class C{
    public static void main(Strin[] args){
    //at here I want to access "test" method by reflection,but I dont know how?
    thanks advance!

    Indeed it does. If you want to find the declared methods of the superclass then you should be able to figure out how to do that: find the superclass, find its declared methods.

  • BUG? @Transactional doesn't work on inherited methods

    Hi all,
    I have to state an odd behaviour:
    I have a DAO interface :
    public interface UserDAO
    ..public User save(User user);
    In my application I have to use this UserDAO interface to rebuild the DAO layer with a Toplink database access. The new UserDAO implementation is injected into the original ServiceLayer. The according UserService looks like :
    public interface UserService
    ..public User save(User user);
    And the implementation :
    public class UserServiceImpl
    ..protected UserDAO userDAO;
    ..public User save(User user)
    ....userDAO.save(user);
    ..public setUserDAO(UserDAO userDAO)
    ....this.userDAO = userDAO;
    However, until I can save my user objects with Toplink I have to annotade the UserService as transactional. Therefore I would write a new UserService class, exending from the original one, like this :
    import org.springframework.transaction.annotation.Transactional;
    @Transactional
    public class TransactionalUserService extends UserServiceImpl
    And thats all, since I can inject my JPA UserDAO into this TransactionalUserService and everything will work right.
    --> BUT NO ! In this case nothing happens transactional. It only works, when I rewrite the UserService. Then I have :
    @Transactional
    public class TransactionalUserService implements UserService
    ..private UserDAO userDAO;
    ..public User save(User user)
    ....userDAO.save(user);
    ..public setUserDAO(UserDAO userDAO)
    ....this.userDAO = userDAO;
    Now my user objects are stored into the database. But why only in this case and not with inherited methods ?

    Spring is the one managing your transactions and @Transactional is a Spring annotation so you might get a quick answer to your question on the Spring Data Access Forum.
    That being said, instead of subclassing UserServiceImpl just to add @Transactional you could instead use <tx:advice> in your Spring XML configuration to make UserServiceImpl transactional.
    --Shaun                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Block Inheritance and Default Domain Policy

       Hello to all, I will run a cross-forest migration and target forest has a Default Domain Policy. Target domain is Windows 2003 Functional Level, but has almost all DCs on Windows 2008. As first level OUs represents country codes (USA, GBR, FRA,
    etc) and a new country will be created I want to block GPOs from Domain level. The task itself is very easy, just configure "Block Inheritance" on the new country OU. Important: Default Domain Policy is >> not set << to "Enforce"
    on target domain.
       Question: the security configurations (account, password, local policies) from Default Domain Policy will be blocked? If yes, how domain users below this new country OU will have basic configurations for them (password complexity, password length,
    certificates, etc) ?
       Regards, EEOC.

       Question: the security configurations (account, password, local policies) from Default Domain Policy will be blocked? If yes, how domain users below this new country OU will have basic configurations for them (password complexity, password length,
    certificates, etc) ?
    The Domain security policy for passwords etc, is domain-wide, and cannot be blocked.
    It applies to, and is controlled by, the Domain Controllers.
    Don
    (Please take a moment to "Vote as Helpful" and/or "Mark as Answer", where applicable.
    This helps the community, keeps the forums tidy, and recognises useful contributions. Thanks!)

  • Default Domain Policy security settings block inheritance

    I know this has been answered in one way but just to clarify, in our case default domain policy contains password security policies, Network security: LAN Manager authentication level, and some
    Public Key Policies/Trusted Root
    Certification Authorities settings. All of these are on computer settings, user side is disabled and is not Enforced.  
    Question is that if further down AD there is a inheritance filter applied, would all of the settings form Default domain policies would pass through or just security settings?
    I find that they have also linked the default domain policy at OUs where they have put inhetitance filter, probably thinking that they wanted to filter out every other policy but the default domain policy.
    Thanks
    NSW DECC

    Hi,
    >>Question is that if further down AD there is a inheritance filter applied, would all of the settings form Default domain policies would pass through or just security settings?
    The default domain policy will be blocked by enabling block inheritance at OU level. As Ramu suggested, we can enforce the default domain policy to prevent it from being blocked.
    In addition, regarding this question, the following thread can also be referred to for more information.
    Can I block inheritance of "Default Domain Policy"?
    http://social.technet.microsoft.com/Forums/en-US/ce5173b8-b803-4e50-b05b-c4a5677bf9ba/can-i-block-inheritance-of-default-domain-policy?forum=winserverGP
    Best regards,
    Frank Shen

  • Parameters/exceptions of inherited methods or events cannot be changed

    Hi All, I want to add parameter in method BBP_MAP_AFTER_BAPI.
    but i got message "Parameters/exceptions of inherited methods or events cannot be changed".
    Could any one help me with this problem ?
    Thanks.
    Budituta

    You have to change method signatures at the highest superclass level. 
    I.e. if A is the superclass and B is the subclass of A, then if you want to change the signature of a method in B that is inherited from A, you have to change B.
    matt

  • What is a "blocking call" method?

    Hello, I'm new to java. In a tuturial I was reading a method was described as a "blocking call" method. Could any one explain to me this concept.
    Thank you,
    Jorge
    Edited by: Jorgejch on Jan 27, 2008 6:11 PM

    A blocking call is a method in an I/O system that doesn't return until it has a result. It's the standard way most of Java I/O works. The most obvious example is when reading from a Socket, if there is no data present the call will block the calling thread until either data arrives or some time period expires. This is in contrast to the Non-blocking mode used in NIO to read from files and Sockets.
    NIO is an advanced topic and not really appropriate to "New to Java".

  • Inherited Method Conflict

    An inherited method which is not overridden should behave as if the class had that method itself, but I have found a polymorphism conflict where they act differently.
    Here is an example where a subclass provides a special method for the parent interface.
    public interface iOne { }
    public interface iTwo extends iOne { }
    public class Alpha
    {     public void a(iTwo i){;}     }
    public class Beta extends Alpha
    {     public void a(iOne i){;} }
    public class Delta implements iTwo { }
    public class Run
         public void run()
         { (new Beta()).a(new Delta()); }
    }Errors on compile.
    Run.java:9: reference to a is ambiguous, both method a(iTwo) in Alpha and method a(iOne) in Beta match
              b.a(d);
    but if the two methods are moved into the same class
    public class Alpha { }
    public class Beta extends Alpha
         public void a(iOne i){;}
         public void a(iTwo i){;}
    }It compiles and runs fine.
    Does anyone have any idea why this happens? I would prefer to keep the appropriate methods in the parent so they are re-useable.

    Let me see if I've got it by paraphrasing the section of the JLS that schapel pointed out:
    Let 'a' be a method name having 1 parameter 'i'
    - one declaration appears within a class Beta and the type of the parameter is iOne
    - the other declaration appears within a class Alpha and the type of the parameter is iTwo ...
    Then the method 'a' declared in Beta is more specific than the method declared in Alpha if and only if both of the following are true:
    1) Beta can be converted to Alpha by method invocation conversion. (personally I don't understand this line, how can you convert a class by 'method invocation conversion' ? shouldn't they have said 'convert..by casting'? If they essentially mean casting, then you can convert a Beta to an Alpha).
    2) 'i' (of type iOne) can be converted to iTwo by method invocation conversion. (you cannot convert an iOne to an iTwo).
    Note that if you reverse them and say:
    Then the method 'a' declared in Alpha is more specific than the method declared in Beta if and only if both of the following are true:
    1) Alpha can be converted to Beta by method invocation conversion. (an Alpha cannot be converted to a Beta)
    2) 'i' (of type iTwo) can be converted to iOne by method invocation conversion. (you can convert a iTwo to an iOne).
    Either way you look at it, both statements cannot be true, thus, there is no 'a' method that is more specific.

  • Redefinition / Overwriting of inherited method using class-builder

    Hi,
    I have a class which inherits some methods from it's baseclass. I want so overwrite ( re-define ) some of the methods from the baseclass using se80 / se24.
    How can this be done ?
    Best Regards,
    Frank

    Hi,
    <b>go se80</b> 1) enter the class name which u have inherited from the base class
    2) in the herarchy ...methods---->Inherited Methods
    3) right click on the method which u have to redifine.
    4) select redifine option.
    5) ur method will be placed under redifination list.
    6) go write the code
    note :- U cannot redifine final Method...Appropriate message will be shown
    save and activate.
    Mark Helpful answers
    Message was edited by: Manoj Gupta

  • Overriding Inherited methods

    why cant we override the inherited methods with more secure access modifier (private)..... while if we do the apposite ie override with more non secure access modifiers , java is able to distinguish them and call the appropriate method or pop up the syntax error.. i am not able to understand the concept ... if any one could give a practical example it would helpful to me to understand...

    rajeshrocks25 wrote:
    Hey, correct me i am wrong.
    When we inherit Java loads all the members of the super class and we override or hide them then it simply ignores the superclass members ri8???
    thats what happening in the above code?I strongly suggest rereading the tutorial for Polymorphism or searching Google for alternative explanations for "Java Polymorphism", it doesn't sound like the concept has fully sunk in yet. As for a direct answer, at the bottom of the polymorphism tutorial I linked earlier they mention:
    The Java virtual machine (JVM) calls the appropriate method for the object that is referred to in each variable. It does not call the method that is defined by the variable's type. This behavior is referred to as virtual method invocation and demonstrates an aspect of the important polymorphism features in the Java language.To add, if the object being referred to does not contain the appropriate method, then the JVM will look for an appropriate method in the superclass, and the superclass of the superclass until the appropriate method is found.
    Also to clarify your confusion earlier, if a subclass A extends a class B, then A is considered a B so there is no use in casting A to B. In a more practical sense, imagine a [Boy] extending a [Man]. The [Boy] is of course a [Boy] because that is what we have defined him as. But the [Boy] is also a [Man] because he extends the [Man]. Therefore casting a [Boy] into a [Man] is pointless, because he already is indeed a [Man], just a subclass in the form of a [Boy].
    Furthermore if we declare a [Man] and instantiate him with a [Boy] then invoking a method talk(), [Boy] is inspected for the appropriate method talk(), if it has not been overridden then the JVM will look in [Man] and calls the appropriate method. Alternatively if we declare a [Man] and instantiate him as a [Man] then invoking the method talk(), [Man] is inspected for the appropriate method. It might also be of worth to note the instantiated [Man] cannot be cast into a [Boy], because a [Boy] is a subclass of [Man] and may contain properties or methods undefined by [Man].
    Mel

  • Using inherited methods (Quick Question)

    If I have a subclass that extends a parent class, do I have to use the methods that are inherited from the parent class or can they be ignored? For example, I inherit getArea and getVolume, but I am only interested in using getArea in my subclass, can I just ignore getVolume and not do anything with it? Or is there something I have to do to block its use? Also, does the same go for the attributes that are inherited via the constructor when autogenerate the inherited constructor? Thanks!

    NPP83 wrote:
    If I have a subclass that extends a parent class, do I have to use the methods that are inherited from the parent class or can they be ignored? For example, I inherit getArea and getVolume, but I am only interested in using getArea in my subclass, can I just ignore getVolume and not do anything with it?I think the more pertinent question is: are they correct? You might be able to subclass a cube with a square (they both only require a side length), but the getVolume method would be patently incorrect for a square.
    Or is there something I have to do to block its use?UnsupportedOperationException is the most normal.
    Also, does the same go for the attributes that are inherited via the constructor when autogenerate the inherited constructor?Erm....???? If you mean, do you inherit attributes, the answer is: yes(***), unless they're private.
    Winston
    (***) Actually 'maybe', if they're default-visibility.

  • Calling inherited method of object

    Dears,
    I have this problem:
    test case
    -- base type
    create or replace type T_integer as object
    val Number,
    member procedure set_value( pValue Number)
    ) not final instantiable
    create or replace type body T_integer IS
    member procedure set_value( pValue Number) is
    begin
    if( abs(mod(pValue,1)) > 0 ) then -- not whole number
    raise value_error;
    else
    self.val := pValue;
    end if;
    end;
    END T_integer;
    -- subtype
    create or replace type T_integer_positive under T_integer
    overriding member procedure set_value( pValue Number)
    ) not final instantiable
    create or replace type body T_integer_positive IS
    overriding member procedure set_value( pValue Number) is
    begin
    if( pValue < 0 ) then -- not positive number
    raise value_error;
    else
    /** HERE IS THE PROBLEM *******/
    T_integer.set_value(self, pValue);
    end if;
    end;
    END T_integer;
    I need to call supertype's method in subtype but it leads to recurse.
    declare
    foo T_integer_positive := T_integer_positive(1);
    begin
    foo.set_value(58);
    dbms_output.put_line(foo.val);
    end;
    this pl/sql block never ends.
    Please can somebody help me how to call method of supertype in overriden method of subtype.
    In Pascal there is reserved word "inherited", what is in Oracle ?
    Many thanks fo any suggestion.
    af

    Thanks to all.
    I found in Metalink that this is really NOT supported by Oracle Objects : http://metalink.oracle.com/metalink/plsql/ml2_documents.showDocument?p_database_id=FOR&p_id=189601.995
    Zlatko, your solution is pretty but i am not sure it is usable for my wide object hierarchy (very wide) which i have to rewrite.

  • Block Paymemt method entered during PO or IR [me21n or MIRO]

    Hi, for a particular type of outbound payment scenario, we need the invoices to be blocked by default.
    Lets take for example the payment method is Wire [W]
    so if payment method wire is selected during MIRO, then a block should be placed when released to accounting
    2. user will go to document and enter some required text and then release for payment
    3. Invoice is now picked up by F110 and payment docs created.
    Is this possible to set for a particular payment method.
    Thanks
    DN

    Thanks for the response. But we cannot change the payment terms for a particular type of Vendor. No way we are going to get global approval.
    So payment terms are out of scope.
    I am just surprised, that it isnt possible to block certain payment methods by default based on payment method.
    So lets say a payment bock is set at the vendor level, is it possible to release documents in accounting once required text has been entered and then send them out? [I dont think this is possible]
    does anyone know how else payment can be blocked when released to accounting until something manual is done?

  • Can't use inherited method with JButton

    I am trying to use the getActionListeners() method on a JButton. The method is inherited from the AbstractButton class. However, when I try to compile with this line...
    ActionListener CurrentListeners[] = OKButton.getActionListeners();I get the following error:
    Method getActionListeners() not found in class javax.swing.JButton.
    I am also using the removeActionListener() method, which is inherited from the same object, and the compiler has no problem with that!

    Okay, then does anyone know how I can change the
    ActionListener for a JButton? I want to use the
    removeActionListener(ActionListener l) method and then
    addActionListener(...). However, since I can't get
    the current ActionListener, I have nothing to pass
    into the remove method.It's my belief that's this is why this method was added to AbstractButton in JDK 1.4. It makes life so much easier. Why don't you download the JDK 1.4 and install it into your IDE?
    Otherwise, you should subclass JButton as someone else suggested and provide an accessor method for the listener.

  • Inherited methods of super class not shown in outline

    Hello, is there a possibility to display the methods and variables from the super class in the outline of the inheriting class in AiE?

    Hi Christian,
    We had discussed this, when we implemented the outline and quick outline some years, ago. Finally, we sticked to the approach that is used by other plug-ins in Eclipse (like JDT).
    There Outline View shows only the content of the current editor. This helps to keep the content of the view stable when you select an element for navigation (because you won't be able to switch the editor by selecting an element ).
    Inherited members are usually part of other editors, unless you have redefined a method in your current class. Therefore, they are not shown in Outline View (unless they are redefinitions).
    The filters like "Hide Non-Public Members of classes" can only hide elements of the current editor.
    Inherited members can be shown in the Quick Outline, because the interaction pattern is different there: If you navigate to an element the pop-up is closed. Therefore, a content change of the Quick-Outline does not hurt.
    Michael

Maybe you are looking for