Java - Chapter 8

  • Uploaded by: Buba Mic
  • 0
  • 0
  • January 2021
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Java - Chapter 8 as PDF for free.

More details

  • Words: 12,314
  • Pages: 90
Loading documents preview...
c  

  

  Y Y Y . This module discusses    . . Inheritance increases the flexibility in designing classes through the creation of general classes from which other classes can derive behaviors and attributes. The class that is at the top of a hierarchy of classes is known as the superclass. Classes that derive from this class are known as subclasses. All classes in Java derive from an ultimate superclass called Object. A subclass inherits all non-private data and methods of the superclass. Data and methods can be referenced as if they were declared as normal members of the class. A subclass does not inherit superclass constructors. A subclass can override the methods of the superclass. This module also discusses    and  . An interface can only contain constants and abstract methods. A class can implement more than one interface, and unrelated classes can implement the same interface. If several classes implement a common interface, the methods declared as members of the interface can be executed   .   provides a programmer with enormous flexibility. A method can be defined to accept a general class type or interface as an argument. Any class that extends from the general class or implements the interface can then be passed as an argument to the method.

Y Y  Inheritance .  Abstraction .  Superclass .  Subclass .  Multiple inheritance .Y  à à .  Superclass object à  and    methods .  Derived methods .  Overriding .Y   and à Y  ×  Interfaces  implements  Null method definition  Polymorphism Y  Dynamic binding or virtual method invocationY

Y

Y

  

 

         .

  

  . The technique of deriving new class definitions from an existing class definition is known as inheritance. Here are some reasons for using inheritance: -- Reuse of predefined, well-tested classes Y -- Standardization of behaviors across a group of classes Y -- Ability to use members of a family of classes interchangeably in methods Y

Y With inheritanceÔY Y Y Y Y Y  Y Y YYY . New attributes, fields, or data members, and methods can be added to adapt them to new situations.

Y    Y YY Y   among classes and objects. Every object represents this "is a" relationship to its class. All objects inherit from the class that defines them. For example, in the code shown in Figure 1, the object referenced by f1 "is a" Fish. It inherits from the class   . .

In Figure 2, the object referenced by c1 "is a" Cat. It inherits its data and behavior from the class V.

Y The    and V classes a   

 aa      and data. The commonality of behaviors across some classes Y Y   for inheritance. The programmer defining these classes could also define a class x  that describes the data and behaviors common to the    and V classes. The inheritance model applied to these classes would be one where the   class is the parent class. The parent class would define common behaviors for the    and V classes. The    and V classes each inherit common behaviors from the parent class. The   and V classes also define their own adaptations of these behaviors, which include new data and new behaviors.

There exists an "is a" relationship between Animal, Fish, and Cat. A Fish is an Animal, and a Cat is an Animal. This "is a" relationship is the key concept of inheritance in object-oriented technology. Java allows "is a" relationships to exist between classes. The Java API defines all classes as inheriting from the class ‘ à, which is at the ultimate root of all class hierarchies. When discussing class hierarchy, defining explicit inheritance from the ‘ à class is not required. The   class automatically extends from ‘ à. Since the   class extends from  , the    class has all the methods and attributes of the ‘ à class. Y With inheritance the "is a" relationship extends to the root of the hierarchy. The object referenced by f1 "is a"    and "is an"  . The object referenced by c1 "is a" V and "is an"  . The   "is an" ‘ à as shown in Figure 3. The "is a" relationship between Animal, Fish, and Cat can be thought of as ³Y YY   Y    . V and    are examples of subclasses of the   class that provide additional functionality, such as the à attribute, àà method and the  method of the Cat class. 0   are classes that derive a core set of attributes and behaviors from another class. The class they derive from is referred to as the   . Another set of terms used to describe this relationship is that of a   Y  , which is  , and Y  , which is V or   . In Figure 3, the   class is the superclass for V and   , and the ‘ à class is the superclass for   and all other Java classes.

Note that the UML does not repeat a description of all the attributes and methods derived from the superclass. When an attribute from a parent or super class will be modified, the UML will repeat this description. The UML will also describe any new behaviors or attributes that are added to the subclass. Y The wY  is introduced in this UML. This symbol  Y Y Y Y  . The protected access modifier allows all the subclasses and other classes in the same package to access the method or variable. In this example, the i and à methods are protected. This allows the subclasses access to these methods, but not classes in other packages. Y

Y

Y

    @Y The same   class can also be used to derive a  à

class. This new class does not have a name attribute like the    or V classes. However, all of these classes share some similar behaviors. This similarity of behaviors and attributes allows the programmer to define a class with these common behaviors and derive the subclasses V,  à and    definitions from class  . This is shown in Figure 1. The   class generalizes the behaviors of many subclasses. Y

Y

Y Often classes that define generalized behaviors such as the   class have little use as standalone classes. In any context, objects of the type  , a very general parent class, provide very little functionality. The   class could not provide the functionality needed for a    object or the functionality needed for a V object.

