When Constructors are Called
When a class hierarchy is created, in what order are the constructors for the classes that, make up the called ?For example, given a subclass called B and a super class called A, is. As constructor called before B’s, or vice The answer is .that in a class hierarchy, constructors are called in order of derivation, from super class to subclass. Further, since super must be the first statement executed in a subclass’ constructor, order ,is the same whether or not super used. If super is not used, then the default or parameter less constructor of each super class will be executed. The following program illustrates when constructors are executed.
In a class hierarchy, when a method in a subclass has the same name and type signature as a method in.its super class, then the method in the subclass is said to override the method in the super class. When an overridden method is called from within a subclass-It will always refer to the version of that method defined by the subclass. The version of the method defined by the super class will be hidden. Consider the following.
While the examples in the preceding section demonstrate the mechanics of method
overriding, they do not show its power. Indeed, if there were nothing more to method overriding than a name space convention; then it would be, at best, an interesting curiosity, but of little real value. However, this is not the case. Method overriding forms the basis for one of Java’s most powerful concepts: dynamic method dispatch. Dynamic method dispatch is the mechanism by which a call to an overridden function is resolved at run time, rather than compile time. Dynamic method dispatch is . important because this is how Java implements run-time polymorphic. Let’s begin by restating an important principle: a super class reference variable can refer to a subclass object. Java uses this fact to resolve calls to overridden methods at . run time. Here is how. When an overridden method is called through a super class reference, Java determines which version of that method to execute based upon the . type of the object being referred to at the time the call occurs; Thus, this determinate on is made’ at run time. When different types of objects are referred to, different versions of an overridden method will be called, In other words, it is the type of tile being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed. Therefore, if a super class contains a method that’ is overridden by a subclass, then when different types of objects are referred to reference variable, different versions of the method are executed. Here is an example that illustrates dynamic ‘method dispatch.
This program creates one super class called A and two sub classes of it, called Band
Sub classes B and C override, call me declared in A. Inside the main method, objects of type A, B, and C’CU ‘declared. Also, a reference of type A, called r, is declared. The program then assigns’ a reference to each type of object to r and uses. that reference to As the output shows, the version of call me executed is determined by~the type of object being referred the time of the call. Had it been determined by . the type of the reference variable, r, you would see three calls to method .
Why’ Overridden .Methods
As stat~d earlier, overridden methods allow Java to support time polymorphism. Polymorphism is essential too object-oriented programming for one reason: it allows a general class to specify methods that will be common to all of its derivatives, while . allowing subclasses to define the specific implementation of some or all of those methods. Overrid en methods are-another way that Java implements the ‘tone ‘ interface, multiple methods” aspect of polymorphism Part of the key to successfully applying polymorphism is understanding that the super classes and sub classes form a hierarchy which moves from lesser to greater
specialization. Used correctly, the super class provides all elements that a subclass can use directly. It also defines those methods that the derived class must implement on its own. This allows the subclass the flexibility to define its own methods, yet still enforces a consistent interface. Thus, by with overridden . methods, a super class can define the general form of the methods that will be used by all of its sub classes.
Dynamic, run-time, polymorphic one of.the most powerful mechanisms that object-oriented design brings to hear on code reuse’ and robustness. The ability of existing code libraries to call methods on instances of new classes without recompiling while maintaining a clean abstract interface is a profoundly powerful tool.