Java Programming: Guided Learning with Early Objects - PowerPoint PPT Presentation

Loading...

PPT – Java Programming: Guided Learning with Early Objects PowerPoint presentation | free to download - id: 7d6d50-YjZhZ



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Java Programming: Guided Learning with Early Objects

Description:

Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 51
Provided by: wikis1911
Learn more at: http://mcst-is-cs.wikispaces.com
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Java Programming: Guided Learning with Early Objects


1
Java Programming Guided Learning with Early
Objects
  • Chapter 9
  • Inheritance and Polymorphism

2
Objectives
  • Learn about inheritance
  • Learn about subclasses and superclasses
  • Explore how to override the methods of a
    superclass
  • Examine how constructors of superclasses and
    subclasses work together

3
Objectives (continued)
  • Become familiar with protected members of a class
  • Learn about polymorphism
  • Examine abstract classes
  • Become familiar with interfaces
  • Learn about composition (aggregation)

4
Inheritance
  • Mechanism that allows class definition to be
    extended
  • No changes to existing class
  • Implies is-a relationship
  • Subclass new class derived from existing class
  • Also called derived class
  • Superclass existing class that other classes are
    derived from
  • Also called base class

5
Figure 9-1 Inheritance hierarchy
6
Inheritance (continued)
  • Inheritance has a hierarchical structure
  • Syntax
  • modifiers class ClassName extends BaseClass
    modifiers
  • memberList
  • private members of the superclass cannot be
    accessed directly by the subclass

7
Inheritance (continued)
  • Subclass directly accesses public members of
    superclass
  • Subclass can include additional members
  • Subclass can override public methods of
    superclass
  • Subclass can shadow public variables of
    superclass
  • Members of superclass are also members of subclass

8
Inheritance (continued)
  • Single inheritance subclass derived from a
    single superclass
  • Multiple inheritance subclass derived from more
    than one superclass
  • Java supports only single inheritance
  • Constructor of subclass cannot directly access
    private data members of the superclass

9
Using Methods of the Superclass in a Subclass
  • Data members of subclass include its own data
    members and members of superclass
  • Overriding subclass method may have same
    signature as method in superclass
  • Also called redefining a method
  • Corresponding method in subclass must have same
    name and formal parameters
  • Overloading same name, different formal
    parameter list

10
Figure 9-2 UML class diagram of the class
Rectangle
11
Figure 9-3 UML class diagram of the class Box and
the inheritance hierarchy
12
Using Methods of the Superclass in a Subclass
(continued)
  • Write method definitions of a subclass to specify
    a call to a public method of superclass
  • Subclass overrides public method
  • Must call method of using super, dot operator,
    method name, parameter list
  • Subclass does not override public method
  • Call public method using method name, parameter
    list

13
Constructors of the Superclass and Subclass
  • Subclass may have its own private data members
  • Should have its own constructors
  • Constructors initialize instance variables

14
Constructors of the Superclass and Subclass
(continued)
  • Subclass inherits instance variables of
    superclass
  • Cannot directly access private instance variables
    of superclass
  • Cannot directly access private methods of
    superclass

15
Constructors of the Superclass and Subclass
(continued)
  • Subclass constructors should initialize only the
    instance variables of the subclass
  • Call superclass constructor to instantiate
    superclass instance variables
  • Reserved word super calls superclass constructor
  • Superclass constructor parameters passed to super

16
Figure 9-4 Objects myRectangle and myBox
17
Protected Members of a Class
  • private class members cannot be accessed directly
    outside the class
  • Subclass cannot access private members of
    superclass directly
  • Making private members public violates
    encapsulation

18
Protected Members of a Class (continued)
  • protected members can be accessed directly by
    subclasses
  • UML diagram means protected

19
Figure 9-6 UML class diagram of the class
BaseClass
20
Figure 9-7 UML class diagram of the class
DerivedClass and the inheritance hierarchy
21
class Object
  • Recall that class Clock included the method
    toString
  • Every Java class includes method toString
  • Method toString comes from Java class Object
  • Defining a class without using extends derives
    the class from Object
  • class Object is the superclass of every Java class

22
Table 9-1 A Constructor and Some Methods of the
class Object
23
class Object (continued)
  • Every public member of class Object can be
    overridden or invoked by every object
  • Method toString is a public member of every Java
    class
  • Default definition returns class name and hash
    code of object as a string
  • Method equals determines whether two objects are
    aliases
  • Method clone makes shallow copy of an object

24
Java Stream Classes
  • Recall that class Scanner is used for inputting
    data from standard input device
  • I/O performed using FileReader and PrintWriter
  • class InputStreamReader derived from class Reader
  • class FileReader derived from class
    InputStreamReader
  • class PrintWriter derived from class Writer

25
Figure 9-8 Java stream classes hierarchy
26
Polymorphism via Inheritance
  • Methods in different classes may have same name
    and same formal parameter list
  • Reference variable of a class can refer to object
    of its own class or a subclass
  • Reference variable can invoke method of its own
    class or of its subclasses

