Transcript

Java ProgrammingFifth Edition

Advanced Inheritance Concepts

Java Programming, Fifth Edition 2

Objectives

• Create and use abstract classes

• Use dynamic method binding

• Create arrays of subclass objects

• Use the Object class and its methods

Java Programming, Fifth Edition 3

Objectives (continued)

• Use inheritance to achieve good software design

• Create and use interfaces

• Create and use packages

Java Programming, Fifth Edition 4

Creating and UsingAbstract 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– Example:

public abstract class Animal

Java Programming, Fifth Edition 5

Creating and UsingAbstract 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, Fifth Edition 6

Creating and UsingAbstract 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, Fifth 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

• Example:

Animal ref;

ref = new Cow();

Java Programming, Fifth 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

Using Dynamic Method Binding (continued)

Java Programming, Fifth Edition 9

Java Programming, Fifth Edition 10

Using a Superclass asa Method Parameter Type

• Use superclass as method argument– Pass in subclass

• Use dynamic method bindingpublic static void talkingAnimal (Animal animal)

Dog dog = new Dog();

talkingAnimal(dog);

Java Programming, Fifth Edition 11

Using a Superclass asa Method Parameter Type (continued)

Java Programming, Fifth Edition 12

Java Programming, Fifth Edition 13

Creating Arrays ofSubclass Objects

• Create superclass reference – Treat subclass objects as superclass objects

• Create array of different objects

• Share same ancestry

• Creates array of three Animal referencesAnimal[] ref = new Animal[3];

– Reserve memory for three Animal object references

Java Programming, Fifth Edition 14

Using the Object Classand 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, Fifth Edition 15

Java Programming, Fifth Edition 16

Using the toString() Method

• toString() method– Converts Object into String – Contains information about Object– Output

• Class name

• @ sign

• Hash code

Java Programming, Fifth Edition 17

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

Using the toString() Method (continued)

Java Programming, Fifth Edition 18

Java Programming, Fifth Edition 19

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, Fifth Edition 20

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, Fifth Edition 21

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, Fifth Edition 22

Java Programming, Fifth Edition 23

Using Inheritance To AchieveGood Software Design

• Create powerful computer programs more easily – If components used “as is” or slightly modified

• Make programming large systems more manageable

Java Programming, Fifth Edition 24

Using Inheritance To AchieveGood Software Design (continued)

• Advantages of extendable superclasses– Save development time

• Much code already written

– Save testing time• Superclass code already tested

– Programmers understand how superclass works– Superclass maintains its integrity

• Bytecode not changed

Java Programming, Fifth Edition 25

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, Fifth Edition 26

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, Fifth Edition 27

Creating and Using Interfaces (continued)

Java Programming, Fifth Edition 28

Creating and Using Interfaces (continued)

Java Programming, Fifth Edition 29

Java Programming, Fifth Edition 30

Creating and Using Interfaces (continued)

• Create an interface– Example: 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, Fifth Edition 31

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, Fifth Edition 32

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, Fifth Edition 33

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

Creating Interfaces to Store Related Constants (continued)

Java Programming, Fifth Edition 34

Java Programming, Fifth Edition 35

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, Fifth Edition 36

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, Fifth Edition 37

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, Fifth Edition 38

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, Fifth Edition 39

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, Fifth Edition 40

You Do It (continued)

• Overriding the Object class equals() method

• Eliminating duplicate user entries

• Creating a package

Don’t Do It

• Don’t write a body for an abstract method

• Don’t forget to end an abstract method header with a semicolon

• Don’t forget to override any abstract methods in any subclasses you derive

• Don’t mistakenly overload an abstract method instead of overriding it

• Don’t try to instantiate an abstract class object

• Don’t forget to override all the methods in an interface that you implement

Java Programming, Fifth Edition 41

Java Programming, Fifth Edition 42

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, Fifth Edition 43

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

top related