Java Programming, Third Edition - PowerPoint PPT Presentation

1 / 51
About This Presentation
Title:

Java Programming, Third Edition

Description:

Keyword abstract used to create abstract class ... Create empty method within an abstract class ... You may create reference to abstract superclass object ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 52
Provided by: dwigh2
Category:

less

Transcript and Presenter's Notes

Title: Java Programming, Third Edition


1
Java Programming, Third Edition
  • Chapter Twelve
  • Advanced Inheritance Concepts

2
Objectives
  • Create and use abstract classes
  • Use dynamic method binding
  • Create arrays of subclass objects
  • Use the Object class and its methods

3
Objectives (continued)
  • Use inheritance to achieve good software design
  • Create and use interfaces
  • Create and use packages

4
Creating and Using Abstract Classes
  • Superclass contains features shared by its
    subclasses
  • Abstract class is a superclass that is not
    instantiated
  • Abstract class is a template for subclasses
  • Keyword abstract used to create abstract class
  • Abstract classes have abstract and non-abstract
    methods

5
Creating and Using Abstract Classes (continued)
  • Abstract methods are empty
  • Two ways to create abstract method
  • Use abstract keyword
  • Create empty method within an abstract class
  • Concrete child must override inherited abstract
    methods
  • Example abstract class named Animal
  • String attribute called nameOfAnimal
  • Non-abstract setAnimalName( ) and getAnimalName(
    )
  • Abstract method named speak( )

6
(No Transcript)
7
Creating and Using Abstract Classes (continued)
  • Create three classes extending Animal
  • Dog, Cow, and Snake
  • Override speak( ) within each child class

8
(No Transcript)
9
(No Transcript)
10
(No Transcript)
11
Creating and Using Abstract Classes (continued)
  • Demonstrate three methods in UseAnimals driver
  • Instantiate Dog, Cow, Snake objects
  • Utilize non-abstract setter and getter from
    parent
  • Call specific versions of speak( )
  • Abstract Animal class supports polymorphism
  • Polymorphism one method name referring to many
    implementations

12
(No Transcript)
13
(No Transcript)
14
Using Dynamic Method Binding
  • Each object of a subclass is a superclass object
  • You may convert subclass objects to superclass
    objects
  • You may create reference to abstract superclass
    object
  • Reference variable points to a memory address
  • Reference is declared with name only, like
    primitive
  • Reference to abstract superclass may store child
    class object
  • Example AnimalReference
  • Animal holds Cow object and then Dog object

15
(No Transcript)
16
(No Transcript)
17
Using Dynamic Method Binding (continued)
  • Call to speak( ) based on reference content
  • A form of polymorphism
  • Binding class method to object occurs at run-time
  • Dynamic method binding
  • Technique behind virtual method calls
  • Correct method bound to object based on current
    (dynamic) context

18
Creating Arrays of Subclass Objects
  • An Array of abstract class references is legal
  • Stores different child objects with same ancestry
  • Example AnimalArray class
  • Array declaration Animal ref new Animal3
  • Statement does not instantiate Animal objects
  • Statement reserves room for Animal object
    references
  • All array operations valid

19
(No Transcript)
20
(No Transcript)
21
Using the Object Class and its Methods
  • Object class is ancestor of every class coded in
    Java
  • Your class extends Object or a descendant of
    Object
  • Object class is defined in the java.lang package
  • java.lang automatically imported
  • You may use or override Object class methods

22
The toString( ) Method
  • Object class toString( ) method
  • Converts Object into a String
  • String contains information about Object data
  • String value returned by toString( )
  • Class name of which object is an instance
  • _at_ symbol
  • Hexadecimal address (memory location) of object
  • Example DogToString application
  • Call Object class toString( ) against Dog
    instance
  • Output Dog_at_1ac04e8

23
(No Transcript)
24
(No Transcript)
25
The toString( ) Method (continued)
  • Object class toString( ) method may be overridden
  • User-defined toString( ) returns useful data
  • toString( ) helpful in debugging applications
  • Example BankAccountClass
  • Explicit toString( ) method returns attributes
  • toString( ) called in TestBankAccount
  • Bug inferred from returned data
  • balance attribute improperly initialized

