Bobby D. Gerardo - PowerPoint PPT Presentation

About This Presentation
Title:

Bobby D. Gerardo

Description:

Casting in a Class Hierarchy. Passing Objects as Arguments. 34. Polymorphism ... Polymorphism and Late Binding of Method Calls ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 48
Provided by: Bob7158
Category:
Tags: bobby | calls | casting | gerardo

less

Transcript and Presenter's Notes

Title: Bobby D. Gerardo


1
Programming and Problem Solving Using Java
  • Bobby D. Gerardo
  • Kunsan National University
  • Summer 2004

2
Chapter 6Class Hierarchies, Inheritance, and
Interfaces
3
Review
4
(No Transcript)
5
(No Transcript)
6
(No Transcript)
7
(No Transcript)
8
(No Transcript)
9
(No Transcript)
10
(No Transcript)
11
(No Transcript)
12
(No Transcript)
13
Sorting an Array
  • Method
  • IntArray.selectionSort(scores)

14
(No Transcript)
15
Array Search
  • Search method
  • i.e. int index IntArray.search(scores, 100)

16
(No Transcript)
17
(No Transcript)
18
(No Transcript)
19
Chapter 6Class Hierarchies, Inheritance, and
Interfaces
20
Class Hierarchies, Inheritance, and Interfaces
  • Throughout the book we followed object oriented
    design techniques by using abstraction modeling
    real life entities by retaining only essential
    information.
  • We also use encapsulation clustering data and
    methods together into classes, providing safe and
    reliable boundaries.
  • This chapter enhances our capability to use
    object oriented design by introducing key
    features such as hierarchies, inheritance, and
    interfaces.

21
Class Hierarchies and Inheritance
  • Code reuse is very important concept in OOP
  • If you build new applications using code that has
    been written and tested, you are more likely to
    develop programs are error free.
  • Extending the existing class
  • Subclass with additional data field
  • Objects also inherit the data fields and methods
    of the original class called superclass.

22
Class Hierarchies and Inheritance (cont)
  • Hierarchies and inheritance is a rich concept
    that often appear in science
  • Inheritance, in combination with hierarchical
    organizations, allows you to capture the idea
    that one thing maybe a refinement or extension of
    another.

23
is a versus has a Relationships
  • Java allows you to capture both the inheritance
    (is a) relationship and the has a relationship.
    For example, a car class might be declared as
    follows
  • public class Car extends Vehicle
  • Wheels w new Wheels4 //cars have 4
    wheels
  • The keyword extends specifies that Car is a
    subclass of Vehicle.

24
Case Study 1 A Hierarchy of Employee Classes
25
Case Study 1 A hierarchy of Employee Classes
  • Problem You have a class called NewEmployee
    that stores basic data about an employee name,
    social security number, job title, address, phone
    number, age, starting year of employment, and
    total pay to date. Besides accessor and modifier
    methods, this class has a method to compute the
    employees numbers of years with the company, one
    to compute the number of years to retirement, and
    one to update the total pay to date. It also has
    a toString() method.

26
Case Study 1 A hierarchy of Employee Classes
(cont)
  • Now suppose you decide to differentiate between
    two kinds of employees salaried and hourly
    employees. Hourly employees are paid an hourly
    rate and their weekly pay varies depending on how
    many hours they work. Salaried employees, on the
    other hand, have a fixed annual salary and their
    weekly pay rate is the same regardless of how
    many hours of work.

27
Analysis and Design
  • Fig. 6.2 shows a description of class
    NewEmployee. In addition to the methods described
    above, we added an equals() method that compares
    two NewEmployee objects.

Fig. 6.2 Class NewEmployee
Data Fields Attributes String
name Name String socSecNum Social security
number String jobTitle Job Title String
address Address String phoneNumber Phone
Number int age Age int startYear Starting
year of employment double totalPay Total pay to
date Methods Behavior . .
28
Implementation
  • Fig. 6.5 shows the NewEmployee class. The
    declaration statements for the type String data
    fields initialize them to empty string ( ). The
    numeric data fields are initialized to zero (the
    default value).
  • See Fig. 6.5 in the book.

