Title: Object-Oriented Application Development (Part II)
1Object-Oriented Application Development (Part II)
- Dr.Wanwipa Titthasiri
- Department of Computer Science
2Objectives
- In this chapter you will
- Study the concepts of inheritance and
polymorphism - Learn more about how Javas predefined classes
use inheritance and polymorphism - Explore how inheritance and polymorphism are used
in programmer-defined classes - Learn the importance of overloading, overriding,
and dynamic binding - Develop skills in using inheritance and
polymorphism in object-oriented applications
3Inheritance
- Inheritance is a relationship in which one class
has access to public members of another - Java has predefined classes that inherit
attributes and behaviors from other classes - Programmer-defined classes can inherit from
predefined or programmer-defined classes
4Inheritance and Javas Predefined Classes
- Inheritance hierarchy Classes farther down
inherit from classes farther up - Superclass A class that is at a higher level
- Subclass A class at a lower level
- Direct superclass A class immediately above a
given class - Direct subclass A class immediately below a
given class
5Inheritance and Javas Predefined Classes
(continued)
6Inheritance and Javas Predefined Classes
(continued)
- Subclasses have access to public and protected
members of their superclasses - Is-a relationship A subclass has an is-a
relationship with its superclass - Is-a relationships are also called generalization
relationships - A superclass is more general that its subclasses
- Example Number is more general than double
7Inheritance and Programmer-Defined Classes
- Recall MusicWorldApp.java
- CDOrder has a LineItem and LineItem has a CD
object - Does not involve inheritance
- Inheritance is needed when a general type can be
broken down into specific types - Example The information system in a university
must model different types of people - A person is a general type of object
- A Person class serves as a superclass
8Inheritance and Programmer-Defined Classes
(continued)
9Inheritance and Programmer-Defined Classes
(continued)
- A student is a person, so Student is a subclass
of Person - All attributes of Person are attributes of
Student - A student is a more specialized instance of a
person - Students have a college class (junior, senior
etc.) - Faculty and staff do not have a college class
- Only methods unique to Student are defined
10Inheritance and Programmer-Defined Classes
(continued)
11Inheritance and Programmer-Defined Classes
(continued)
- The keyword extends means inherits from
- The class Student inherits public and protected
members of Person - The keyword super calls the superclass
constructor - When a Student object is created, a Person object
is also created - The keyword this refers to the object whose
method or constructor is being called
12Inheritance and Programmer-Defined Classes
(continued)
- The object-defining classes Person and Student
cannot be executed on their own - An application class is required to execute tasks
required by the application - Application classes contain a main method
- The JVM looks up the inheritance hierarchy for
methods if it doesnt find it in the calling class
13Inheritance and Programmer-Defined Classes
(continued)
14Inheritance and Programmer-Defined Classes
(continued)
15Apply the Concept
- Develop a simple banking application
- Checking accounts have a monthly service fee
- Savings accounts pay interest monthly
- Three tasks
- Identify all object-defining classes required
- Sketch a class diagram
- Plan the application class with a flowchart
- Nouns are modeled by objects
- Bank account, checking account, savings account
- Verbs are modeled with methods
- Assess fee, pay interest
16Apply the Concept (continued)
- Create a class diagram
- Focus on customer, bank account, checking
account, and savings account - Checking and savings accounts are specialized
bank accounts - Class name in the top section, instance variables
in the middle, methods in the bottom - Composition is represented by solid diamonds and
inheritance is represented by open arrows - Application class is represented with a flowchart
17Apply the Concept (continued)
18Apply the Concept (continued)
- Customer.java defines Customer objects
- SSN, name, checking account, savings account
- Constructor, accessor methods for accounts
- BankAccount.java defines BankAccount objects,
members common to all accounts - Account number, balance, accessor methods,
toString - CheckingAccount.java and SavingsAccount.java
nearly identical - Checking accounts incur a service charge, savings
accrue interest
19Apply the Concept (continued)
- Both CheckingAccount and SavingsAccount class
definitions include extends BankAccount - Constructors call the BankAccount constructor
first using the keyword super - BankingApp.java is procedural with 5 parts
- Preparation, declaration, input, processing,
output - All individual classes must be in the same
directory, and be compiled individually
20Polymorphism
- Polymorphism refers to the ability of a single
method name to be used in different ways - Different kinds of polymorphism
- Overloading
- Overriding
- Static binding
- Dynamic binding
21Overloading
- Overloading occurs when a single method name is
used more than once - In the same class
- Different parameter lists (different signatures)
- Example The indexOf method in the String class
- Parameter lists can differ in number, type or
order of arguments
22Overloading (continued)
23Overriding
- One method name is used in different classes
within an inheritance hierarchy (same signature) - Each of the methods may do completely different
things - A method in a subclass may override a method in
its superclass - Example The toString method in Object is
overridden in the BankAccount class - If a method is not found in the named class, Java
looks up the hierarchy
24Static (Early) and Dynamic (Late) Binding
- When overriding is used, the Java compiler
attempts to bind a method call to its method
definition - Binding at compile time is known as static
(early) binding - Dynamic binding means the method call is not
bound to the definition until run time
25Static (Early) and Dynamic (Late) Binding
(continued)
- Step 1 ObjectA is created from ClassA
- Step 2 ObjectB is created from ClassB
- Assume ClassB is a subclass of ClassA
- ClassA contains a method called methodA
- ObjectA.methodA() the compiler checks ObjectA for
methodA static binding - Step 3 ObjectB ObjectA
- Java thinks ObjectA is an instance of ClassB
- Step 4 ObjectA tries to call methodA
- Java does not check ClassB for methodA
26Static (Early) and Dynamic (Late) Binding
(continued)
27Apply the Concept
- Recall that a bank offers checking (with a
service charge) and savings (with interest) - For simplicity, three classes
- PolySavingsAccount is a subclass of
PolyBankAccount - PolyBankingApp is a driver
- PolyBankAccount has a payInterest method but
cannot pay interest
28Apply the Concept (continued)
29Apply the Concept (continued)
30Apply the Concept (continued)
- PolyBankAccount has overloaded constructors
- The method payInterest is called on an instance
of PolySavingsAccount - Overrides payInterest in PolyBankAccount
- Dynamic binding (Lines 24 29)
- account3 created from PolyBankAccount
- account3 now refers to PolySavingsAccount
- account3 calls methods from PolySavingsAccount
- The payInterest method is not bound to account3
until run time
31Apply the Concept (continued)
32Summary
- A superclass is higher in the hierarchy and a
subclass is lower - The inheritance relationship is an is-a
relationship or generalization relationship - A subclass inherits public and protected members
of its superclass - Polymorphism means a single method can have many
implementations depending on its signature and
location - Overloading and overriding (with static and early
binding) are forms of polymorphism