Download presentation
Presentation is loading. Please wait.
1
ITEC200 – Week03 Inheritance and Class Hierarchies
2
www.ics.mq.edu.au/ppdp 2 Learning Objectives – Week 03 Inheritance 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
3
www.ics.mq.edu.au/ppdp 3 Introduction to Inheritance and Class Hierarchies Example Class Hierarchy: Exception Class Hierarchy
4
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
5
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
6
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
7
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
8
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
9
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)
10
www.ics.mq.edu.au/ppdp 10 Abstract Class Number and the Java Wrapper Classes
11
www.ics.mq.edu.au/ppdp 11 Summary of Features of Actual Classes, Abstract Classes, and Interfaces
12
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
13
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
14
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
15
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
16
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
17
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 even the classes that extend it
18
www.ics.mq.edu.au/ppdp 18 Visibility Supports Encapsulation (continued)
19
www.ics.mq.edu.au/ppdp 19 A Shape Class Hierarchy
20
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
21
www.ics.mq.edu.au/ppdp 21 Object Factories (continued)
22
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
23
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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.