locked
What can a subclass do? RRS feed

  • Question

  • I am a little confused about what a sub class can do with the variables and methods inherited from the super class. According to the book I have read, the subclass can choose to do one of the following: 1. Accept the same variables and methods by doing nothing 2. Ignore the same variable and methods by doing nothing also 3. Add new variables and methods 4. Override the variables and methods. My questions are as follows: 1. If you choose to ignore or accept the variables and methods by doing nothing (you can still see these methods and variables in class view), how would the system know whether you are accepting or ignoring the variables and methods?? 2. Can you override variables? How do you do that? 3. What actually happens in Polymorphism? Do you simply override the derived variables and methods to suite the requirements of the subclass?? Thanks
    Saturday, December 9, 2006 12:08 AM

Answers

  • If you don't use them, then nothing happens, but they still exist in the class.  Everything that belongs to a base class is inherited in any subclasses.  You shouldn't hide anything, but if necessary you should override basic behaviour to provide more specialised behaviour.
    Tuesday, December 12, 2006 10:27 AM
  • The programmer deals with it by doing nothing, same goes for the system/computer.

    If you're in a situation where you want to use a subset of functionality from a class you are deriving from you could always use the facade pattern.  You basically create a new class that has the simplified interface you want to use and redirect all the calls through that class to the original.  Then when you use the new class you'll only see the members you want included.

    Tuesday, December 12, 2006 6:29 PM

All replies

  • 1. If you want to hide methods or variables that are in the Parent class I believe you can use the new keyword (which hides Parent members) in addition to the private modifier which will make them inaccessible outside the class.

    2. You can override Properties, which should be used to expose class data, giving you control over how they are set and retrieved.

    An Example of Polymorphism

    class Parent
    {
      public virtual int MultiplyMe(int x) //or abstract isntead of virtual if you don't want default behavior
      {
        return x * 1;
      }
    }

    class Child1 : Parent
    {
      public override int MultiplyMe(int x)
      {
         return x * 2;
      }
    }

    class Child2 : Parent
    {
      public override int MultiplyMe(int x)
      {
         return x * 3;
      }
    }


    //Usage
    Parent p1 = new Parent();
    Child1 c1 = new Child1();
    Child2 c2 = new Child2();

    Parent[] parents = new Parent[] {p1, c1, c2};

    int x = 10;

    foreach(Parent p in parents)
    {
      x = p.MultiplyMe(x);
    }

    //Iteration 1: Parent.MultiplyMe is used, value returned is 10
    //Iteration 2: Child1.MultiplyMe is used, value returned is 20
    //Iteration 3: Child2.MultiplyMe is used, value returned is 60

    Polymorphism allows me to treat my sub classes as the parent class, allowing me to group them easily and iterate over them for example.  When a method is called the method of the appropriate sub class is used.

    Saturday, December 9, 2006 2:41 AM
  • Sorry, back to the first question:

    Is it correct to say that the programmer who chooses either to use or not to use the variables and methods inherited from the parent class simply needs to do "nothing" to tell the child class of his / her decision??

    What I am confused about is, if the programmer chooses to use the inherited variables and methods in the child class, it makes sense to say that the programmer does not need to do any extra work since the variables and methods are automatically transfered to the child class.

    What if the programmer chooses not to use the inherited variables and methods? Would the programmer then need to either destroy or override the inherited variables and methods?

    I know the second part of the question sounds bizzar but I just want to know what would happen in a situation where the programmer decides to keep only a few inherited variables and methods.

    By the way, I got this confusion from one of the books I read recently. :-)

    Saturday, December 9, 2006 8:37 AM
  • Hiding or changing the meaning of members from the superclass is generally considered bad practice.  As it leads to confusion when using the base or the subclass.  And it can be argued that if you need to hide functionality or change its meaning, then the base functionality doesn't represent a common implementation, and therefore that functionality should migrate to the relevant subclasses.

    The superclass/base-class should provide the basic common functionality.  The subclasses should provide specialised functionality based on their type. 

    To provide a more specialised implementation of base functionality you should override the base functionality. 

    Any extra member-variables or methods in a subclass are essentially tagged on to the end of the list of base member-variables and methods.

    Saturday, December 9, 2006 8:49 AM
  • I agree with Paul, if you need to change the class that much then you shouldn't inherit from it.   

    "

    What if the programmer chooses not to use the inherited variables and methods? Would the programmer then need to either destroy or override the inherited variables and methods? "

    Look at it like this, if you bought a reference book on Cars that covered all of the cars from 1920 to 2000 but you only cared about the cars from 1950 to 1970 you would just read that section and ignore the rest.  You probably wouldn't rip out the rest of the pages and burn them :)

    Saturday, December 9, 2006 6:15 PM
  • okay thanks to all.

    I guess I should clarify my questions abit more. What I want to know is not only how the programmer deals with these unused methods and variables but more importantly how the system or computer deals with the variables and methods passed down to the subclass when the programmer chooses not to use them?

    Tuesday, December 12, 2006 10:21 AM
  • If you don't use them, then nothing happens, but they still exist in the class.  Everything that belongs to a base class is inherited in any subclasses.  You shouldn't hide anything, but if necessary you should override basic behaviour to provide more specialised behaviour.
    Tuesday, December 12, 2006 10:27 AM
  • The programmer deals with it by doing nothing, same goes for the system/computer.

    If you're in a situation where you want to use a subset of functionality from a class you are deriving from you could always use the facade pattern.  You basically create a new class that has the simplified interface you want to use and redirect all the calls through that class to the original.  Then when you use the new class you'll only see the members you want included.

    Tuesday, December 12, 2006 6:29 PM