Unformatted text preview:

© 2006 Pearson Addison-Wesley. All rights reserved 9 A-1Chapter 9Advanced Java TopicsCS102 Sections 51 and 52Marc Smith and Jim Ten EyckSpring 2007© 2006 Pearson Addison-Wesley. All rights reserved 9 A-2Inheritance Revisited• Inheritance– Allows a class to derive the behavior and structure ofan existing class© 2006 Pearson Addison-Wesley. All rights reserved 9 A-3Inheritance RevisitedFigure 9-1Figure 9-1Inheritance: Relationships among timepieces© 2006 Pearson Addison-Wesley. All rights reserved 9 A-4Inheritance Revisited• Superclass or base class– A class from which another class is derived• Subclass, derived class, or descendant class– A class that inherits the members of another class• Benefits of inheritance– It enables the reuse of existing classes– It reduces the effort necessary to add features to anexisting object© 2006 Pearson Addison-Wesley. All rights reserved 9 A-5Inheritance Revisited• A subclass– Can add new members to those it inherits– Can override an inherited method of its superclass• A method in a subclass overrides a method in thesuperclass if the two methods have the samedeclarations© 2006 Pearson Addison-Wesley. All rights reserved 9 A-6Inheritance RevisitedFigure 9-2Figure 9-2The subclass Ball inherits members of the superclass Sphere and overrides andadds methods© 2006 Pearson Addison-Wesley. All rights reserved 9 A-7Inheritance Revisited• A subclass inherits private members from thesuperclass, but cannot access them directly• Methods of a subclass can call the superclass’spublic methods• Clients of a subclass can invoke the superclass’spublic methods• An overridden method– Instances of the subclass will use the new method– Instances of the superclass will use the original method© 2006 Pearson Addison-Wesley. All rights reserved 9 A-8Inheritance RevisitedFigure 9-3Figure 9-3An object invokes the correct version of a method© 2006 Pearson Addison-Wesley. All rights reserved 9 A-9Java Access ModifiersFigure 9-4Figure 9-4Access to public, protected, package access, and private members of a class by aclient and a subclass© 2006 Pearson Addison-Wesley. All rights reserved 9 A-10Java Access Modifiers• Membership categories of a class– Public members can be used by anyone– Members declared without an access modifier (thedefault) are available to• Methods of the class• Methods of other classes in the same package– Private members can be used only by methods of theclass– Protected members can be used only by• Methods of the class• Methods of other classes in the same package• Methods of the subclass© 2006 Pearson Addison-Wesley. All rights reserved 9 A-11Is-a and Has-a Relationships• Two basic kinds of relationships– Is-a relationship– Has-a relationship© 2006 Pearson Addison-Wesley. All rights reserved 9 A-12Is-a Relationship• Inheritance shouldimply an is-arelationship betweenthe superclass and thesubclass• Example:– If the class Ball isderived from the classSphere• A ball is a sphereFigure 9-5Figure 9-5A ball “is a” sphere© 2006 Pearson Addison-Wesley. All rights reserved 9 A-13Is-a Relationship• Object type compatibility– An instance of a subclass can be used instead of aninstance of the superclass, but not the other way around© 2006 Pearson Addison-Wesley. All rights reserved 9 A-14Has-a RelationshipsFigure 9-6Figure 9-6A pen “has a” or“contains a” ball© 2006 Pearson Addison-Wesley. All rights reserved 9 A-15Has-a Relationships• Has-a relationship– Also called containment– Cannot be implemented using inheritance• Example: To implement the has-a relationshipbetween a pen and a ball– Define a data field point – whose type is Ball– within the class Pen© 2006 Pearson Addison-Wesley. All rights reserved 9 A-16Dynamic Binding and AbstractClasses• A polymorphic method– A method that has multiple meanings– Created when a subclass overrides a method of thesuperclass• Late binding or dynamic binding– The appropriate version of a polymorphic method isdecided at execution time© 2006 Pearson Addison-Wesley. All rights reserved 9 A-17Dynamic Binding and AbstractClassesFigure 9-7aFigure 9-7aarea is overridden: a)mySphere.DisplayStatistics( )calls area in Sphere© 2006 Pearson Addison-Wesley. All rights reserved 9 A-18Dynamic Binding and AbstractClassesFigure 9-7bFigure 9-7barea is overridden: b) myBall.displayStatistics( ) calls area in Ball© 2006 Pearson Addison-Wesley. All rights reserved 9 A-19Dynamic Binding and AbstractClasses• Controlling whether a subclass can override asuperclass method– Field modifier final• Prevents a method from being overridden by a subclass– Field modifier abstract• Requires the subclass to override the method• Early binding or static binding– The appropriate version of a method is decided atcompilation time– Used by methods that are final or static© 2006 Pearson Addison-Wesley. All rights reserved 9 A-20Dynamic Binding and AbstractClasses• Overloading methods– To overload a method is to define another method withthe same name but with a different set of parameters– The arguments in each version of an overloadedmethod determine which version of the method will beused© 2006 Pearson Addison-Wesley. All rights reserved 9 A-21Please open file carrano_ppt09_B.pptto continue viewing chapter


View Full Document

VASSAR CMPU 102 - Advanced Java Topics

Download Advanced Java Topics
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 Advanced Java Topics 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 Advanced Java Topics 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?