SJU CSC 3405 - Polymorphism and Abstract Classes

Unformatted text preview:

Key TermsBrief OutlineList of DisplaysDisplaysTeaching SuggestionsKey PointsTipsProgramming Projects AnswersSavitch, Absolute Java 2/e: Chapter 8, Instructor’s ManualChapter 8Polymorphism and Abstract ClassesKey Terms late binding, p. 477binding, p. 477early binding, p. 477polymorphism, p. 478final, p. 479upcasting, p. 492downcasting, p. 492instanceOf, p. 493clone, p. 496abstract method, p. 502abstract cannot be private, p. 505abstract class, p. 505concrete class, p. 505Brief Outline8.1 PolymorphismLate BindingThe final ModifierLate Binding with toStringDowncasting and UpcastingA First Look at the clone Method8.2 Abstract ClassesAbstract ClassesList of Displays DisplaysDisplay 8.1 The Base Class Sale, p. 481 - 483Display 8.2 The Derived Class DiscoutSale, p. 484 - 485Display 8.3 Late Binding Demonstration, p. 486Display 8.4 No Late Binding with Static Methods, p. 490Display 8.5 Copy Constructor Versus clone Method, p. 500 - 501Display 8.6 Employee Class and its Derived Classes, p. 503 - 504Display 8.7 Employee Class as an Abstract Class, p. 506Copyright © 2005 Pearson Education Addison-Wesley. All rights reserved.Savitch, Absolute Java 2/e: Chapter 8, Instructor’s ManualTeaching SuggestionsThis chapter discusses polymorphism. The concept of polymorphism is one that is sometimes hard for students to grasp. As indicated, this chapter does not require any information from Chapter 6 and could be taught before that. However, inheritance is required, so a possible reordering of topics could be Chapters 7 & 8 before Chapter 6. In this section, we also see the introduction of abstract classes. An abstract class is one that is at least partially undefined within the program. The programmer can not create an instance of an abstract class and these classes can be used as a basis of an inheritance hierarchy. When a class extends an abstract class, the subclass is required to give a definition to the abstract methods or be declared abstract. This is compiler enforced. These two topics in this chapter are somewhat disjoint and the instructor may choose to cover them at separate times as one does not need polymorphism to have an abstract class or have an abstract class to use polymorphism.Key PointsLate Binding. Java uses late binding for all method calls, which is what makes polymorphism possible. This concept deals with how the machine actually handles the call to the method at the system level. Polymorphism. The main idea of polymorphism is the ability to associate many meanings to onemethod, making the method execute differently depending on in what context the method is called. The final Modifier. Adding final to a method definition means that a method can not be redefinedin any subclass. If a class is declared final, it can not be used as a superclass.An Object Knows the Definitions of its Methods. The only methods that can be called on an object are those that are defined by its class type. The method that will get executed is the one that is defined by its object type.Abstract Method. An abstract method is one that is not defined in a particular class. The entire method signature is given with a semicolon where the method body should be. Also indicative of an abstract method is the keyword abstract in the method signature. Classes that subclass from a class with an abstract method are forced by the compiler to provide a definition for the abstract method. Abstract Class. As soon as a class has an abstract method, the entire class becomes abstract indicated by the keyword abstract in the class definition.TipsChecking to See if Downcasting is Legitimate. This tip is optional and omitting it will not hinderstudents trying to understand polymorphism. The discussion focuses on using the instanceof operator to see if downcasting makes sense in a particular case.Copyright © 2005 Pearson Education Addison-Wesley. All rights reserved.Savitch, Absolute Java 2/e: Chapter 8, Instructor’s ManualAn Abstract Class is a Type. Even though you cannot create an instance of an abstract class, you can still use it as a type of a variable or as a type for a parameter or return type. There is a tip in this section that reiterates this same idea.PitfallsNo Late Binding for Static Methods. Java does not use late binding for private methods, methodsthat are final, or static methods. This is most important for static methods. When methods are static, Java uses static binding and the method’s execution is determined at compile time, which may or may not be what the programmer had originally intended.Downcasting. It is the programmer’s responsibility to use downcasting when necessary. However, if the programmer uses downcasting in the incorrect place, a run time error will usually be thrown.Sometimes the clone Method Return Type is Object. The clone method is inherited from the classObject and when you are writing a clone method in an object, you must give its return type as Object. If you give another return type, a compiler error will be thrown because you cannot use method overloading on a method by only changing the return type. Limitations of Copy Constructor. This pitfall is another optional section and talks about why the copy constructor is not always the best choice. In fact, the clone method is the preferred method for doing the copy constructor functionality in Java. Therefore, this discussion could be relevant for why clone should be used. Alternatively, an instructor could choose not to discuss a copy constructor, but rather just wait to discuss the clone method.You Cannot Create Instances of an Abstract Class. This pitfall is pretty self explanatory. The compiler will simply not allow this to happen.ExamplesSales Records. In this example we see how late binding is used when calling the bill method of the Sale class. It also introduces the idea that as software designers, we may not always have perfect information about a problem. We can not always see all the possible ways a program willbe used when built and one way to possibly help our programs be more versatile is to harness thepower of polymorphism.Programming Projects Answers1./** * Question1AlienAbstract.java * * This program introduces inheritance by rewriting a non-OO classCopyright © 2005 Pearson Education Addison-Wesley. All rights reserved.Savitch, Absolute Java 2/e: Chapter 8, Instructor’s Manual * using a switch statement for types into one with derived classes * for the subtypes.


View Full Document

SJU CSC 3405 - Polymorphism and Abstract Classes

Download Polymorphism and Abstract Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Polymorphism and Abstract Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Polymorphism and Abstract Classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?