Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC200 – Week03 Inheritance and Class Hierarchies.

Similar presentations

Presentation on theme: "ITEC200 – Week03 Inheritance and Class Hierarchies."— Presentation transcript:

1 ITEC200 – Week03 Inheritance and Class Hierarchies

2 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 3 Introduction to Inheritance and Class Hierarchies Example Class Hierarchy: Exception Class Hierarchy

4 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 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 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 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 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 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 10 Abstract Class Number and the Java Wrapper Classes

11 11 Summary of Features of Actual Classes, Abstract Classes, and Interfaces

12 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 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 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 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 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 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 18 Visibility Supports Encapsulation (continued)

19 19 A Shape Class Hierarchy

20 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 21 Object Factories (continued)

22 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 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

Download ppt "ITEC200 – Week03 Inheritance and Class Hierarchies."

Similar presentations

Ads by Google