6 Replies Latest reply: May 3, 2010 2:20 AM by DarrylBurke RSS

    Using inherited methods (Quick Question)

    843789
      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!
        • 1. Re: Using inherited methods (Quick Question)
          843789
          Yes you are free to ignore methods and attributes of the parent class. If you never call/access/use them then nothing will happen. However, you might want to rethink your design. If a child never needs "things" that a parent class provides then is it really a child class.
          • 2. Re: Using inherited methods (Quick Question)
            843789
            Thanks. I thought so. Well what I am running into is I am using polymorphism on the methods I am inheriting, but the auto generated constructor is including super() with the parameters I am inheriting from the parent class, and often enough I am only needed to use only a few of those parameters. When comes time to instantiate the class in a main() method for testing, can I just enter 0 for those parameters which I dont need and none of my modified methods use?
            • 3. Re: Using inherited methods (Quick Question)
              843789
              The relationship between a class X and a class Y that it extends is sometimes summed up by saying that what is being modelled is that a X IS-A Y. This relationship is quite strong and quite specific.

              So if the parent class has a getVolume() method then that method will apply to any instance that IS-A parent including those that happen also to be instances of the child class. You are free not to use getVolume() or any of the methods that the parent class offers. But if you find yourself actually wanting to prevent instances of the child class from using this method that may be a sign that child class IS-NOT-REALLY-A parent. And, in that case, you might want to reconsider whether using "extends" is really appropriate.
              • 4. Re: Using inherited methods (Quick Question)
                YoungWinston
                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.
                • 5. Re: Using inherited methods (Quick Question)
                  843789
                  very slow ... :(

                  As an alternative you could have a Paintable interface with a getArea() method and a Fillable interface with a getVolume(). Then define
                  class Cube implements Paintable, Fillable {etc
                  class Rectangle implements Paintable {etc
                  The relationship is not modelled using extends.
                  • 6. Re: Using inherited methods (Quick Question)
                    DarrylBurke
                    NPP83 wrote:
                    does the same go for the attributes that are inherited via the constructor when autogenerate the inherited constructor?
                    Constructors aren't inherited.

                    db