Java Programming Fourth Edition
Chapter 12 Advanced Inheritance Concepts
Java Programming, Fourth Edition 2
Objectives
• Create and use abstract classes • Use dynamic method binding • Use a superclass as a method parameter • Create arrays of subclass objects
Java Programming, Fourth Edition 3
Objectives (continued)
• Use the Object class and its methods • Use inheritance to achieve good software design • Create and use interfaces • Create and use packages
Java Programming, Fourth Edition 4
Creating and Using Abstract Classes
• Abstract class – Cannot create any concrete objects – Can inherit – Usually has one or more empty abstract methods – Use keyword abstract – Provide superclass from which other objects can
inherit – public abstract class Animal
Java Programming, Fourth Edition 5
Creating and Using Abstract Classes (continued)
• Abstract method does not have: – Body – Curly braces – Method statements
• To create abstract method – Keyword abstract – Header including method type, name, and
arguments – Include semicolon at end of declaration – public abstract void speak();
Java Programming, Fourth Edition 6
Creating and Using Abstract Classes (continued)
• Subclass of abstract class – Inherits abstract method from parent
• Must provide implementation for inherited method • Or be abstract itself
– Code subclass method to override empty superclass method
Java Programming, Fourth Edition 7
Using Dynamic Method Binding
• Every subclass object “is a” superclass member – Convert subclass objects to superclass objects – Can create reference to superclass object
• Create variable name to hold memory address • Store concrete subclass object Animal ref; ref = new Cow();
Java Programming, Fourth Edition 8
Using Dynamic Method Binding (continued)
• Dynamic method binding – Also called late binding – Application’s ability to select correct subclass
method – Makes programs flexible
• When application executes – Correct method attached to application based on
current one
Java Programming, Fourth Edition 9
The AnimalReference Application
Java Programming, Fourth Edition 10
Using a Superclass as a Method Parameter
• Use superclass as method argument – Pass in subclass
• Use dynamic method binding public static void talkingAnimal(Animal animal)
Dog dog = new Dog(); talkingAnimal(dog);
Java Programming, Fourth Edition 11
The TalkingAnimalDemo Class
Java Programming, Fourth Edition 12
Output of TalkingAnimalDemo Application
Java Programming, Fourth Edition 13
Creating Arrays of Subclass Objects
• Create superclass reference – Treat subclass objects as superclass objects
• Create array of different objects • Share same ancestry
• Creates array of three Animal references – Animal[] ref = new Animal[3]; – Reserve memory for three Animal object references
Java Programming, Fourth Edition 14
Using the Object Class and Its Methods
• Object Class – Every Java class extension of Object class – Defined in java.lang package – Imported automatically – Includes methods to use or override
Java Programming, Fourth Edition 15
Using the toString() Method
• toString() method – Converts Object into String – Contains information about Object – Output
• Class name • @ sign • Hash code
Java Programming, Fourth Edition 16
Using the toString() Method (continued)
• Write overloaded version of toString() method – Display some or all data field values for object – Can be very useful in debugging a program
• Display toString() value • Examine contents
Java Programming, Fourth Edition 17
The TestBankAccount Application
Java Programming, Fourth Edition 18
Using the equals() Method
• equals() method – Takes single argument
• Same type as invoking object – Returns boolean value
• Indicates whether objects are equal – Considers two objects of same class to be equal
• Only if they have same hash code
Java Programming, Fourth Edition 19
Using the equals() Method (continued)
• Example of equals() method if(someObject.equals(someOtherObjectOfTheSameType))
System.out.println("The objects are equal");
• To consider objects to be equal based on contents – Must write own equals() method
Java Programming, Fourth Edition 20
Using the equals() Method (continued)
• Object method hashCode() – Returns integer representing hash code – Whenever you override equals() method
• Should override hashCode() method as well • Equal objects should have equal hash codes
Java Programming, Fourth Edition 21
The BankAccount Class Containing Its Own equals() Method
Java Programming, Fourth Edition 22
Using Inheritance To Achieve Good Software Design
• Create powerful computer programs more easily – If components used “as is” or slightly modified
• Make programming large systems more manageable
Java Programming, Fourth Edition 23
Using Inheritance To Achieve Good Software Design (continued)
• Advantages of extendable superclasses – Save development time
• Much code already written – Save testing time
• Superclass code already tested – Programmers understand how superclasses works
Java Programming, Fourth Edition 24
Creating and Using Interfaces
• Multiple inheritance – Inherit from more than one class – Prohibited in Java – Variables and methods in parent classes might have
identical names • Creates conflict • Which class should super refer when child class has
multiple parents?
Java Programming, Fourth Edition 25
Creating and Using Interfaces (continued)
• Interface – Alternative to multiple inheritance – Looks like a class
• Except all methods and data items implicitly public, abstract, and final
– Description of what class does – Declares method headers
Java Programming, Fourth Edition 26
The Animal and Dog Classes and a Worker Interface
Java Programming, Fourth Edition 27
The WorkingDog Class
Java Programming, Fourth Edition 28
Creating and Using Interfaces (continued)
• Create an interface – public interface Worker
• Implement an interface – Keyword implements
• Requires subclass to implement own version of each method
– Use interface name in class header • Requires class objects to include code • public class WorkingDog extends Dog implements Worker
Java Programming, Fourth Edition 29
Creating and Using Interfaces (continued)
• Abstract classes versus interfaces – Cannot instantiate concrete objects of either – Abstract classes
• Can contain nonabstract methods • Provide data or methods that subclasses can inherit • Subclasses maintain ability to override inherited
methods • Can include methods that contain actual behavior
object performs
Java Programming, Fourth Edition 30
Creating and Using Interfaces (continued)
• Abstract classes versus interfaces (continued) – Interface
• Methods must be abstract • Programmer knows what actions to include • Every implementing class defines behavior that must
occur when method executes • Class can implement behavior from more than one
parent
Java Programming, Fourth Edition 31
Creating Interfaces to Store Related Constants
• Interfaces can contain data fields – Must be public, static, and final
• Interfaces contain constants – Provide set of data that many classes can use
without having to redeclare values
Java Programming, Fourth Edition 32
The PizzaConstants Interface
Java Programming, Fourth Edition 33
Creating and Using Packages
• Package – Named collection of classes – Easily imports related classes into new programs – Encourages other programmers to reuse software – Helps avoid naming conflicts or collisions – Give every package a unique name
Java Programming, Fourth Edition 34
Creating and Using Packages (continued)
• Create classes for others to use – Protect your work
• Do not provide users with source code in files with .java extensions
• Provide users with compiled files with .class extensions
– Include package statement at beginning of class file • Place compiled code into indicated folder
Java Programming, Fourth Edition 35
Creating and Using Packages (continued)
• Compile file to place in package – Use Compiler option with javac command
• -d option places generated .class file in folder – Use type-import-on-demand declaration
• import com.course.animals.* – Or use separate import statements for each class
• Cannot import more than one package in one statement
Java Programming, Fourth Edition 36
Creating and Using Packages (continued)
• Java ARchive (JAR) file – Delivered to users – Compress and store data
• Reduce size of archived class files – Based on Zip file format
Java Programming, Fourth Edition 37
You Do It
• Creating an abstract class • Extending an abstract class • Extending an abstract class with a second subclass • Instantiating objects from subclasses • Using object references
Java Programming, Fourth Edition 38
You Do It (continued)
• Overriding the Object class equals() method • Eliminating duplicate user entries • Creating a package
Java Programming, Fourth Edition 39
Summary
• Abstract class – Class that you create only to extend from, but not to
instantiate from – Usually contain abstract methods
• Methods with no method statements • Can convert subclass objects to superclass objects • Dynamic method binding
– Create a method that has one or more parameters that might be one of several types
– Create an array of superclass object references but store subclass instances in it
Java Programming, Fourth Edition 40
Summary (continued)
• Interface – Similar to a class – All methods implicitly public and abstract – All of its data implicitly public, static, and final – Create class that uses interface
• Include keyword implements and interface name in class header
• Place classes in packages – Convention uses Internet domain names in reverse
order