26
(No Transcript)
27
(No Transcript)
28
(No Transcript)
29
The equals( ) Method
  • Object class equals( ) takes single argument
  • Argument identical in type to calling object
  • SomeObject.equals(anOtherObjectOfTheSameType)
  • Returns true if objects are equal
  • Object class equals( ) view of equality
  • Both objects have the same memory addresses
  • One object is a reference to another
  • Example CompareAccounts application
  • Two BankAccount objects initialized with same
    values
  • Object equals( ) returns false (addresses not the
    same)

30
(No Transcript)
31
(No Transcript)
32
The equals( ) Method (continued)
  • Object class equals( ) may be overridden
  • User expects equals( ) to compare attributes
  • Example BankAccount equals( ) method
  • Compares attributes of two BankAccount objects
  • Returns true if attribute values identical
  • Called against two objects in earlier example

33
(No Transcript)
34
(No Transcript)
35
Using Inheritance to Achieve Good Software Design
  • Features of good software design
  • Correctness, reliability, robustness,
    reusability, efficiency, ease of maintenance
  • Inheritance is part of good software design
  • Development time reduced (build on ancestors)
  • Debugging time reduced (ancestors tested)
  • Learning time reduced (superclass basis known)
  • Integrity maintained (superclass separately
    compiled)

36
Creating and Using Interfaces
  • Multiple inheritance class derives from more
    than one superclass
  • Java does not allow multiple inheritance
  • Rationale for banning inheritance
  • Confused inheritance lines
  • Possible name conflict
  • Ambiguity involved in binding super( )
  • Interface is an alternative to multiple
    inheritance

37
Creating and Using Interfaces (continued)
  • Interface describes what a class does, not how it
    does it
  • Features specific to an interface
  • All methods are implicitly abstract and final
  • All data is implicitly public, static, and final
  • Interface methods have full headers, but empty
    bodies
  • Create an interface using implements keyword
  • User subclass must define each interface method
  • Ex WorkingDog extends Dog implements Worker

38
(No Transcript)
39
(No Transcript)
40
(No Transcript)
41
Creating and Using Interfaces (continued)
  • WorkingDog tested in DemoWorkingDogs
  • Two WorkingDog objects use the following methods
  • setAnimalName(), getAnimalName() inherited from
    Animal
  • speak( ) inherited from the Dog (which extends
    Animal)
  • setHoursOfTraining( ), getHoursOfTraining( ) in
    WorkingDog
  • work( ) implemented through Worker interface

42
(No Transcript)
43
(No Transcript)
44
Creating Interfaces to Store Related Constants
  • Interfaces contain data that is public, static,
    final
  • Purpose to using constants in interface
  • Provide data that can be reused without
    re-declaration
  • Example PizzaConstants
  • Provides a number of constants for a pizzeria
  • Class implementing interface can use permanent
    values

45
(No Transcript)
46
(No Transcript)
47
Creating and Using Packages
  • Package named collection of classes
  • Examples java.lang, java.util, javax.swing
  • You may create your own packages
  • Conveniently store related classes
  • May be imported by other programmers
  • Mechanics of creating a package
  • Include package statement at beginning of class
    file
  • Statement indicates folder to place class
  • Example package com.course.animals

48
Creating and Using Packages (continued)
  • Mechanics of using a package
  • Use import statement at top of class file
  • Example import com.course.animals.
  • Wild card () provides access to all packaged
    classes
  • Package names should be unique
  • Naming convention
  • Internet domain name in reverse order
  • Ex If domain name is course.com, use com.course

49
Summary
  • Each object of subclass is a superclass object
  • Abstract classes may be extended, not
    instantiated
  • Abstract methods have full headers, empty bodies
  • User subclasses must define abstract methods
  • You can convert subclass objects to superclass
    object

50
Summary (continued)
  • Abstract classes may be declared as reference
  • You may store concrete subclass in abstract
    reference
  • Dynamic method binding select method at run-time
  • Object class is ancestor of every Java class
  • Object class methods may be used or overridden

51
Summary (continued)
  • Interface methods are implicitly public,
    abstract, final
  • Interface data is implicitly public, static,
    final
  • Keyword implements provides access to interface
  • Interface methods must be defined by user class
  • Use packages to conveniently group classes
Write a Comment
User Comments (0)
About PowerShow.com