Hiding inherited methods

hi,
I need to know how to hide a method from a subclass because it doesn't need it at all. I think using aggregation will solve the problem but how? do you have any examples regarding this please?
For example, if the class Dog defines the properties "bark," "fetch," and "eat food" then a class Cat can be derived from Dog by hiding "bark" and "fetch" and adding "purr" and "sleep." And then "bark" shouldn't appear in the Cat class, is using aggregation solve the problem? by putting the class Cat as "whole" and class Dog as its "part".

Poor design, poor use of inheritance. Cats aren't dogs, simple as that. This relationship is meaningless. Cat and Dog could be subclasses of Animal, which could have a method 'makeNoise' that both Cat and Dog could implement with their own noise. But a cat isn't a special kind of dog, so inheritance makes no sense here. Further, if you think something should be a subclass, but you have to find some way of getting rid of some methods from the superclass, chances are it isn't a subclass at all

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • 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

  • "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

  • 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

  • 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

  • Overwriting inherited method of a class

    Hi all,
    http://help.sap.com/saphelp_erp2004/helpdata/en/65/d4463c9c9ffc3ae10000000a114084/frameset.htm
    As given in the above link I inherited the class CL_BSP_CONTROLLER2. When I try to overwrite a method called DO_REQUEST I'm prompted for an access key.
    Did I miss something?
    Regards
    Noufal

    You have to click on the button called "<b>Redefine</b>" which is between "<b>Find Next</b>" and "<b>Undo</b>".
    Regards
    Raja

  • 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.

  • Overriding and hiding a method.. the difference?

    hello,
    the text says that "...a subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method. A subclass can hide a static method in the superclass by declaring a static method in the subclass with the same signature as the static method in the superclass"
    what does this mean? i don't see the difference between this overriding and hiding of the methods? is it just the concepts??
    what's the difference when i have a non-static method in the superclass overridden by a non-static method in the sub class and when i have a static method hidden by a static method in the subclass?

    over-riding means that you declare an implemenation of some method on the current class that over-rides the implemenation inherieted from your superclass. Over-riding the toString method to print customer data would be a good example- the implemenation on the current class will be invoked, instead of the superclass. Because static methods are bound to a class, rather then instance, they can not be over-ridden. Instead, you can only replace that method with a method of same signature and name. Maybe this code makes it more clear:
    Object arg = MyClass.getInstance();
    //the implemenation of MyClass.toString will be
    //invoked if it is over-ridden, even though Object.toString
    //is being referenced
    arg.toString();But, static methods, like Class.method() and SubClass.method(), have no inherieted functionality- they are totally unique methods that simply share the same name. I'm not sure if this explanation makes it clear or not, but the operative point is, there's no functional relationship between static methods.

  • Inherited method doesn't as expected

    Hi,
    I have a situation like this:
    public class Parent {
         private String a = "a";
         public void go() { System.out.println(a); }
    public static void main(String args[]) {
              Child child = new Child();
              child.go();
    class Child extends Parent {
         public String a = "b";
    I expect this to print "b", but it prints "a".
    Can anyone explain this, please?
    Tnx

    Thanks for your response, but ... sorry ... I still
    don't understand it exactly.
    There is one object, but since the variable a is
    declared private in Parent, I don't expect that
    variable to be present in the object since the object
    is instanciated with new Child().Of course it's present in the object. The object has everything that is in the parent, grandparent, child, etc.--everything from every class up the tree. Not everything is accessible from everywhere, however. Code in the child class can't access private members of the parent class, for instance. But in this case, since the method we're executing is defined in the parent, that method can access the parent's private members.

Maybe you are looking for