1 / 22

Java Programming, Second Edition

Java Programming, Second Edition. Chapter Twelve Advanced Inheritance Concepts. In this chapter, you will:. Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use the Object class and its methods Use inheritance to achieve good software design

ilori
Download Presentation

Java Programming, Second Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts

  2. In this chapter, you will: Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use the Object class and its methods Use inheritance to achieve good software design Create and use interfaces Create and use packages

  3. Creating and Using Abstract Classes • Abstract class- A class from which you cannot create any concrete objects, but from which you can inherit • You can only extend abstract classes • Use the keyword abstract • You cannot use the keyword new

  4. Creating and Using Abstract Classes • Nonabstract classes from which objects can be instantiated are called concrete classes • In other programming languages, such as C++, abstract classes are known as virtual classes

  5. Abstract Methods • Abstract method- A method with no method statements • To create an abstract method, you provide • the keyword abstract • the intended method type, name, and arguments • but you do not provide any statements within the method • You must code a subclass method to override any inherited abstract superclass method

  6. Using Dynamic Method Binding • When you create a superclass and one or more subclasses, each object of the subclass “is a” superclass object • Because every subclass “is a” superclass member, you can convert subclass objects to superclass objects

  7. Using Dynamic Method Binding • You can create a reference to a superclass • But you do not use the keyword new • You create a variable name to hold the memory address of a subclass concrete object

  8. Using Dynamic Method Binding • Dynamic method binding- The program’s ability to select the correct subclass method • Is also called late binding

  9. Creating Arrays of Subclass Objects • You might want to create a superclass reference and treat subclass objects as superclass objects so you can create an array of different objects that share the same ancestry • Manipulate an array of subclass objects by invoking the appropriate method for each subclass • Elements in a single array must be of the same type

  10. Using the Object Class and Its Methods • Every class in Java is a subclass except for the Object class • The Object class is defined in the java.lang package • java.lang is automatically imported every time you write a program • The Object class includes methods that you can override

  11. Using the Object Class and Its Methods • toString Method- If you do not create a toString() method for a class, then you can use the superclass version of the toString() method • Can be useful for debugging • equals() method- Takes a single argument, which must be the same type as the type of the invoking method • Returns a Boolean value

  12. Using Inheritance to Achieve Good Software Design • Extended superclass advantages • Subclass creators save development time • Subclass creators save testing time • Programmers who create or use new subclasses already understand how the superclass woks, so the time it takes to learn the new class features is reduced • When you create a new subclass in Java, neither the superclass source code nor the superclass bytecode is changed; the superclass maintains its integrity

  13. Creating and Using Interfaces • Multiple inheritance- The capability to inherit from more than one class • Multiple inheritance is prohibited in the Java programming language because it is problematic • Java provides an alternative to multiple inheritance: an interface

  14. Creating and Using Interfaces • Interface- Looks much like a class, except all of its methods must be abstract and all of its data (if any) must be static final • Use the keyword implements and the interface name in the class header • implements exposes elements of the program to the user without exposing the source code

  15. Creating and Using Packages • Creating packages encourages others to reuse software because it makes it convenient to import many related classes at once • When you create a number of classes that inherit from each other, you will often find it convenient to place these classes in a package

  16. Creating and Using Packages • Include a package statement at the beginning of the class file to place compiled code in the indicated folder • The package statement must appear outside the class definition • When compiling a file that you want to place in a package, use the –d compiler option with the javac command

More Related