DOC PREVIEW
UNF COP 2551 - Inheritance

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 8OutlineClass HierarchiesSlide 4The Object ClassThe Object Class – Example: to StringSlide 7 The Object Class – Example: equalsAbstract ClassesAbstract Classes and their MethodsSlide 11Slide 12Slide 13Slide 15Visibility RevisitedSlide 17Slide 18Slide 19Slide 20Slide 21Designing for InheritanceInheritance Design IssuesInheritance Design Issues - moreRestricting InheritanceSummaryChapter 8InheritancePart 2© 2004 Pearson Addison-Wesley. All rights reserved 2/23OutlineCreating SubclassesOverriding MethodsClass HierarchiesInheritance and VisibilityDesigning for Inheritance© 2004 Pearson Addison-Wesley. All rights reserved 3/23Class Hierarchies•A child class of one parent can be the parent of another child, forming a class hierarchyBusinessKMart MacysServiceBusinessKinkosRetailBusiness© 2004 Pearson Addison-Wesley. All rights reserved 4/23Class Hierarchies•Two children of the same parent are called siblings Common features should be put as high in the hierarchy as is reasonable•An inherited member is passed continually down the line•Therefore, a child class inherits from all its ancestor classes•There is no single class hierarchy that is appropriate for all situations© 2004 Pearson Addison-Wesley. All rights reserved 5/23The Object Class•A class called Object is defined in the java.lang package of the Java standard class library All classes are derived from the Object classLook at your JavaDoc to see the inheritance!•If a class is not explicitly defined to be the child of an existing class, it is assumed to be the child of the Object class•Therefore, the Object class is the ultimate root of all class hierarchies© 2004 Pearson Addison-Wesley. All rights reserved 6/23The Object Class – Example: to String•The Object class contains a few useful methods, which are inherited by all classes (which may/may not be overridden…)•For example, the toString method is defined in the Object class•Every time we define ‘our own’ toString method, we are actually overriding an inherited definition. We have done this repeatedly in objects for which we want ‘certain’ attributes displayed simply by printing the object-only name. By developing our own ‘toString()’ we can tailor the outputs to be exactly what we want and not use the default from Object.So what happens if we System.out.println(objName) and don’t have a toString method in that object? Try it.© 2004 Pearson Addison-Wesley. All rights reserved 7/23The Object Class – Example: to String•Note: the Object class is the prime class in the language.•The toString method in the Object class (and it has one) is defined to return a string indicating the object’s class plus a few other goodies. •This toString() method in Object is inherited (unless we override) •Normally, when we want to print objects, we want certain attributes to be displayed and in formats that ‘we’ would like to see. Thus we frequently override the default toString() in Object and code our ‘own’ toString method in ‘our’ objects so that when we invoke it, it prints out what we want.© 2004 Pearson Addison-Wesley. All rights reserved 8/23 The Object Class – Example: equals•The equals method of the Object class returns true (Boolean) if two references are aliases (contain the same address)•We can (and very often do) override equals in any class to define equality in some more appropriate way •As we've seen, the String class defines the equals method to return true if two String objects contain exactly the same characters!•The designers of the String class have overridden the equals method inherited from Object in favor of a more useful version that we normally use in comparing Strings.Consequently, we use the overridden format with: int equals (object1.equals(object2))© 2004 Pearson Addison-Wesley. All rights reserved 9/23Abstract Classes•An abstract class is a placeholder typically in a class hierarchy that represents a generic concept An abstract class cannot be instantiated (that is, cannot make objects from it).•We use the modifier abstract on the class header to declare a class as abstract:public abstract class Product{ // contents}// end Product© 2004 Pearson Addison-Wesley. All rights reserved 10/23Abstract Classes and their Methods•An abstract class often contains abstract methods that contain a method name with no definition (no body). Classes that inherit will supply a body (later) Unlike an interface, the abstract modifier (the tag) must be applied to each abstract method in an abstract class because an abstract class may also contain non-abstract methods (concrete methods) with full definitions. So those methods that are abstract methods must be clearly defined to be abstract. These may be well used in places where we plan on overriding its methods later…and we want a placeholder.)© 2004 Pearson Addison-Wesley. All rights reserved 11/23Abstract Classes and their Methods• Placeholders??? What??•This means that some of its methods in an abstract class may be abstract and some may be concrete •In an abstract class, we must tag a method as abstract, if it is abstract, as in: public abstract int myMethod (……) •A class declared as abstract does not have to contain abstract methods – to make it abstract: simply declaring it abstract makes it so as in public abstract GoofyClass. But if it does have methods, those methods that are abstract must be declared to be abstract.•There are some very important applications that use abstract classes!!© 2004 Pearson Addison-Wesley. All rights reserved 12/23Abstract Classes•The child of an abstract class must override all the abstract methods of the parent, or it too will be considered abstractSo, if an abstract class contains abstract methods and possibly concrete methods), all the abstract methods must be overridden by a child, unless the child too is to be abstract.Sometimes we have an inheritance hierarchy containing levels of abstract classes.•Note: An abstract method cannot be defined as final or static, because it could not then be overridden.Abstract methods are almost always overridden. The use of abstract classes is an important element of software design – it allows us to establish common elements in a hierarchy that are too generic to instantiateThink: calculate area of a geometric


View Full Document

UNF COP 2551 - Inheritance

Download Inheritance
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 Inheritance 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 Inheritance 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?