12.07.2015 Views

Accelerated

Accelerated

Accelerated

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

60CHAPTER 4 ■ CLASSES, STRUCTS, AND OBJECTSsimple assignment expression. Underneath the covers, however, it’s really still referencing the sameCircle object. This is the gist of type specialization and the automatic conversion that goes alongwith it.Now let’s consider the rest of the code in the Main method. After you get a GeometricShape referenceon the Circle instance, you can pass it to the DrawShape method, which does nothing but callthe Draw method on the shape. However, the shape object reference really points to a Circle, theDraw method is defined as virtual, and the Circle class overrides the virtual method, so calling Drawon the GeometricShape reference actually calls Circle.Draw. That is polymorphism in action. TheDrawShape method doesn’t need to care at all about what specific type of shape the object is. All itcares about is whether it is, in fact, a GeometricShape. And Circle is a GeometricShape. This is whyinheritance is often referred to as an is-a relationship. In the given example, Rectangle is-aGeometricShape, and Circle is-a GeometricShape. The key to determining whether inheritancemakes sense or not is to apply the is-a relationship, along with some good old common sense, toyour design. If a class D inherits from a class B, and class D semantically is-not-a class B, then inheritanceis not the correct tool for that relationship.One last important note about inheritance and convertibility is in order. I’ve said that the compilerimplicitly converts the Circle instance reference into a GeometricShape instance reference.Implicit, in this case, means that the code doesn’t have to do anything special to do the conversion,and by something special, I typically mean a cast operation. Because the compiler has the ability todo this based upon its knowledge of the inheritance hierarchy, it would seem to make sense thatyou don’t have to get a GeometricShape reference before you can call DrawShape with the Circleobject instance. In fact, this is exactly true. The last line of the Main method demonstrates this. Youcan simply pass the Circle instance reference directly into the DrawShape method, and because thecompiler can implicitly convert the type to a GeometricShape reference based upon the inheritance,it does all of the work for you. Again, you can see the power of this mechanism.Now, you can pass any object instance that derives from GeometricShape. After the software isshrink-wrapped and labeled version 1, someone can come along later in version 2 and define newshapes that derive from GeometricShape, and the code for DrawShape does not need to change. Itdoesn’t even need to know what the new specializations are. They could be Trapezoid, Square(a specialization of Rectangle), or Ellipse. It does not matter, as long as they derive fromGeometricShape.Member HidingFrom the previous section’s discussion, you can see how the concept of inheritance, although apowerful one, can be overused. When programmers are first introduced to inheritance, they have atendency to use it too much, creating designs and hierarchical structures that are hard to maintain.It’s important to note that there are alternatives to using inheritance that in many cases make moresense. Among the various types of associations between classes in a software system design, inheritanceis the strongest bond of them all. I uncover many more issues with regards to inheritance nearthe end of the chapter. However, let’s go ahead and cover some basic effects of inheritance here.Note that inheritance extends functionality but cannot remove functionality. For example, thepublic methods available on a base class are available through instances of the derived class andclasses derived from that class. You cannot remove these capabilities from the derived class. Considerthe following code:public class A{public void DoSomething(){System.Console.WriteLine( "A.DoSomething" );}}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!