29
Testing
  • Fig. 6.9 (in the book) shows a class that tests
    all three employees classes.
  • Fig. 6.10 (in the book) shows the console window
    after the class executes.

30
Operations in Class Hierarchy
  • Fig. 6.11 shows the class hierarchy for the
    employee classes developed in the last section.

Fig. 6.11 Class hierarchy
31
Operations in Class Hierarchy (2)
  • Fig. 6.12 Data fields and methods for class
    NewEmployee
  • Fig. 6.13 Data fields and methods for class
    SalaryEmployee
  • Fig. 6.14 Data fields and methods for class
    NewEmployeeHourly

32
Operations in Class Hierarchy (3)
  • Method overloading When class has multiple
    methods with the same name.
  • A method signature consists of the method name
    and its parameter lists
  • Method overriding The fact that a method
    defined in a subclass blocks the execution of a
    method with the same signature defined in its
    superclass is called method overriding.

33
Operations in Class Hierarchy (4)
  • Protected visibility
  • Shadowing data fields
  • Misnomer of Superclass and Subclass
  • Assignment in a Class Hierarchy
  • Casting in a Class Hierarchy
  • Passing Objects as Arguments

34
Polymorphism
  • We will study how we can store objects of
    different types in a single array and process
    them in a uniform way using a programming
    language feature called polymorphism.

35
Polymorphism and Late Binding of Method Calls
  • The feature that enables a particular method call
    to have behavior at different times is called
    polymorphism, which means multiple forms.
  • The feature that enables a programming language
    to select a code fragment to execute at run-time
    is called dynamic binding.

36
Case Study 2 A Company with an Array of
Employees of Different Types
37
Case Study 2 A Company with an Array of
Employees of Different Types
  • Problem In section 5.5, we showed how to
    process an array of employees in which each
    element was the same type (Employees). Now our
    company has several employees of different types
    (NewEmployee, HourlyEmployee, SalaryEmployee). We
    want to store them in an array and compute the
    total company payroll.

38
Interfaces
  • An interface is used to specify a set of
    requirements that is imposed on a collection of
    classes.
  • Classes that implement an interface are
    guaranteed to have certain functionality
    (behavior) and also inherit any constants that
    are defined in the interface.
  • Form public interface InterfaceName
  • abstract method headings
  • constant declaration

39
Declaring Constants
  • We can declare constants in an interface. These
    constants would be inherited by classes that
    implement the interface.
  • public static final double DEDUCT_PCT 5.5
  • In a class Payable to declare DEDUCT_PCT as a
    constant with a value of 5.5

40
The Comparable Interface and Method compareTo
  • We can declare constants in an interface. One of
    these is the Comparable interface, ensures that
    the programmer will be able to compare objects in
    each class that implements it.
  • All classes that implement the Comparable
    interface must have a compareTo method
  • See Fig. 6.22 and Table 6.1

41
Abstract Classes
  • Sometimes we need to new create class, called
    abstract class that should be instantiated and
    whose sole purpose is to serve as the parent
    (superclass) of a group of classes.
  • Form public abstract class ClassName
  • data field declaration
  • abstract method headings
  • method definitions

42
Case Study 3 Areas of Geometric Figures
43
Case Study 3 Areas of Geometric Figures
  • Problem Consider the problem of finding the
    total area of a collection of geometric figures.
    This problem has many practical applications. For
    example in calculating the paint needed for a
    house, a painter might visualize the interior
    walls as a series of rectangular and triangular
    shapes. In calculating the amount of fertilizer
    to buy, a landscaper might represent a garden as
    a collection of circle and triangular shapes.

44
Case Study 4 Drawing of Geometric Figures
45
Case Study 4 Drawing of Geometric Figures
  • Problem We would like to draw geometric figures
    on the screen. Each figure object will be one of
    our standard shapes and can appear anywhere on
    the screen with any interior color or border
    color.

46
Programming Projects
  • Prog. Project 1, P. 436
  • Prog. Project 2, P. 436
  • Prog. Project 3, P. 436
  • Prog. Project 4, P. 436

47
? End of Chapter
Write a Comment
User Comments (0)
About PowerShow.com