itec200 – week03 inheritance and class hierarchies
Post on 19-Dec-2015
236 views
TRANSCRIPT
ITEC200 – Week03
Inheritance and Class Hierarchies
www.ics.mq.edu.au/ppdp 2
Learning Objectives – Week 03Inheritance and Class Hierarchies (Ch3)
Students can:
• Interpret and design inheritance class hierarchies facilitating code reuse (including the use of interfaces, abstract classes)
• Explain and use the OO concepts of method overriding, method overloading and polymorphism
• “clone” an object and can explain the difference between a true clone (deep copy) and a shallow copy
• Explain how interfaces and delegation can be used to emulate features of multiple inheritance and use these features appropriately
• Describe the levels of visability that Java supports and designate these levels appropriately in class design
• Create and use object factories
www.ics.mq.edu.au/ppdp 3
Introduction to Inheritance and Class Hierarchies
Example Class Hierarchy: Exception Class Hierarchy
www.ics.mq.edu.au/ppdp 4
A Superclass and a Subclass
• Consider two classes: Computer and Laptop• A laptop is a kind of computer and is therefore a
subclass of computer
www.ics.mq.edu.au/ppdp 5
Method Overriding vs Method Overloading
• If a derived class has a method found within its base class, that method will override the base class’s method
• The keyword super can be used to gain access to superclass methods overridden by the base class
• A subclass method must have the same return type as the corresponding superclass method
www.ics.mq.edu.au/ppdp 6
Polymorphism
• A variable of a superclass type can reference an object of a subclass type
• Polymorphism means many forms or many shapes• Polymorphism allows the JVM to determine which
method to invoke at run time• At compile time, the Java compiler can’t determine
what type of object a superclass may reference but it is known at run time
www.ics.mq.edu.au/ppdp 7
Polymorphism and Casting
• A variable can reference an object whose type is a subclass of the variable type
• The type of reference, not the type of the object referenced, determines what operations can be performed
• Java is a strongly typed language so the compiler always verifies that the type of the expression being assigned is compatible with the variable type
www.ics.mq.edu.au/ppdp 8
Java 5.0 Reduces Need for Casting
• Two new features that reduce the need for casting:– Autoboxing/unboxing
– Generics
• Autoboxing/unboxing eases the conversion between a primitive type and its corresponding wrapper type
www.ics.mq.edu.au/ppdp 9
Abstract Classes
• An abstract class is a class that has abstract methods (defines signature but not implementation)
Features:– Abstract classes can contain data fields, and concrete methods– An abstract class cannot be instantiated– An abstract class can declare abstract methods, which must be
implemented in its subclasses– An abstract class can have constructors to initialize its data fields
when a new subclass is created• Subclass uses super(…) to call the constructor
– May implement an interface but it doesn’t have to define all of the methods declared in the interface (Implementation is left to its subclasses)
www.ics.mq.edu.au/ppdp 10
Abstract Class Number and the Java Wrapper Classes
www.ics.mq.edu.au/ppdp 11
Summary of Features of Actual Classes, Abstract Classes, and Interfaces
www.ics.mq.edu.au/ppdp 12
Class Object
• Object is the root of the class hierarchy; every class has Object as a superclass
• All classes inherit the methods defined in class Object but may be overridden
www.ics.mq.edu.au/ppdp 13
The Object.clone method
• Java provides the Object.clone method to help solve the shallow copy problem
• The initial copy is a shallow copy as the current object’s data fields are copied
• To make a deep copy, you must create cloned copies of all components by invoking their respective clone methods
www.ics.mq.edu.au/ppdp 14
Using Multiple Interfaces to Emulate Multiple Inheritance
• Multiple inheritance: the ability to extend more than one class
• Java does not facilitate multiple inheritance directly
• However, if we define two interfaces, a class can implement both
• Multiple interfaces emulate multiple inheritance
www.ics.mq.edu.au/ppdp 15
Implementing Reuse Through Delegation
• You can reduce duplication of modifications and reduce problems associated with version control through a technique known as delegation
• In delegation, a method of one class accomplishes an operation by delegating it to a method of another class
www.ics.mq.edu.au/ppdp 16
Packages
• Package is a bundle of classes that have been logically grouped together
• The package to which a class belongs is declared by the first statement in the file in which the class is defined using the keyword package followed by the package name
• All classes in the same package are stored in the same directory or folder
• Files that do specify a package are considered part of the ‘default’ package
www.ics.mq.edu.au/ppdp 17
Visibility
• Visability refers to the access control for classes and their data elements (eg, public, protected, package, private)
• Public visability (keyword ‘public’) allows all classes/methods/data-fields (ie all members) to access the class / data fields / methods
• Protected visability (keyword ‘protected’) allows all members of the package and any class that wishes to extend the class to access the class / data fields / methods
• Package visibility is the default (no keyword) only allows members of the package to access the classes / data fields / methods of the package
• Private visibility (keyword ‘private’) is for members of a class that should not be accessible to anyone but the class, not eventhe classes that extend it
www.ics.mq.edu.au/ppdp 18
Visibility Supports Encapsulation (continued)
www.ics.mq.edu.au/ppdp 19
A Shape Class Hierarchy
www.ics.mq.edu.au/ppdp 20
Object Factories
• An object factory is a method that creates instances of other classes
• Object factories are useful when:– The necessary parameters are not known or must be
derived via computation
– The appropriate implementation of an interface or abstract class should be selected as the result of some computation
www.ics.mq.edu.au/ppdp 21
Object Factories (continued)
www.ics.mq.edu.au/ppdp 22
Where to from here…
• Work through Chapter 3 of the Koffman & Wolfgang Text
• Conceptual Questions and Practical Exercises• Submit all preliminary work• Be prompt for your online class
www.ics.mq.edu.au/ppdp 23
Acknowledgements
These slides were based upon the Objects, Abstraction, Data Structures and Design using Java Version 5.0 Chapter 3 PowerPoint presentation
by Elliot B. Koffman and Paul A. T. Wolfgang