27
Polymorphism via Inheritance (continued)
  • Binding associating a method definition with its
    invocation
  • Early binding methods definition associated
    with its invocation at compile time
  • Late binding methods definition associated with
    its invocation at run time

28
Polymorphism via Inheritance (continued)
  • Java uses late binding for all methods
  • Polymorphism associating multiple meanings with
    the same method name
  • Polymorphic reference variables can point to own
    class or objects of subclasses

29
Operator instanceof
  • Object of subclass can be considered object of
    superclass
  • Using appropriate cast operator can treat object
    of superclass as an object of subclass
  • Operator instanceof determines class type of an
    object reference

30
Abstract Methods and Classes
  • Abstract method has only heading but no body
  • Heading contains reserved word abstract
  • Ends with a semicolon
  • Example
  • public void abstract print ()

31
Abstract Methods and Classes (continued)
  • Abstract class declared with reserved word
    abstract in heading
  • Abstract class contains instance variables,
    constructors, and non abstract methods
  • Abstract class contains one or more abstract
    methods

32
Abstract Methods and Classes (continued)
  • If class contains abstract method, class must be
    declared abstract
  • Cannot instantiate an object of an abstract class
  • Can declare a reference variable of abstract
    class type

33
Abstract Methods and Classes (continued)
  • Can instantiate an object of a subclass of an
    abstract class
  • Must define all abstract methods of the
    superclass
  • Used as superclasses from which other subclasses
    can be derived
  • Placeholders to store common members

34
Figure 9-9 Inheritance hierarchy of banking
accounts
35
Interfaces
  • Recall that class ActionListener is an interface
  • Other interfaces
  • interface WindowListener
  • interface MouseListener
  • Java does not support multiple inheritance
  • Class can extend definition of only one class
  • Java program may contain variety of GUI
    components
  • Generate many events

36
Interfaces (continued)
  • Java program can implement many interfaces
  • Inner class implements appropriate interface
  • Interface contains only abstract methods and
    named constants
  • Interfaces defined using reserved word interface

37
Polymorphism via Interfaces
  • Interfaces allow GUI programs to handle more than
    one type of event
  • Events handled by separate interfaces
  • Interface used in implementation of abstract data
    types
  • Hides implementation details from user

38
Polymorphism via Interfaces (continued)
  • Can create polymorphic references using
    interfaces
  • Interface name as type of reference variable
  • Cannot create object of an interface
  • Interface contains only method headings and named
    constants

39
Polymorphism via Interfaces (continued)
  • Interface name can declare a parameter to a
    method
  • Any reference variable of class that implements
    an interface can be passed as parameter

40
Composition (Aggregation)
  • Composition one or more members of a class are
    objects of one or more other classes
  • Has-a relationship
  • Example
  • Every person has a birthday.

41
Figure 9-17 UML class diagram of the class Date
42
Figure 9-18 UML class diagram of the class
PersonalInfo and composition (aggregation)
43
Summary
  • Inheritance and composition are meaningful ways
    to relate two or more classes
  • Inheritance is an is-a relationship
  • Composition (aggregation) is a has-a
    relationship
  • Single inheritance subclass derived from only
    one superclass
  • Multiple inheritance subclass derived from one
    or more superclasses

44
Summary (continued)
  • Java allows only single inheritance
  • private members of a superclass are private to
    the superclass
  • Subclass cannot access them directly
  • Subclass can override methods of a superclass
  • Available only to objects of subclass

45
Summary (continued)
  • If subclass overrides public method of superclass
  • Specify a call to public method of superclass
  • Use reserved word super, dot operator, method
    name, and parameter list
  • Dot operator and method name not required for
    constructor
  • If subclass does not override a public method of
    superclass
  • Specify method call using name of method,
    parameter list

46
Summary (continued)
  • Call to constructor of superclass uses reserved
    word super with appropriate parameter list
  • Call to super must be first statement
  • Modifier protected gives subclass direct access
    to members
  • protected members may be accessed by any class in
    the same package
  • Classes not explicitly derived from other classes
    derived from class Object

47
Summary (continued)
  • class Object directly or indirectly becomes
    superclass of every class in Java
  • Reference variable of superclass can point to
    object of subclass type
  • Several methods can have same name and same
    formal parameter list
  • Reference variable of a class can refer to object
    of its own class or object of its subclass

48
Summary (continued)
  • Early binding methods definition associated
    with its invocation at compile time
  • Late binding methods definition associated with
    invocation at run time
  • Java uses late binding for all methods
  • Few exceptions
  • Polymorphism assign multiple meanings to same
    method
  • Implemented using late binding

49
Summary (continued)
  • Cannot automatically make reference variable of
    subclass type point to object of superclass
  • Abstract method has only the heading with
    reserved word abstract
  • Abstract class declared with reserved word
    abstract in its heading
  • Contains instance variables, constructors, and
    non abstract methods
  • Can contain abstract methods

50
Summary (continued)
  • If a class contains abstract method, it must be
    declared abstract
  • Cannot instantiate an object of an abstract class
  • Can instantiate object of subclass if you define
    all abstract methods
  • Interface contains only abstract methods, named
    constants
  • Class may implement more than one interface
About PowerShow.com