Sei sulla pagina 1di 24
UNIT2 [43 bjct-rentedfeaturesindasses, 2.4 Extending Classes for Modifying Behaviours — Method Overloading and Polymorphism Objectives 1. Define overloading methods, overriding method and polymorphism. 2. Describe object's behaviours under different situations by overloading methods. 3. Modify object’s behaviours by overriding methods. 4, Describe polymorphism. Introduction ‘You learnt that an object performs its behaviour whenever it receives message, and it is usually necessary ro supply supplementary data to an object so that i has sufficient data to perform the operations aecordingly. The corresponding implementation in Java isa method with a suitable parameter list. Occasionally, you find that an object needs to perform the same behaviour with differene set of supplementary data. This ‘means that the object must accepca message type with different sets of supplementary «data, The implication is that che corresponding class definition defines more than ‘one method with the same name but with different parameter lists. This is possible and is known as overloading. We will discuss this feature very soon. In the previous section, we discussed a feature of object-oriented programming language named inheritance. By using such a feature, we can create new classes based on an existing one and add new attributes and methods that are specific to the new classes only. Also, it is possible to derive the subclasses that have all bchaviouts inherited from the superclass, but the subclass objects can perform the bchavious differently. Java makes such eapability possible by a feacure named overriding discussed later. ‘As subclasses ofthe same superclass inherit the same set of methods, objects ofthese subclasses and the common superclass have the methods defined in the superclass. ‘As well as from simply using the methods in the superclass, the subclasses may redefine the methods in cheir own ways. Consequently, if any object of these classes accepts the same message, they may behave differently. Such a feature is known as polymorphism. |44) WAWASAN OPEN UNIVERSITY C102 Computing Behaviours under different situations — Overloading methods [An object performs a behaviour if ie receives the corresponding message. When the object performs the behaviour, it usually requires some supplementary data Sometimes, the object is expected to perform the same behaviour under different situations. Thats, the number and/or the types of supplementary data are different From the implementation poine of view, since a method name determines the “object behaviour and its method parameter list specifies the supplementary data t0 be supplied to the object, i is expected thar a class definition permits more than one method named the same but wich different parameter lists. This is known as overloading. More information about overriding can be found in the following reading. ® Reading Malik, D S, Java Programming: From Problem Analysis to Program Design, “Method Overloading: An Introduction, p. 413-414 We have actually used methods that feature overloading, The one we have used the most is the printin () method. This method is defined in the class ave io.Peintstream, which is provided by the software library that comes with the J2SDK. For example, to display the contents of a variable named vax, itis possible to use the statement, system.out.printin (var); disregarding the type of the variable var. The variable can be of primitive or non- primitive type (including array type). Ie is understood that the printin ) method displays the contents ofa variable. The expected operation isthe same, but the type of the supplementary data can be different, ‘According to the definition of the class java.io.Printstream, there are ten ‘prinein() methods in coral but with differenc parameter lists. They ate public void printing) (s+. public void printIn (boolean x) ( ... I public void printin(char x) { -.. 1 public void printin(int x) ( -.. public void printin(long x) {s+ public void println (float x) (s+) public void printin (double x) (..- public void printin(char(] x) (++) public void printin (java.lang.String *) { public void printin (java.lang.Object *) |... J UNIT2 [45 Object-rentesfesturesin dass, The ten printia() methods have different parameter lists. One of them accepts no parameter, which shows nothing but ensures the next message to be shown is on 2 rnew line on the screen. Six methods accept a primitive-type parameter. The others accept a parameter of three commonly used non-primitive types char (), String and object. If Java did not support overloading, the above methods would have been: public void printin() (... 3 public void printBooleanin (boolean x) (+++ } public void printcharin(char x) ( ... public void printintin(int x) (-.. 1 public void printLengin(long x) ( -.. public void printfloatin(float x) (+. ) public void printDowblela (double x) { -.. } public void printchararrayin(char[) x) {++ ) public void printSteingla(java-lang.string x) { +.) public void printbjectIn (java.lang.Object x) { -.. J} Java features overloading that enables a class definition to define more than one ‘method with the same name but with different parameter lists according to the number of parameters and/or the parameter types. The return type ofthe overloaded ‘methods can be different, even though they are usually the same. With respect to the aforementioned printin() methods, you can verify that their parameter lists are different in either the number of parameters (no parameter or one parameter) or the parameter types. ‘When a class definition is compiled and a statement callsa method ofan object (ora class ificis a class method), the compiler sofeware verifies whether the corresponding class defines a method of that name and its parameter list is compatible, For example, the following statement, systom.out.printin (100) displays the valuc 100 on the sexeen. A literal 100 is by default an ine value. ‘Therefore, the compiler verifies whether there is a print1n() method that can accept a parameter of type int. As the method definition, pt a pi public void peintin(int x) ( ... } is found, the compilation succeeds and the above print in () method is the one to be executed at runtime. For the following starement, double d-3.147 systen.ovt-printin (a); the compiler software can figure out the type of the value to be supplied to the printin() method is double. Therefore, the peintin() method that accepts a parameter of type doubLe will be executed at runtime.

Potrebbero piacerti anche