CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2003
    Location
    American living in Vienna, Austria
    Posts
    19

    disabling parent's functions when inheriting ..?

    Hi,
    I have a very annoying JAvA exercise to do in which one class needs to be an "exdends"-child of a parent. The child should NOT have functions avaliable though that the parent does have. As in .. the parent has a function called pair() .. this function should not be callable for the child. I can modify the parent as well as the child however much I like .. both have to be non-abstract classes though, and if possible, the inheritance should be direct.
    As far as I can tell, I can't really tell the child that it doesn't have functions its parent has. am I missing a loophole?

    Thanks.

  2. #2
    Join Date
    Aug 2004
    Posts
    165

    Re: disabling parent's functions when inheriting ..?

    Declare pair() as a private method in the super class (parent).

    The subclass (child) then won't be able to call this method. Also if the subclass were to create it's own pair() method this would not interfere/effect/or have anything to do with the pair() method of the superclass.

    This excercise seems to be teaching you about access modifiers. I suggest you go through your notes to see if you've actually been told about them... it would help.

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: disabling parent's functions when inheriting ..?

    The public interface of a class is inherited and exposed by its subclasses. There's nothing you can do to change that, because although you can override methods in the subclass to give wider access, you can't restrict access. This is why a subclass is said to have an 'is-a' relation to the superclass - it is a superclass type (possibly extended). This is known as the Liskov Substitution Principle, which basically says that any subclass reference can be substituted wherever a superclass reference is used.

    If this behaviour is not what you want (i.e. you don't want to expose some public superclass methods), then inheritance is not the correct technique to use.

    A common technique is to use containment (the Adapter pattern), where your class holds an instance of what would have been the superclass as a private field, and declares methods that forward to this contained instance. This way, you can expose only those methods of the contained instance that you want, and you have the opportunity of changing the names and behaviour to better fit your exact requirements. However, since your class is not a subclass of the original, it can't be used where a reference to the original is expected.

    The Java SDK sometimes uses a horrible hack to get around this problem and simply overrides any undesirable superclass methods to simply throw an UnsupportedOperationException when they are called. This is poor design and breaks the LSP, but I guess an ugly compromise is sometimes easier than doing it right...

    The Java SDK Stack class is an example of inheritance incorrectly used instead of containment - it subclasses Vector and so exposes all Vector's public methods.

    Inheritance is often overused by novices - to a man with a hammer, everything looks like a nail...
    Last edited by dlorde; November 26th, 2004 at 10:10 AM.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: disabling parent's functions when inheriting ..?

    Quote Originally Posted by Spacecowboy_J
    Hi,
    I have a very annoying JAvA exercise to do in which one class needs to be an "exdends"-child of a parent. The child should NOT have functions avaliable though that the parent does have.
    In summary of what dlorde notes; you cant really do this (or you shouldnt) because then, it means that the child is not actually an extension of it's parent.. Suppose you have a Vehicle and a Car. A Car is an extension of Vehicle, but if a Vehicle can be driven and a Car cannot, then how can you assert that a Car is a Vehicle?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured