100% found this document useful (1 vote)
75 views73 pages

ch10 - Upd. With Interfac v2

jgug

Uploaded by

Reema techcampus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
75 views73 pages

ch10 - Upd. With Interfac v2

jgug

Uploaded by

Reema techcampus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 73

Java™ How to Program, 10/e

Late Objects Version

© Copyright 1992-2015 by Pearson Education, Inc. All Rights


Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Polymorphism
 Enables you to “program in the general” rather than “program
in the specific.”
 Polymorphism enables you to write programs that process
objects that share the same superclass as if they were all
objects of the superclass; this can simplify programming.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 With polymorphism, we can design and implement
systems that are easily extensible
 New classes can be added with little or no modification to the
general portions of the program, as long as the new classes are
part of the inheritance hierarchy that the program processes
generically.
 The new classes simply “plug right in.”
 The only parts of a program that must be altered to
accommodate new classes are those that require direct
knowledge of the new classes that we add to the hierarchy.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 In the next example, we aim a superclass reference at a
subclass object.
 Invoking a method on a subclass object via a superclass reference
invokes the subclass functionality
 The type of the referenced object, not the type of the variable,
determines which method is called
 This example demonstrates that an object of a subclass can
be treated as an object of its superclass, enabling various
interesting manipulations.
 A program can create an array of superclass variables that
refer to objects of many subclass types.
 Allowed because each subclass object is an object of its superclass.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 A superclass object cannot be treated as a subclass object,
because a superclass object is not an object of any of its
subclasses.
 The is-a relationship applies only up the hierarchy from a
subclass to its direct (and indirect) superclasses, and not
down the hierarchy.
 The Java compiler does allow the assignment of a
superclass reference to a subclass variable if you explicitly
cast the superclass reference to the subclass type
 A technique known as downcasting that enables a program to invoke
subclass methods that are not in the superclass.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 When a superclass variable contains a reference to a
subclass object, and that reference is used to call a method,
the subclass version of the method is called.
 The Java compiler allows this “crossover” because an object of a
subclass is an object of its superclass (but not vice versa).
 When the compiler encounters a method call made through
a variable, the compiler determines if the method can be
called by checking the variable’s class type.
 If that class contains the proper method declaration (or inherits one),
the call is compiled.
 At execution time, the type of the object to which the
variable refers determines the actual method to use.
 This process is called dynamic binding.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Abstract classes
 Sometimes it’s useful to declare classes for which you never intend
to create objects.
 Used only as superclasses in inheritance hierarchies, so they are
sometimes called abstract superclasses.
 Cannot be used to instantiate objects—abstract classes are
incomplete.
 Subclasses must declare the “missing pieces” to become “concrete”
classes, from which you can instantiate objects; otherwise, these
subclasses, too, will be abstract.
 An abstract class provides a superclass from which other
classes can inherit and thus share a common design.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Classes that can be used to instantiate objects are called
concrete classes.
 Such classes provide implementations of every method
they declare (some of the implementations can be
inherited).
 Abstract superclasses are too general to create real
objects—they specify only what is common among
subclasses.
 Concrete classes provide the specifics that make it
reasonable to instantiate objects.
 Not all hierarchies contain abstract classes.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Programmers often write client code that uses only
abstract superclass types to reduce client code’s
dependencies on a range of subclass types.
 You can write a method with a parameter of an abstract
superclass type.
 When called, such a method can receive an object of any
concrete class that directly or indirectly extends the superclass
specified as the parameter’s type.
 Abstract classes sometimes constitute several levels of
a hierarchy.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 You make a class abstract by declaring it with keyword
abstract.
 An abstract class normally contains one or more abstract
methods.
 An abstract method is an instance method with keyword abstract in
its declaration, as in
public abstract void draw(); // abstract method
 Abstract methods do not provide implementations.
 A class that contains abstract methods must be an abstract class
even if that class contains some concrete (nonabstract) methods.
 Each concrete subclass of an abstract superclass also must
provide concrete implementations of each of the superclass’s
abstract methods.
 Constructors and static methods cannot be declared
abstract.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Cannot instantiate objects of abstract superclasses, but
you can use abstract superclasses to declare variables
 These can hold references to objects of any concrete class
derived from those abstract superclasses.
 We’ll use such variables to manipulate subclass objects
polymorphically.
 Can use abstract superclass names to invoke static
methods declared in those abstract superclasses.
 Polymorphism is particularly effective for

implementing so-called layered software systems.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Use an abstract method and polymorphism to perform payroll
calculations based on the type of inheritance hierarchy headed
by an employee.
 Enhanced employee inheritance hierarchy requirements:
 A company pays its employees on a weekly basis. The
employees are of four types: Salaried employees are paid a
fixed weekly salary regardless of the number of hours
worked, hourly employees are paid by the hour and receive
overtime pay (i.e., 1.5 times their hourly salary rate) for all
hours worked in excess of 40 hours, commission
employees are paid a percentage of their sales and base-
salaried commission employees receive a base salary plus
a percentage of their sales. For the current pay period, the
company has decided to reward salaried-commission
employees by adding 10% to their base salaries. The
company wants you to write a Java application that
performs its payroll calculations polymorphically.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 abstract class Employee represents the general
concept of an employee.
 Subclasses: SalariedEmployee,

CommissionEmployee , HourlyEmployee and


BasePlusCommissionEmployee (an indirect
subclass)
 Fig. 10.2 shows the inheritance hierarchy for our

polymorphic employee-payroll application.


 Abstract class names are italicized in the UML.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Abstract superclass Employee declares the
“interface” to the hierarchy—that is, the set of methods
that a program can invoke on all Employee objects.
 We use the term “interface” here in a general sense to refer to
the various ways programs can communicate with objects of
any Employee subclass.
 Each employee has a first name, a last name and a
social security number defined in abstract superclass
Employee.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Class Employee (Fig. 10.4) provides methods earnings
and toString, in addition to the get and set methods that
manipulate Employee’s instance variables.
 An earnings method applies to all employees, but each
earnings calculation depends on the employee’s class.
 An abstract method—there is not enough information to
determine what amount earnings should return.
 Each subclass overrides earnings with an appropriate
implementation.
 Iterate through the array of Employees and call method
earnings for each Employee subclass object.
 Method calls processed polymorphically.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 The diagram in Fig. 10.3 shows each of the five classes
in the hierarchy down the left side and methods
earnings and toString across the top.
 For each class, the diagram shows the desired results of
each method.
 Declaring the earnings method abstract
indicates that each concrete subclass must provide an
appropriate earnings implementation and that a
program will be able to use superclass Employee
variables to invoke method earnings
polymorphically for any type of Employee.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
10.5.2 Concrete Subclass
SalariedEmployee

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
10.5.3 Concrete Subclass
HourlyEmployee

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
10.5.4 Concrete Subclass
CommissionEmployee

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
10.5.5 Indirect Concrete
Subclass
BasePlusCommissionEmployee

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Fig. 10.9 creates an object of each of the four concrete.
 Manipulates these objects nonpolymorphically, via variables of
each object’s own type, then polymorphically, using an array
of Employee variables.
 While processing the objects polymorphically, the
program increases the base salary of each
BasePlusCommissionEmployee by 10%
 Requires determining the object’s type at execution time.
 Finally, the program polymorphically determines and
outputs the type of each object in the Employee array.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 All calls to method toString and earnings are
resolved at execution time, based on the type of the
object to which currentEmployee refers.
 Known as dynamic binding or late binding.
 Java decides which class’s toString method to call at
execution time rather than at compile time
 A superclass reference can be used to invoke only
methods of the superclass—the subclass method
implementations are invoked polymorphically.
 Attempting to invoke a subclass-only method directly
on a superclass reference is a compilation error.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 Every object knows its own class and can access this
information through the getClass method, which all
classes inherit from class Object.
 The getClass method returns an object of type Class
(from package java.lang), which contains information
about the object’s type, including its class name.
 The result of the getClass call is used to invoke getName
to get the object’s class name.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 There are three proper ways to assign superclass and
subclass references to variables of superclass and subclass
types.
 Assigning a superclass reference to a superclass variable is
straightforward.
 Assigning a subclass reference to a subclass variable is
straightforward.
 Assigning a subclass reference to a superclass variable is
safe, because the subclass object is an object of its
superclass.
 The superclass variable can be used to refer only to superclass
members.
 If this code refers to subclass-only mem-bers through the superclass
variable, the compiler reports errors.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 A final method in a superclass cannot be overridden
in a subclass.
 Methods that are declared private are implicitly final,
because it’s not possible to override them in a subclass.
 Methods that are declared static are implicitly final.
 A final method’s declaration can never change, so all
subclasses use the same method implementation, and calls to
final methods are resolved at compile time—this is known
as static binding.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 A final class cannot be extended to create a subclass.
 All methods in a final class are implicitly final.
 Class String is an example of a final class.
 If you were allowed to create a subclass of String, objects of
that subclass could be used wherever Strings are expected.
 Since class String cannot be extended, programs that use
Strings can rely on the functionality of String objects as
specified in the Java API.
 Making the class final also prevents programmers from
creating subclasses that might bypass security restrictions.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
 interfaces are particularly useful for assigning common
functionality to possibly unrelated classes.
 This allows objects of these classes to be processed

polymorphically—objects of classes that implement the


same interface can respond to all of the interface
method calls.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 Once a class implements an interface, all objects of that
class have an is-a relationship with the interface type,
and all objects of the class are guaranteed to provide
the functionality described by the interface.
 This is true of all subclasses of that class as well.
 Interfaces are particularly useful for assigning common

functionality to possibly unrelated classes.


 Allows objects of unrelated classes to be processed
polymorphically—objects of classes that implement the same
interface can respond to all of the interface method calls.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
 An interface is a classlike construct that contains only
constants and abstract methods.
 In many ways, an interface is similar to an abstract class,

but the intent of an interface is to specify common


behavior for objects of related classes or unrelated classes.

67 67
What is the common feature?????

68
To distinguish an interface from a class, Java uses the following syntax to define an
interface:

public interface InterfaceName {

constant declarations;

abstract method signatures;

04/28/20
 The interface body contains method declarations for ALL the
methods included in the interface.
 A method declaration within an interface is followed by a
semicolon (;)
 An interface does not provide implementations for the
methods declared within it.

classes & Interface 70


04/28/20
Abstract
71
72
Output:

73

You might also like