{ Y  Y Y Y   Y  Y  Y Y  Y

Y  Y Y . The J       is another design feature of inheritance. The   , V and  à classes represent concrete classes from which a useful object can be created.

Y Programmers arrive at abstract and concrete designs of classes in one of two ways. - The first way is   , or discovery of general behaviors among classes. Y - The second way is    , or the identification of specific adaptation of general behaviors or adoption of new behaviors in an inherited class. Y When designing classes, Y Y Y  Y  Y Y  Y  Y Y  . These generalizations can be implemented through the design of parent classes, which are also known as superclasses. Superclasses can then be designed to describe required and optional fields and behaviors. A superclass can be an abstract representation of common behaviors and data. Y  Y Y   Y Y Y   Y  ÔY Y   Y  YYY Y  Y Y . Objects demonstrate behaviors of an abstract superclass through concrete implementations of these behaviors in subclasses.

Y The   class can define common behaviors for   , V, and  à objects. An object of the   class has almost no practical use. What a programmer uses is a specific instance of one of the derived classes. Y   Y  ÔY Y Y   Y Y Y Y  Y Y   Y Y  Y Y Y  Y Y.

{ Y Y   Y Y Y Y  Y Y   Y Y  Y Y Y YY  . In a program that manages data for an aquarium, the    class is derived as a concrete implementation of the   class. In a veterinarian application, the V object is a concrete implementation of the   class. In this manner, an abstract representation such as the   class can be used in two unrelated applications. This is shown in Figure 2.

Y   Y Y  Y Y Y  Y YY Y   YY   . For example, an application can design classes to include an |àà class and two derived classes called   à

and   à . Scroll down the graphic in Figure 3 to see the details for the   à and   à classes.

In this example, managers are represented using objects of the   à class and programmers are represented using objects of the   à class. All other employees are represented using objects of the |àà class. All of the classes in the inheritance hierarchy are concrete classes. It is not unreasonable to expect to have objects of the type |àà without any additional information described in the class.

This is an example of a parent class and derived classes that are all concrete classes and can be used to create objects. This means that each class can be used to create objects of the class. In this model, an object referenced by  is an instance of the |àà class. An object referenced by is a instance of the   à class and contains attributes and methods of the |àà class. This object ³Y ë Manager and ³Y ë Employee. Similarly an object referenced by p1 is a Programmer and an Employee.

Y While fish, cats, and spiders are all animals, their anatomy and behaviors are different. Fish swim and have fins instead of legs. Spiders have eight legs and cats only have four. These differences between fish, cats, and spiders represent specializations of the general and abstract class  .   , V, and  à classes are subclasses of the parent superclass  . In the same sense,   à and   à are subclasses of the parent class |àà.

Y A subclass definition can include a method described in the parent class. The context in which this occurs is known as . This context suggests that the subclass version of a behavior is different than the parent class version of the behavior. Rules for overriding methods of a parent class are discussed in a later section. Some parent class methods must be overridden, in order to establish the subclass as a concrete class from which objects can be created. A parent class can hold general behaviors as in the example of the   class. However, in order to declare the class as an abstraction Y   YY Y Y Y  Y YY Y Y   Y Y Y  Y YY Y Y   .

When a subclass derives from a parent class that has been declared abstract, the subclass  Y Y Y  Y that are declared abstract in the superclass, or the subclass itself must be abstract. There are some important ideas regarding the abstract or concrete status of a class. - A superclass can be either abstract or concrete. Y - A subclass can also be abstract or concrete. Y - A subclass can be derived from an abstract parent class and continue to add new methods or attributes without overriding the parent class abstract methods. In this case, the subclass is also declared abstract and no objects can be created of this class. - In order to make the subclass concrete, all abstract methods of the parent class must be overridden.

Y Y

Y

 ! " #$ "   

  In some object-oriented languages such as C++, a class can be derived from multiple parent classes or superclasses. In Java, Y  Y Y Y Y Y  . For example, if a   à object was both a member of the à  and |àà classes, and if both the à  and |àà classes have a field name and a method

àà, the JVM would not know which àà method to use for a   à object. This is shown in Figure 1.

  YY

To avoid such confusion, and because Java is a strongly typed language, Y  Y Y Y Y Y Y   Y  .

A form of multiple inheritance is provided through the use of interfaces. These are described later in this module.Y

Y Y    Y   Y  Y Y   Y  Y  . It can show that the   à class is a subclass of the |àà class, which is a subclass of the à  class. In the same regards, it shows that the à  class is a superclass to the |àà class, the   à class, and all other classes that are subclasses of the |àà class or the   à class.

Y Figure 2 illustrates the four level hierarchy path for the {à  à class, which is used in a GUI.

  YY

GUIs include items such as windows and dialog boxes to facilitate user input. They are a vital aspect of virtually all applications. Figure 3 shows the API class documentation for a class.

  YY

Note that all API documentation presents the inheritance hierarchy for each class. Also notice that the class definition statement includes the declaration that the {à  à class extends {àV à . The keyword à à is used to   Y Y     relationship between the classes.

Y In Figure 4, the API documentation shows all the methods inherited from the parent class.

Y

  YY

Y Y Y

Y

% & 

$  '$""#   

  Y

% & 

$  ( )*    

  The Java language includes specific keywords that support inheritance. The most important is the keyword à à. When a class extends from another class, it is considered a subclass. Extending from another class is also known as subclassing. The idea behind keyword à à is that Y Y  Y   Y  Y Y  Y Y  Y

 Y Y  Y  Y Y   Y  Y Y Y  Y Y  Y

 Y Y .YY

Y Y Another keyword used with inheritance is à , which allows programmers to access constructors and overridden methods of a parent class. Since Java does not permit multiple inheritances, there is only one parent class. The keyword à provides a reference to the derived attributes of the object. The keyword à cannot access private features or circumvent access rules.

Y

Figure 1 highlights the Java keywords that are associated with inheritance.Y

Y

Y Y Y

Y

    The Java language provides comprehensive support for inheritance through the API of classes. Almost any Java program that creates and uses GUIs, uses inheritance models that are implemented in the classes that form the Abstract Window Toolkit (x{º of classes. Windows is an example of a operating system that creates and uses GUIs. Each class that has been created extends from the ‘ à class, which is the superclass of all classes. The table in Figure 1 shows all of the methods of the ‘ à class.

  YY

All the attributes and behaviors of an object are defined by its class, its superclass, and the superclasses of the superclasses. Since the ‘ à class is the superclass of all classes in Java, the attributes and behaviors defined in ‘ à are present in all Java objects. As a result, ‘ à class behaviors and attributes are inherited by all new classes.

Y

The common ‘ à ancestor of all classes allows the programmer Y Y Y Y  Y Y Y  , both API or user written. It is common for a methods to have an argument that states à‘   . { Y Y  Y    Y  Y Y Y  Y Y  Y Y   Y Y Y

Y Y .

Y Each class that is created includes all of the methods of the ‘ à class. Although every class extends the ‘ à class, this information Y Y YY  YY Y  Y   . It is assumed that the class   or |àà extends the class ‘ à, and therefore does not have to be declared.

Y Every class is a descendent of the ‘ à class. When a custom class is created without extending from a parent class, the  Y 

Y   Y   an ³àà   ‘ àë as part of the class definition. In this way, all classes extend from ‘ à, either directly or indirectly through the hierarchy of the parent class. In the example of  , V and   , the   class derives directly from the ‘ à class, and the V and    classes derive indirectly through their parent class, from the ‘ à class.

Y 0Y  Y  Y Y ‘ Y  Y  Y   Y  . These are: * the à method and * the    method. These methods perform very generic actions when executed.

For example, the à  method will compare its reference value to that of another object. { Y Y Y  Y  Y Y Y  Y Y  Y Y  Y Y  . The    method will convert the reference value of an object to a String. Either of these methods may not be very useful when comparing or printing object data. In a later section the ways of overriding to change the operations of these methods will be explored.

Y In Figure 2, all of the derived methods of the   à

class from the parent class |àà are identified. Note that all the derived methods from the ‘ à class are also identified. Y

Y

Y Y Y Y

Y

  c#  

  

 Y @Y

 + #  #     

  @Y Access modifiers control what superclass attributes and methods are inherited by the subclass. Figure 1 describes access modifiers and their impact on inheritance.

{ Y  Y  Y Y   Y  Y  Y Y   Y Y  Y Y   Y  Y   Y Y  Y Y Y  Y  Y . These are the implications of the use of different modifiers: ½½½    à - A subclass cannot access these variables and methods, thus the subclass cannot override these methods. No other class can access these either. Y ½½½   àà - A subclass can be created in the package that the parent class belongs to  in a different package. Y The  àà modifier allows the subclass to access any protected attribute or method, and to override any protected method.

Additionally, classes in the same package as the parent class can access the protected method and attribute. Y   Y YY YY Y YY YY In this state, the parent class attributes can be accessed by subclasses in the same package. Although methods can be overridden in subclasses of the same or different packages, the attribute cannot be accessed by the subclass in a different package.

Y Figure 2 shows the impact of inheritance when a subclass is in the same package as the parent class and when the subclass is in a different package.

It is important to understand that all methods and data from a parent class exist in the subclass object. However, the private attributes and methods are inaccessible to the subclass object. The code examples shown in Figure include three classes, which are the  à  class, the V  class, and a {à class. { Y  Y  !Y

{ Y" Y  !Y

-- A private attribute in the  à  class is not directly accessible to the subclass. -- The non-private method of the Parent class is directly accessible. -- The non-private method accesses the private attribute. This code shows that inaccessibility does not mean that the private methods and data do not exist.

Note that the V  class makes a call to the  à  class

à method and the public variable Y  . Both of these are part of the inherited properties from the  à  class. Review the code of the {à class 4.

Note that the V  object c is used to directly access the method à from the  à  class. Both of these examples show that Y  Y

 Y  Y Y   Y  Y Y  Y Y Y .

Y If a subclass declares an attribute with the same name as an attribute of the superclass, a  Y Y  Y

  is created. Avoid doing this unless it is an OO design requirement.

This introduces uncertainty into the behavior of the classes. If another programmer uses these classes and expects the values of attributes in the parent class but receives the values of the shadow variables in the child class, their code may not work properly.

Y If a subclass declares a method with the same name, returntype, and argument as a parent class method, this is known as .

#### If the parent class declares a method to be × , this method cannot be overridden. #### If the parent class declares a method to be   this method cannot be overridden in the child class. When a child class overrides a parent class method, the child class can access the parent class method with the keyword à . Y

Y Y

Y

, &   @Y

,c &  Y @Y Y While inheritance provides extensive support to reuse existing classes,  Y  Y Y  Y  Y Y   Y  Y Y  . For example, the ‘ à class methods à  and    have limited uses if they only compare or print the addresses of objects. A    method that prints the data of an object in a formatted manner or an à  method that compares the values of two objects, would be more practical. { Y  Y Y Y Y Y YY  Y Y   Y  Y Y   Y  Y  Y Y $Y Y  Y This is another component of inheritance. Almost any method that is inherited can be overridden. The exception is a non-private method that has been declared final in the parent class. Recall that access modifiers influence inheritance. Y Y Y Y Y YY YY

Y The table in Figure 1 describes the rules for overriding parent class methods.

Y In the   class example, all of the subclasses of animals implement the à method in different ways. Therefore, the à method has been overridden in each of the subclasses. Both the  à and V objects use the i method defined in the   class, which describes how many legs are used to walk. Since    do not walk, a class which represent them does not need a walk method. This method can be overridden in the    class to reflect this situation. Figure 2 illustrates the   class methods that have been overridden in each of the subclasses. Y

Y Y Y Y

Y

Y

, &  #     @Y Y In general, class designs should include overriding the à , Và, and    methods. Apply this to the Student class used in previous examples. This is shown in Figure 1.

Y

Y Y

Y Y It may be necessary for a class to override the following Object methods. The à Và are listed together since they must be overridden together.

½½ à Y ½½à VàY ½½  à Y ½½  Y

Y A class cannot override these Object methods, since they are final: Y ½½ àV Y ½½   Y ½½   Y ½½i  Y The à  class has methods to access private data. The à  class inherits the à  method and the    method from the ‘ à class. Using these methods with à  objects produces information that is not useable. For example, both à  objects contain the same data. However, each à  object has a different address and causes the method to return false.

Y The à   method will use the    method of an object to print information about the object. Since the    method in the ‘ à class only returns the memory address of the object as a  , this is what is printed.

Y To deal with these limitations,  the   , à , and the Và methods in the à  class. The à class uses the    method of an object to print information about the object. The à  version of the    method will return a  that represents the student name, identification, and other information. When the à   method is provided a reference to the à  object, it uses

the overridden    method to print student information that is useful. This is shown in Figure 2.

Y

Y

Y Y Y

Y Y Y

Y Y Since two à  objects will need to be compared to ensure that none of the student information is duplicated, the à  method is overridden. It is possible for two à  objects to have the same data, but since they are not the same object, they will have different memory addresses. { Y Y  Y Y  Y Y   Y Y Y  Y  , which is a unique identifier given to each object on the heap. { Y  Y   is in the code of the Và method and YY Y Y Y Y Y$Y Y

Y   Y Y Y . To ensure that this unique identifier is the same, the Và method of the object needs to be overridden.

In this example, the Và method uses the student identification to determine a hashvalue. The method returns an integer as the hashvalue of the object. The à  method is overridden to compare the data in the fields of the two objects. If all fields are equal, then the method returns a true. A test of the hashvalue of the objects is also included.

Y In Figure 3, the à  method is overridden, but not the Và method. Although the objects contain the same data, since each one has been assigned a different hashvalue they will be considered different objects. Figure 3: YY YYY Y   YY Y Y Y Y  YYY YYYY Y Y YYY YYYY YYYY YYYYYY YYY Y YYYY YY YYY!YY Y"YYYY  Y#  Y Y $ %YYY Y""YYYY  Y Y %YYY Y"YYYY  Y Y &'%YYY Y"(YYYY    Y#  Y Y $ Y)Y"( %YYY Y" YYYYY Y YYY Y"YYYY Y*Y+YYY Y"YYYYYY!YY Y"YYYYYYY $ Y)Y,,%YYY Y"YYYYYY-YY Y" YYYYY Y. YY  YYY YYYYYYYY Y"YYYYYY  Y YY / Y Y Y Y YYY YYYYYYY  YYY / Y Y Y Y YYY Y(YYYYYYYY Y YYYY Y* Y Y Y+YYY YYYYYYY!YY YYYYYYYY $ Y)Y %YYY YYYYYYYY Y)Y%YYY YYYYYYYY &'Y)Y %YYY Y YYYYYY-YY

Y(YYYYY Y. Y(Y  YYY Y("YYYYYYY Y(YYYYYY  Y YY / Y Y Y Y YYY Y((YYYYYY  YYY / Y Y Y Y YYY Y( YYYYYY  Y YY / Y Y Y Y YYY Y(YYYYYYYY Y(YYYY Y* Y Y YY Y +YYY Y(YYYYYY!YY Y(YYYYYYY $ Y)Y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Y!YY ""YYYYYYY   Y Y)Y# %YYY "YYYYYYYYYYYYY #* 2 $ Y))Y $ YYY "(YYYYYYY88Y 2 &'Y))Y &'YYY " YYYYYYY88Y 2 Y))Y +YYY "YYYYYYYYY!YY "YYYYYYYYYY Y)Y%YYY "YYYYYYYYY-YY "YYYYYYYY %YYY " YYYYYY-YY ""YYYYY9

YYYY """YYYY Y   Y Y

* :;Y  +YYY ""YYYYYY!YY ""(YYYYYYYY "Y %YYY "" YYYYYYY "Y)Y.Y*,9 1Y< ,Y,7,+%YYY ""YYYYYYY Y)Y.Y*,9 1Y< ,Y,7,+%YYY ""YYYYYYY #* "26 * ++YYY ""YYYYYYYYY!YY ""YYYYYYYYYY1 22  *,Y.Y  Y YY =,+%YYY "" YYYYYYYYY-YY "YYYYYYY1 22  * "+%YYY ""YYYYYYY1 22  * "+%YYY "YYYYYY-YY "(Y>-Y?Y#YY YYY " Y-Y

"Y

Y In Figure 4, the Và and the à  methods are both overridden. This yields a more accurate result of the comparison of the two objects. Y YY YYY Y   YY Y Y Y Y  YYY YYYY Y Y YYY YYYY YYYY YYYYYY YYY Y YYYY YY YYY!YY Y"YYYY  Y#  Y Y $ %YYY Y""YYYY  Y Y %YYY Y"YYYY  Y Y &'%YYY Y"(YYYY Y   Y#  Y Y $ Y)Y"( %YYY Y" YYYYY Y YYY Y"YYYY Y*Y+YYY Y"YYYYYY!YY Y"YYYYYYY $ Y)Y,,%YYY Y"YYYYYY-YY Y" YYYYY Y. YY  YYY YYYYYYYY Y"YYYYYY  Y YY / Y Y Y Y YYY YYYYYYY  YYY / Y Y Y Y YYY Y(YYYYYYYY Y YYYY Y* Y Y Y+YYY YYYYYYY!YY YYYYYYYY $ Y)Y %YYY YYYYYYYY Y)Y%YYY YYYYYYYY &'Y)Y %YYY Y YYYYYY-YY Y(YYYYY Y. Y(Y  YYY Y("YYYYYYY Y(YYYYYY  Y YY / Y Y Y Y YYY Y((YYYYYY  YYY / Y Y Y Y YYY Y( YYYYYY  Y Y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y6 YYYY Y YYYYYYY Y YYYYYY  Y Y7Y Y Y YY  Y1YYY Y YYYYYYYY "YYYY Y   Y6 *Y +YYY ""YYYYYY!YY "YYYYYYY   Y Y)Y# %YYY "(YYYYYYY #* 2 $ Y))Y $ YYY " YYYYYYY88Y 2 &'Y))Y &'YYY "YYYYYYY88Y 2 Y))Y +YYY "YYYYYYYYY!YY "YYYYYYYYYY Y)Y%YYY "YYYYYYYYY-YY " YYYYYYYY %YYY ""YYYYYY-YY """YYYY Y YYYY ""YYYYYYY ""(YYYYYYYY / Y #Y Y Y YYY "" YYYYYYYY

Y ""YYYY Y Y *+YYY ""YYYYYY!YY ""YYYYYYY YY)Y 2 @#* &'+%YYY ""YYYYYYYY2 *+%YYY "" YYYYYY-YY "YYYYY9

YYYY ""YYYY Y   Y Y

* :;Y  +YYY "YYYYYY!YY "(YYYYYYYY "Y %YYY " YYYYYYY "Y)Y.Y*,9 1Y< ,Y,7,+%YYY "YYYYYYY Y)Y.Y*,9 1Y< ,Y,7,+%YYY "YYYYYYY #* "26 * ++YYY "YYYYYYYYY!YY

Y "YYYYYYYYYY1 22  *,Y.Y  Y YY =,+%YYY " YYYYYYYYY-YY "(YYYYYYY1 22  * "+%YYY "("YYYYYYY1 22  * "+%YYY

"(YYYYYY-YY "((Y>-Y?Y#YY YYY "( Y-Y "(Y

Y Y

Y

Y

Y

,  &    &  $ &   @Y Y The figure presents a comparison of overloading and overriding techniques. Both can be used in classes that form an inheritance hierarchy.Y

Y

Y

Y Y Y Y

Y

Y

- # 

 $"  Y

- "  

 $   

   Y A subclass should be viewed as incorporating all the methods and attributes of its hierarchy of parents. The figure illustrates the subclass  Và and the parent class !àà. To reference the methods and data of each object, it is possible to use the variables  and à in the methods. All of the methods of the subclass include the  variable and the à variable. { Y Y   Y  Y Y   Y Y Y   Y  Y { Y à Y   Y  Y Y   Y Y Y   Y YY

Y { Y Y  Y à Y Y  Y  Y Y

 Y Y  Y Y Y YYYY Y  . Y In the sample code,  and à are used in two places. The parent class method is overridden in the subclass. The overridden method calls the parent class method using the à variable. The overloaded method uses the  variable to call the method of the parent class that is overridden in this subclass.

Y To add more functionality to a parent class method, instead of completely overriding the method, call the overridden method using the à keyword. For example, the

àà à method in the subclass  Và

added functionality to the àà à method in the superclass !àà with the code on lines 15 through 18.

Y YY YY YYYYYY  Y   Y YYYYY Y YY YYYYY YY YYYYY YY Y!"Y"Y   Y#$Y%& $"' Y YY(Y)Y YY*YYYYYYYYY $Y  Y Y+Y,& Y -.,/Y YY0YY Y YYYYYYYYY  Y Y YY Y YYYYYYYYYY Y YYYYYYYYYYYYY1Y YYY  Y Y YYYYYYYYYYY Y YYYYYYYYY!"Y  Y ' 23Y $$Y  $Y Y YYYYYYYYY)Y Y (YYYYYYYYYYYYYY ' 23Y4Y /Y Y *YYYYYYYYY5Y Y 0YY YYYYYYYYYYY Y YYYYYYYYYYY Y  YY1Y  YYY  Y YYYYYYYYYYYYYY1Y YYY  Y YYYYYYYYYYYY YYYYYYYYYY!"Y  Y ' 2  Y  3Y " $$Y  $Y YYYYYYYYYY)Y Y YYYYYYYYYYYYYY ' 23Y4Y  /Y Y(YYYYYYYYY5Y Y*YYYYYYYYY$$ "Y  $Y Y0YY YYYYYYYYYY!"YY $Y 2  67Y 3Y Y YYYYYYYYY)Y YYYYYYYYYYYYYY   Y 8Y+Y9Y   23/Y YYYYYYYYYYYYYY 8  "2,.:Y -Y  Y"$ .,Y YYYYYYYYYYYYYY4Y 8 ' 233/Y YYYYYYYYYYYYYY 8  "2,.:Y -Y  Y"$ .,Y Y YYYYYYYYYYYYY4Y 8 ' 2, "$Y;",33/Y Y(YYYYYYYYY5Y Y*Y5YY$Y -Y   Y"Y Y0Y

Y YY

YY YYYYYY  Y%& $"' Y YYYYY Y YY YYYYY YY YYYYY YY Y!"Y"Y%& $"' Y YY(Y)Y YY*YYYYY $Y  Y /YY YY0YYYYY Y+Y, 9Y  . 9Y;$ 9.,/Y Y YY Y YYYYY  Y Y YY Y YYYYYY Y YYYYYYYY YYY  Y Y YYYYYY Y YYYYY!"Y  Y ' 23Y Y (YYYYY)Y Y *YYYYYYYYYY /Y Y 0YYYYY5Y YYY Y YYYYY$$ "Y  $Y YY5YY$Y -Y%& $"' Y"Y YY

Y

.  

 

 $  Y

./

   $    

 Y .

Inheritance makes non-private code and data defined in the parent class accessible to the subclass. The only exception is constructors. Constructors are not inherited in the normal way, even if they are non-private. Constructors must be defined for each subclass. Figure 1 shows the use of the à operator to construct an object and the matching constructor that is used. YY YYYY  Y&8"YY YYYYY Y YYY YYYYY YYY YYYYY YYYY YY Y!"Y"Y&8"Y YY(Y)YYYYY YY*YYYYYY$<=/YYYYY YY0YYYYYY$> ?$/YYYYY Y YYYYY$ !"Y %"/YYYYY Y YYYYY  Y$@ /YYYYY Y YYYYY  Y$'$$/YYYYYY Y YY Y YYYYA" $$Y  BY$  Y, Y  " ,YYYYY Y YYYY!"Y&8"23YYYYY Y YYYY)YYYYY Y (YYYY5YYYYY Y *YY Y 0YYYYYYYYYY YYYYYYY Y$YY$1Y$YYYYYYYYY Y YYYYYY

YYYYY!"Y&8"2Y$3YYYYY YYYYY)YYYYYYYYY YYYYYYYY$<=Y+Y$/YYYYY YYYYY5YYYYY Y YY Y(YYYYYYYYYY Y*YYYYYY Y$YY$1Y$YYYYYYYYY Y0YYYYYY Y$YY$1Y$YYYYYYYYY YYYYYYY Y YYYY!"Y&8"2Y$BYY$3YYYYY YYYYY)YYYYYYYYY YYYYYYYY$<=Y+Y$/YYYYYYYYY YYYYYYYY$> ?$Y+Y$/YYYYY YYYYY5YYYYY Y YY Y(YYYYYYYYYY Y*YYYYYY Y$YY$1Y$YYYYYYYYY Y0YYYYYY Y!"YY$1Y!"YYY$ !"YYYYYY YYYYYYY Y YYYY!"Y&8"2Y$BY$ !"Y!"3YYYYY YYYYY)YYYYYYYYY YYYYYYYY$<=Y+Y$/YYYYYYYYY YYYYYYYY %"Y+Y!"/YYYYY YYYYY5YYYYY Y YY Y(YYYYYYYYY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YYY)YYYYYYYYY Y0(YYYYYYY&8"YY+Y9Y&8"23/YYYYYYYYY Y0*YYYYYYY&8"Y!Y+Y9Y&8"2 BY 3/YYYYYYYYY Y00YYYYYYY&8"YY+Y9Y&8"2 BY 3/YYYYYYYYY YYYYYYY&8"Y$Y+Y9Y&8"2 BY,&8Y ,3/YYYYYYYYY  YYYYYYY&8"YY+Y9Y&8"2, #BY'C,BY 3/YYYYYYYYY YYYYYYY&8"Y-+Y9Y&8"2 BY,&8Y ,BY 3/YYYYY YYYY5Y Y5YY$Y -Y&8"Y"Y Y

In this example, the code for V includes overloaded constructors. Overloading means different versions of the same method exist in the same class.

Y

The keyword à à means that an object of the parent class is constructed and then extended to include the data and methods of the subclass. The creation of objects in an inheritance model is far more complex than the creation of objects with the ‘ à parent class. This is shown in Figure 2.

When the inheritance model is applied to a class definition, a number of rules apply that affect the inclusion of constructors in the child class. In order to understand these rules, it is important to recognize that a child class object contains within it the attributes of its

parent classes, all the way up to the level of the ‘ à class.  Y Y Y Y Y Y  YY  ÔY Y Y

 Y Y Y  Y  Y Y  Y  ÔY Y Y

Y

 Y Y Y Y   YY Recall the steps by which an object is created.  Y YY YY$ÔYYY  Y

Y Y Y

 Y Y Y ÔY Y Y  Y Y Y  Y%  Y   Y Y Y Y Y Y Y Y   YY   YY After all the attributes of the subclass and parent class of the object have been initialized, then the constructors for each of the classes are executed. { Y  YY Y Y  Y Y Y  Y  Y    Y 0 Y Y  Y  Y

 Y Y  Y  ÔY Y Y   Y Y Y  YY  Y Y  Y Y  Y Y Y Y .

Y Since the first constructor call in the child class constructor is a call to the parent class constructor, a number of situations exist regarding the inclusion of constructors in the parent and child class: Y --  Y Y  Y  YY Y  Y   ÔY

Y Y Y  Y Y YY Y  Y   . Recall that the default constructor is inserted by the compiler if none is provided. In the case of derived or subclasses, the compiler will insert a default constructor in the child class and it will also insert the statement à , which is a call to the parent class default constructor. Y --  Y Y  Y  Y  Y Y Y  Y   ÔY Y Y  Y YY Y  Y   . When the compiler inserts the call à , this code will compile, because a no-argument constructor has been explicitly coded in the parent class. Y

When a parent class does not include a no-argument constructor, the child class cannot use the default constructor. When the compiler tries to insert the call à , it will fail since this call is not valid for the parent class. Y -- % Y   Y Y Y  Y Y Y   Y  Y  Y Y Y Y Y   Y   . If a cascading call to several constructors is used, the constructor that will finally execute must have the call to the parent constructor as the very first statement. Y Figure 3 identifies the different conditions constructors from subclasses and superclasses.

for

using

The call to a parent constructor is through the use of the keyword à . The format of the statement is to match at least one argument list of a constructor of the parent class. The name for the parent class is not used in this call. Since Java does not permit multiple inheritance, there can only be one parent to any subclass. The variable à has a reference to the parent class object and its definition.

Y The sample code in Figures 4 and 5 demonstrates the correct and incorrect usage of the keyword à and constructors in the construction of a subclass. Figure 4: Y Y"YY YYY)Y YYYYYYYY  Y /Y YYYY YYYYYYYYY  Y YY YYYYYYY2  Y3Y

Y YY(YYYYYY)Y YY*YYYYYYYYY Y+Y/Y

YY0YYYYYY5Y Y YY Y YY Y  $Y Y Y5Y Y YY Y Y"Y"$Y#$YY Y Y)Y Y YYYYY@AYA@ DEAD Y Y (YYYYYA>?DY&?>A= Y Y *Y5Y Y 0Y

Figure 5: YY Y"YY YYY)Y YYYYYY  Y /Y YYYY YYYYYYY  Y YY YYYYY2  Y3YYY

Y YY(YYYY)Y YY*YYYYYYY Y+Y/Y YY0YYYY5Y Y YY Y YY Y  $Y Y Y5Y Y YY Y Y"Y"$Y#$YY Y Y)Y Y YYYYY""Y  Y Y (YYYY"$23Y Y *YYYY)Y Y 0YYYYYYY2, YF 9,3/Y YYYYY5Y Y YYYYYA>?DY&?>A= Y YY5Y YY

The Child class in Figure 4 will not compile. The compiler will try to insert a null constructor and an implicit call to à .

Note that the Parent class does not have a null constructor. This is because the Parent class has an explicitly defined constructor. The Child class in Figure 5 will compile. The null constructor of the Child class has been overridden to call the Parent constructor, using à and passing the argument required by the Parent constructor.

Y "  YY  Y YY . The example in Figure 6 illustrates the use of overloaded constructors that call the à constructor. Y Figure 6: YY YY YYYYY  Y Y YYYY Y YY YYYY YY YYYY YY Y!"Y"Y Y YY(Y)Y YY*YYYY  Y /Y YY0YY Y YYYYY""Y  YY

Y Y YYYY Y23)5Y Y YY Y YYYYY Y  $Y Y Y5Y Y YY Y YY Y (YYY  Y $Y Y *YY Y YY Y 0YY YY YYY Y YYYYY"Y $Y#$Y Y YYYYYY)Y

YYYYYYYYYY-"Y  Y$@ /Y YYYYYYYYYY  Y $/Y YYYYYYYYYYY$<=/Y Y YY Y(YY  Y9YY  Y Y*YY Y0YY Y YY$1Y YYY  Y YYY Y $YY$1Y $YYY  Y Y YY Y$YY$1Y$YYYY YYY YYYYY $2  Y BY  Y $BYY$3Y YYYYY)Y YYYYYYYYY23/YY""Y YY  Y Y YYYYYYYY$@ Y+Y /Y Y(YYYYYYYY $Y+Y $/Y Y*YYYYYYYY$<=Y+Y$/Y Y0YYYY5Y YYY  Y9Y9 Y  Y Y YY YYY Y YY$1Y YYY  Y YYY Y $YY$1Y $YYY  Y YYY YYYYY $2  Y BY  Y $3Y Y YYYY)Y Y(Y00000YYY$-"Y- YY$<=YY Y*Y9Y Y<$YYF 9Y Y0YYYYYYY2 BY $BY000003/YY YYYYY5Y Y YY YYY  Y9Y Y  Y YYY YYY Y YY$1Y YYY  Y YYY Y YYYY $2  Y 3Y Y(YYYY)YYYY Y*YYYYYYY2 BY, $YF 9,3/Y Y0YYYY5YYYY Y YYYYYYYYA>?DY&?>A= Y Y YYY5Y

01    .

0    . As a programmer moves up the inheritance hierarchy, classes become more general and often more abstract. At some point, the ancestor class becomes so general that it becomes a guideline for other classes, rather than a definition for an object that can be used.  Y Y  Y Y Y   Y Y Y Y  $Y Y Y Y  Y  ÔY Y  Y Y  Y Y   Y Y Y ÔY Y  Y Y  Y  Y Y Y Y Y  Y  Y Y  Y  Y Y Y $. The class is used to set boundaries for definitions of other subclasses. It is viewed as a common framework for a set of related subclasses.

Y Consider an electronic messaging system, such as fax, email, voicemail, and postal mail. The common feature of all the messaging classes is having a message. An inheritance hierarchy is shown in Figure 1.

A Message object must have information on how a message will be transported. Thus, the à à class serves as an abstract framework for all of the other subclasses.

Y { Y   Y  Y  Y Y Y Y Y Y  Y Y Y  Y Y  Y Y Y  Y Y Y Y    Y   . As the common operations are factored out, it may become clear that the details of how the operations are implemented cannot be specified in a higher-level class. For example, in the message system, the   method can be implemented in all of the subclasses. How is this implemented for the à à class? Clearly the method   is an abstract concept. It is a framework to define inclusion of such a method in the subclass definitions. An abstract method is declared using the syntax shown in Figure 2. YY YYYYYY YYYYYYY'Y!Y  $YY$"$Y YY- "" 9 Y8# Y YYYY YYYY!YY  $G Y2  3/Y YYYY YY YYYYY'Y!Y  $Y$ Y YY8Y! $8Y -Y $YY YY(YYYYY'Y"YYYY!Y  $Y Y!Y$"$Y!Y

YY*YY YY0YY!Y"Y"@ Y)Y5Y Y YY Y YYYYYY Y YY Y YYYYY!"Y!Y"Y'!= )Y Y YY Y YYYYYYY!"Y!Y$ !"Y""<2$ !"Y!"3/Y Y YY Y (YYYYY5$Y'!= Y Y *Y

Y xY   Y YY Y Y YY Y . xY  Y Y Y Y   Y  Y  Y Y   Y

 .

Y The sample code shown in Figure 3 presents the à à class definition, including the abstract display method. YY YY YYYYYY  Y& Y YYYYY Y YY YYYYY YY YYYYY YY Y!Y"Y& Y YY(Y)Y YY*YYYYY  Y$/Y YY0YYYYY  Y/Y Y YYYYY  Y!/Y Y YY Y YYYYYY Y YYYYYY Y- YY- Y Y -YY  YYY  Y Y YYYYYY Y YY Y Y -YY  YYY  Y Y YYYYYY Y YY Y -YY  YYY  YYYY Y YYYYYY Y (YYYYY!"Y& 2  Y- BY  Y BY  Y 3Y Y *YYYYY)Y Y 0YYYYYYYYY$Y+Y- /Y YYYYYYYYYYY+Y /Y Y YYYYYYYYY!Y+Y /Y YYYYYY5Y YYY YYYYYYY!Y$"8Y  $Y YYYYYY!Y $Y$"823)5Y Y YY

Y(YYYYYY Y  $Y Y*YY Y0Y5YY$Y"Y YY

Figure 4 sets out the guidelines for the creation of abstract classes. In the case of the messaging system, the abstract class à à can store sender, receiver, and subject information as data values.

These are concrete data. Programmers of many GUI classes use the abstract class design to move as many common methods and data into the superclass as possible. Y

Y:!Y*( Y< " Y'! YYYY<<
Y Y: YA!YY Y YY  Y Y  Y Y Y Y Y   Y Y   YY  Y  YY Y

 @Y  Y  Y Y Y  Y   

Y Y Y  Y 

 @YY

=   YY Y Y YY YY Y   Y  Y 

 YYY Y YYY  @Y  Y Y Y Y  Y

Y Y Y Y  Y Y Y  YY YY  YY Y

 Y Y  Y  YY  YY Y Y  Y YY @Y  Y Y 

Y YY  Y  Y YY!YY  YY  Y

 Y Y

Y Y Y Y!@Y Y Y Y Y

Y Y  Y" Y YY Y Y Y Y  Y

Y Y YY!@Y Y

Y Y Y Y  Y   Y Y 

YY Y 

 @Y Y Y Y YY  Y!YYY  YY Y 

 @YY Y# YY Y YY##$Y  Y Y Y Y Y Y Y  

Y  Y  Y Y Y  Y Y Y Y   Y  @Y Y Y Y 

YY Y Y Y Y Y  Y  Y Y    Y Y Y Y  

Y Y  Y 

 Y Y   Y Y Y  %Y

@YY Y    Y Y ##Y Y  Y Y Y   Y Y   @Y  Y  Y Y Y  Y Y Y  Y Y Y Y@YY Y Y  Y Y YY Y Y Y  "&YY '   (Y  Y'  % (Y % Y ) *+YY Y Y  Y Y Y  Y Y YYY@YY Y  Y

 Y  Y Y Y Y  Y @YY

Y  Y  Y Y  Y Y YYY!Y  Y Y  "&YY '  (Y  Y

Y,

-Y.Y/YY Y  Y  Y

 YYY Y-"@Y0 

Y Y Y   Y  Y Y Y   YY

Y Y 

 Y Y

 YY  @Y  YY Y Y

Y Y  Y Y"   Y Y  Y Y   Y Y  Y Y Y %

@Y Y Y   Y Y@YY Y  Y  ! Y    Y  

Y  Y Y  Y Y , Y  Y  Y Y  Y Y  @Y  Y  Y  

Y Y    Y Y Y  Y Y Y  Y  Y   Y  Y  Y Y Y  Y  Y @Y Y Y Y Y  Y Y  Y  Y Y Y  Y   Y Y Y Y  Y Y Y Y @Y1Y Y   Y Y  Y  Y  Y Y Y Y   Y Y  Y Y  Y Y  Y  Y Y   Y  YY @YY Y, Y Yx 

Y  Y Y   Y  Y  Y Y  Y @Y  Y Y  Y  Y Y  Y 2344,44544#44 4@Y Y Y Y Y Y Y   @Y Y 

Y Y  Y   Y Y  Y @Y  Y    Y  Y Y  Y Y   Y 

  Y  Y Y  Y  Y Y  Y Y Y Y 

 @Y  Y V  

Y   Y  YYY Y  Y Y Y Y  Y Y Y Y Y  Y Y 

Y Y Y

 Y Y Y Y  Y  YY Y Y  Y Y Y  @Y YV 

Y Y  Y YY Yx  

 Y Y Y  @YY Y0 Y Yx 

Y Y Y!Y

@Y Yx   YY Y Y

YY  Y  YYx  

 @YY

02  Y . The keyword × was introduced in the definition of data values as constants. The keyword × always indicates that the object, method, or data  YY . Y   Y ÔY Y $Y × ÔY Y Y

Y Y  Y  Y Y  ÔY  Y Y  Y Y YYY Y  .

Y The keyword × ,  Y Y Y Y  Y   ÔY  Y Y  Y Y Y Y Y Y   Y Y Y  . { Y  Y  YY  YY There are several classes in the Java language that have been declared × . One of the most commonly used is the  class. This is shown in Figure 1.

A programmer does not expect to have this class extended to some other unpredictable extension of the  class.  Y Y  Y  Y Y Y Y Y  Y  ÔY   Y

Y  Y YY× .

Y { Y Y  , which wrap object methods around Java primitivesÔY Y Y  Y× Y Y  YY   This is shown in Figure 2.

Y:!Y*( Y< " Y<Y YYY0  $Y  !"Y Y Y: YA!YY Y YY  Y Y  Y YY   Y    Y Y"  Y  Y

 YY  Y % 

 Y Y"  Y  Y

 YY  Y Y 

 @YY

 YY

Y$ Y Y Y  Y Y  Y 

YY 

 Y Y YY Y &YY Y6    YY YY Y   Y Y 

 YY Y3    YY     YY   Y   YY  Y   YY  YY Y   Y Y Y

 Y

YY Y  Y  Y

 Y Y  Y Y   YY Y   

Y Y Y

 @Y  Y   

Y  YY  Y  Y Y  YY Y

 Y  Y!  Y Y 

 @Y3

 Y Y  YY   Y Y  Y7 Y Y  Y  Y Y    @YY 

Y YY Y  Y    Y Y Y   Y Y @YY Y6   

YY

 Y YY 

Y YY Y   YY  @YY Y Y Y  Y Y Y Y

 YY  Y Y Y Y" Y YY @Y  Y Y 

Y Y Y   Y    Y Y YY Y

Y YY  @Y3

 Y Y YY  Y

 @Y Y  Y  YY

 Y Y  Y Y  Y Y



YY  YY YY Y Y Y



 Y  Y  

YY Y @YY Y3

Y YY Y Y  Y Y  Y 

 @Y Y!Y#$Y   Y Y8 Y9Y  

Y Y Y Y

@YY Y Y Y Y Y  YY YYY

Y  Y

Y Y! YY @YY Y Y!Y#$Y Y Y YYY

Y YY 

YY   @Y Y!Y!Y Y Y YYY

Y Y  Y  Y    @YY YY!Y  YYYY Y Y Y  Y Y Y Y Y Y YY!Y  YY Y Y Y  Y Y    Y Y    YY Y Y

@YY

Y# Y Y Y  7YY  Y Y  

YY Y Y Y Y  Y  @Y# YY YY   Y

@Y # Y  Y"   Y Y Y 

YY Y  Y YY Y  @YY Y#  Y Y Y Y Y Y " YY   @Y #  Y Y Y  7YY  YY Y Y

YY 

@YY Y  YY Y Y YY Y Y Y   YY  Y Y 

Y 

@YY Y Y Y  Y Y Y Y Y 

Y Y Y

Y Y Y  Y Y Y Y YY Y Y Y Y Y Y

@YY Y Y Y  Y Y Y Y Y Y

Y Y Y

Y Y Y Y  Y Y Y  YY Y  Y

Y Y Y  YY Y Y @YY Y#  YY  Y YY   Y    Y YY 

@YY Y  Y  YY Y Y

Y YY  Y

YY  Y    YY Y 

@YY Y  Y  Y  YY Y Y

YY  Y   Y Y Y 

Y Y 

Y YYY !@YY Y3  Y  YYY Y

Y

 YY  Y Y Y 

@YY Y3

Y

Y  Y  YY Y Y 

Y

 YYY   Y Y

Y  Y  YY Y  Y @Y Y"Y Y Y Y

Y Y Y  Y Y 

Y  Y  Y

 YY   Y:Y YY  @YY Y$  Y  YY Y Y

Y

 YY   Y Y Y 

@YY Y YY Y  Y    YY Y     Y Y $@YY Y  Y

 Y

 YYY Y Y Y Y   Y  @YY

Y,

  YY  Y  Y Y Y 

@Y Y  Y

 Y YYY"  Y Y Y 

  Y  Y Y Y 

@Y YY Y Y  Y

 Y YY YY  @Y Y YY Y   Y   Y Y Y 

Y

 @YY Y Y

Y Y Y  Y Y Y @Y Y   Y  Y Y Y Y  Y

  Y Y Y Y  YY YYY Y

 @Y YV 

Y   Y  YYY Y  Y Y Y Y  Y Y Y Y Y  Y Y 

Y Y Y

 Y Y Y Y  Y  YY Y Y  Y Y Y  @Y YV 

Y Y  Y YY Yx  

 Y Y Y  @YY Y

%YD"YY Y Y YY YY  Y0   Y

YY Y!Yx 

Y Y Y Y  Y@Y 3Y 

Y YY  Y Y Y

@YY Y 3  Y  Y 7 Y Y  Y  Y Y  Y  @Y  Y Y Y  Y 

Y Y  !Y Y Y 

 Y Y   Y  Y  Y  Y  Y  Y  Y  Y  @Y  Y  Y  Y Y Y Y  @Y ,  Y  Y Y Y Y  Y Y Y

  Y  Y  Y  Y @Y ,  Y 

 Y Y Y" Y Y  Y Y  Y Y

Y  Y @Y  Y   Y  Y  Y Y  Y  Y  Y  Y   Y  Y Y  Y  Y   Y  Y Y Y †  

@YY Y  YY

 Y Y Y0   

Y  Y YYV  Y   Y YY Y Y  Y YY Y  Y 0Y   Y Y Y  Y

  Y Y @Y Y Y 0Y

Y  YYV @Y Y Y Y  Y7 Y  Y Y Y Y Y   Y Y YY Y Y 0@Y;Y  YY   Y

 YY Y x 

Y Y

Y Y  Y Y  @YY Y Y   Y  Y Y Y>Y Y Y Y Y   Y Y Y "Y  @Y Y   Y Y Y Y Y Y Y  Y Y Y Y Y Y   Y Y  YY Y  @Y   Y  Y  YY Y >%?Y Y YA<@YY

  #   Y

3 

*)#  #   As stated earlier, Java does not allow for multiple inheritance.

Y Consider a   à class. This class represents individuals who are part of the Employee group of an organization. If a Programmer has to be represented as a Spouse in one context, an Employee in another, and a Programmer in yet another, Java would not enable this through inheritance. A   à class cannot extend from both a à class and an |àà class, even though a programmer could be both a Spouse and an Employee. If a programmer is described as both a Spouse and an Employee, this generally refers to the operations or behaviors that are associated with these classes. While all the data pertinent to a programmer could reside in either the parent class à or the parent class |àà, it should be possible to represent a Programmer as a Spouse in some contexts and as an Employee in others.

Y This requires a way to create an object that holds data representing the   à class and can also perform operations associated with a different class of objects. { Y &  Y   Y Y Y   Y  Y Y  Y  Y    ÔY   Y Y Y  Y Y Y Y  . Interfaces enable multiple inheritance of behaviors from different classes and serve as a design document for external features of a class. Y   Y Y   Y  Y Y  Y   Y  Y Y   . xY Y Y YY Y  Y YY .

xY  Y Y Y   ÔY YY Y Y Y

 . Interfaces provide a mechanism for a subclass to define behaviors from sources other than the direct and indirect superclasses.

Y Interfaces can be used to perform these tasks: Y -- Capture similarities among unrelated classes without artificially forcing a class relationship Y -- Declare methods that classes are expected to implement Y -- Reveal the programming interface of an object without revealing its class Y Consider the example of classes that describe things that fly. Flying includes actions such as taking off, landing, and flying. An interface can be created which defines all of theses actions in the form of abstract methods. Things that fly can include a bird, an airplane, and a helicopter. Each object takes off, lands, and flies differently. The bird flaps its wings, the airplane uses the thrust from an engine, and the helicopter utilizes a rotor blade. Each flying thing implements a different procedure for what seems to be common and similar actions. An interface such as the Flyer interface can define these common methods. Each of the classes implements this interface. Figure 1 shows the inheritance relationship between the Flyer interface and the classes.

Y In addition to implementing a Flyer interface, each of these objects is part of its own superclass/subclass hierarchy. Figure 2 illustrates how the classes relate to their inheritance hierarchies and to the interface implementations.

In this example, one can agree that an Airplane is not an Animal and a Bird is not a Vehicle, but a Bird and an Airplane are both Flyer objects.

Y xY  Y Y Y Y Y Y Y Y  Y Y Y  YY Y  Y YY Y Y   YY In the   inheritance hierarchy some of the subclasses, specifically the    and V classes, can be considered pets. These classes have common behaviors that relate to being a pet. A à interface could be added to the class design in order to address these common methods. Figure 3 illustrates the inheritance relationship between this new à interface and the other classes in the   inheritance hierarchy. Y

    . The syntax for implementing an interface includes the use of the $Y  àà in the class definition statement. Implementing interfaces is more than the declaration in the class definition statement. { a      a                   a     { Y Y   Y Y   Y Y Y Y Y  Y   Y Y Y   Y Y Y  Y Y Y  . In some cases, this may include a null implementation. A null implementation is an implementation of a method definition without any procedural code. For example, since fish do not play, a null method implementation is used to satisfy the contract between the    class and the à interface. This is shown in Figure 1.

Y Y c Y  YY Y  Y Y  Y . The compiler will automatically insert the access modifier  .

When an interface method is implemented in a class, it is a common error to use the default access modifier similar to the interface declaration. This will result in a compiler error since

Y  Y Y Y Y Y  Y YY  Y Y ÔY Y$Y Y $ Y ÔY  Y Y YY  Y Y Y $Y YY Y Y  Y Y  YY

Y Figure 2 summarizes the implementation of an interface. Y

4 )" 5) 6  5

 7$ c  &   .

4 )" Y . Polymorphism literally means "many forms". Polymorphism is a mechanism in the Java language that adds enormous flexibility to programs. To understand the concept of an object assuming many forms, review the code in Figure 1.

Note that there are objects of one class being represented by reference variables that are of a different class. These statements are constructing objects that can be handled in different ways depending on the reference type. The second row in the table shows some objects that are created. In the first statement, the object is actually a ! . The object will have all the attributes of the !  class and the   class. In this statement, the reference to the object is through the variable of the type  . This

statement to means that the object ! , in this context will be seen only in the form of the  . The only behaviors and attributes accessible will that of the   part of the object.

Y In the second statement, the object created is an | à and the reference to the object is of the type  à . In this context, the object will take on the form of the  à and the behaviors of the  à are all accessible through the reference variable. Each of these statements shows that although an object is of a particular type, the form it takes is based on the reference type used to access the object.

Y The third row illustrates another perspective of polymorphism. In this row, the objects created in the first row are cast in a different form. For example the first statement casts the !  object as its true form . The object is seen as a  and in the second row, this same object is referenced as an  . Observe that any object can be referenced by any of the forms that the class implements or inherits. The object !  can be seen as an  , a ! , or a  à .

Y In general terms,  Y Y Y Y Y Y Y

 Y

 Y Y YYY Y   Polymorphism takes advantage of inheritance and interface implementations. Polymorphism is best understood in the context of code. In Figure 2, polymorphism is used in object creation.

Y

Y

Y Y In this example, assume the |àà class and the {àà class both have a method à"à . The code for each one is different. The |àà class

à"à  method returns a String that contains the identification and name of each employee. The {àà

à"à  method returns an identification, name, classroom, and grade. If à# à"à  and

# à"à  are called, different behaviors are exhibited and different results are returned. It is not obvious what happens with e2. The object referenced by e2 is an example of polymorphism. Here the variable e2 is of the type |àà. The actual object created and resulting object reference assigned to e2 is a {àà object. At run-time, the à$ à"à  will result in the invocation of the à"à  method of the {àà object. This is known as   Y  Y  Y Y   Y  Y. The method that is executed is the method of the object, and not the method of the class of the reference variable.

Y Another contextual use of the polymorphism concept is in the context of method calls. In Figure 3, the  class has a method  |ààà.

YY YY YYYYYY"YY? " 8Y$YY !Y YYYYY Y YY YYYYY YY YYYYY YY Y!"Y"Y  "Y YY(Y)Y YY*YYYYYY YY0YYYYYY YYY? " 8Y !Y Y YYYYYY Y YYYYY!"Y  Y$"82? " 8Y3Y Y YYYYY)Y

Y YYYYYYYYY 8  "2,YY " 8Y-   Y Y,3/Y Y YYYYYYYYY 8  "2 ="233/Y Y YYYYY5Y Y YY Y (YYYYY Y *YYYYYY YYYY !Y Y 0YYYYYY YYYYYY!"Y  Y" 2Y3Y Y YYYYY)Y YYYYYYYYYY" 2, Y,3/Y YYYYYY5Y YYY YYYYYY!"YY $Y 2  Y Y673Y Y YYYYY)Y Y(YYYYYYYYY? " 8Y Y+Y9Y? " 823/Y Y*YYYYYYYYYY Y+Y9Y23/Y Y0YYYYYYYYY? " 8YY+Y9Y23/Y YYY Y YYYYYYYYY  "YY Y+Y9Y  "23/Y YYY YYYYYYYYYY  ="23/Y YYYYYYYYYY  ="23/Y YYYYYYYYYY ="23/Y Y YY Y(YYYYYYYYY $"82 3/Y Y*YYYYYYYYY $"82 3/Y Y0YYYYYYYYY $"823/Y YYY Y YYYYYYYYY " 2 3/Y YYYYYYYYYYY YYYYYYYYYYY#Y Y9""Y Y9 FYY YYYYYYYYYY!Y Y "$YY-Y YY YYYYYYYYYYYYYY? " 8Y !Y Y YYYYYYYYYY Y(YYYYYYYYY " 2 3/YYY Y*YY Y0YYYYYYYYYY YYYYYYYYYYY#Y Y9""Y9 FYY Y YYYYYYYYY!YY "$YY-Y Y YYYYYYYYYYYY YYY YYYYYYYYYYY YYYYYYYYYY " 23/YY YYYYYY5Y Y YY Y(Y5YY$Y -Y  "Y"Y Y*Y

This method accepts objects of the type |àà. In the call to this method, line 37 passes the reference to an |àà object and line 38 passes a reference to a {àà object. This works in this statement because a {àà is an |àà. Line 39 passes the variable e2, which is of the type |àà, but holds a reference to a {àà object.

Y The class named  has a method àV {àà  . This method accepts references to objects of the type {àà . Line 41 will work because t1 references a {àà object. Line 47 will not work. While a {àà is an |àà, not all employees are {àà . As a result, the |àà object is not always a {àà object. Inheritance only works in one direction, from superclass to subclass. A method that receives an object of a type can be called with objects that are subclasses of that type, but not more general or superclasses of the type. Line 54 works because it holds a reference to a {àà object, even though the variable e2 is of the type |àà.

Y x Y  Y  Y $Y  Y Y Y  Y  Y Y $Y Y Y Y    YY More cooperation and efficiency occurs when the objects are united by a common superclass. All classes in the Java programming language have an inheritance relationship. For example, a method that is defined to take  ‘ à as a parameter can accept any object in the Java platform. { Y Y  Y  YY Y  Y

47$   &  )      . The technique of Y Y  Y  Y Y  Y Y   is known as dynamic method binding, or virtual method invocation. { YY Y$Y  Y Y . Y YY YY YYYYYY"YY? " 8Y$YY !Y YYYYY Y YY YYYYY YY YYYYY YY Y!"Y"Y  "Y YY(Y)Y YY*YYYYYY YY0YYYYYY YYY? " 8Y !Y Y YYYYYY Y YYYYY!"Y  Y$"82? " 8Y3Y Y YYYYY)Y Y YYYYYYYYY 8  "2,YY " 8Y-   Y Y,3/Y Y YYYYYYYYY 8  "2 ="233/Y Y YYYYY5Y Y YY Y (YYYYYY Y *YYYYYY YYYY !Y Y 0YYYYYY YYYYYY!"Y  Y" 2Y3Y Y YYYYY)Y YYYYYYYYYY" 2, Y,3/Y YYYYYY5Y YYY YYYYYY!"YY $Y 2  Y Y673Y Y YYYYY)Y Y(YYYYYYYYY? " 8Y Y+Y9Y? " 823/Y Y*YYYYYYYYYY Y+Y9Y23/Y Y0YYYYYYYYY? " 8YY+Y9Y23/Y YYY Y YYYYYYYYY  "YY Y+Y9Y  "23/Y YYY YYYYYYYYYY  ="23/Y YYYYYYYYYY  ="23/Y YYYYYYYYYY ="23/Y

Y YY Y(YYYYYYYYY $"82 3/Y Y*YYYYYYYYY $"82 3/Y Y0YYYYYYYYY $"823/Y YYY Y YYYYYYYYY " 2 3/Y YYYYYYYYYYY YYYYYYYYYYY#Y Y9""Y Y9 FYY YYYYYYYYYY!Y Y "$YY-Y YY YYYYYYYYYYYYYY? " 8Y !Y Y YYYYYYYYYY Y(YYYYYYYYY " 2 3/YYY Y*YY Y0YYYYYYYYYY YYYYYYYYYYY#Y Y9""Y9 FYY Y YYYYYYYYY!YY "$YY-Y Y YYYYYYYYYYYY YYY YYYYYYYYYYY YYYYYYYYYY " 23/YY YYYYYY5Y Y YY Y(Y5YY$Y -Y  "Y"Y Y*Y Y

In the code for the  class, line 29 will create a variable that is a reference of the type |àà. However, at runtime, the call to the à$ à"à  on line 35 will invoke the method of the {àà class. This is because on line 29, the object that was created is a {àà

object.

Y   Y Y Y   Y  Y Y Y Y 

Y  Y Y Y Y  Y Y Y    Y   Y Y Y Y . The JVM looks at the type of object for which the call is made, not the type of object reference in the calling statement. So in line 29, e2 is a reference of the type |àà in the calling statement, but the object being called is a {àà

object. Therefore, the {àà version of the

à"à  method is used. Y

  YY YY Y Y Y Y  YY Y Y Y . Again, the JVM looks at the type of object being passed by value as an argument, not the reference type of the variable being passed as an argument to the method. In line 54, the method argument e2 is a reference variable of the type |àà. However, the object actually being referenced is a {àà object and the method àV , which requires a {àà argument, is successfully invoked. Y

Related Documents

Java - Chapter 8
January 2021 0
Java 8 New Features
February 2021 1
Chapter 8
February 2021 1
Chapter 8 (2ed)
January 2021 1
Chapter 8 Well Completions
February 2021 0

More Documents from "Mokr Achour"