Chapter Five - Classes and Inheritance

Download Report

Transcript Chapter Five - Classes and Inheritance

George Blank
University Lecturer
CS 602
Java and the Web
Object Oriented Software Development
Using Java
Chapter 5
Chapter Five Objectives
• Chapter Five is largely about the use of
object-oriented technology in Java,
particularly inheritance, polymorphism,
overloading, overriding, hiding, subtypes and
casting.
Java’s Uniqueness
• Chapter Four gave a basic introduction to the Java
language that might mislead you into thinking that
programming in Java is much like programming in other
computer languages. Like C, C++, Pascal and most other
languages, Java has a relatively small set of commands,
primitives, structures, and operations, with a simple syntax.
Calling it a “language” is misleading. It is nowhere near as
complicated as English, with irregular syntax and over
500,000 words.
The Class Libraries
• However, Java is unlike most other computer
languages in at least one way. Most Java
programming involves using predefined functionality
in the extensive class library, not writing code from
scratch. This is not new—Microsoft Windows
programmers have used the Microsoft Foundation
Class libraries for years. But Java uses predefined
functions far more than its predecessor languages.
Using Libraries
• Java programmers need to learn how to search the
class libraries, identify classes they need and the
class methods, as well as figuring out how to use
them.
• To do this you need to understand object-oriented
principles, as well as how to implement interfaces
and extend classes. The basics of these topics are
in this lecture.
Searching the Libraries
• To search the class libraries, a good starting place is
java.sun.com. Any link will quickly be obsolete as
new versions of Java are released, but an example of
the libraries was at
http://java.sun.com/j2se/1.4.2/docs/api/
• Chapter Five introduces the javadoc utility, which
catalogs the functionality that you acquire by using
classes from the Java libraries.
Object model concepts
– Four concepts are critical to understanding object models:
•
•
•
•
Data abstraction
Polymorphism
Inheritance
Encapsulation
– These can be remembered with the acronym “A Pie,” for Abstraction,
Polymorphism, Inheritance, Encapsulation.
Data Abstraction
• Data abstraction is the process of distilling data down
to its essentials. In an object schema, the abstract data
model is implemented as a tree. The following figure
in the next slide shows such a tree structure for a
genealogical database.
Data Abstraction(Cont)
A tree structure for a genealogical database:
Person
Student
Graduate
Student
Undergraduate
Student
Employee
Faculty
Staff
Objects
• Key uses of Abstraction in object-oriented
programming are the concepts of classes and
objects. Classes are abstractions of real
world concepts. An object is an instantiation
of a class. Note that a software object that
represents a person is a very different thing
than a real person. (The person would not fit
in the computer!)
Polymorphism
• Distinctive objects respond distinctly to the
same message.
• Polymorphism mainly requires the ability for
different objects to have methods with the
same name, but different parameters.
Overloading
• Polymorphism in Java is largely
accomplished with overloading. See section
5.1 of the text for an explanation of
overloading. See also the discussion of subtypes and polymorphism in section 5.2.2.
Rule of Overloading
• Two methods or constructors can be
overloaded if they have different numbers of
parameters or the same number of
parameters for different types. No two
methods or constructors in the same class
may have identical signatures.
Operator Overloading
• Java operators are overloaded on primitive
types. As an example, the arithmetic
operators are overloaded on all numeric
types. Operators + and += are also
overloaded on class String for concatenation.
This allows computations to take place in
naturally within the understood meaning of
the operations.
Common Sense Overloading
There are only two situations where overloading makes
sense:
1. When there is a general, nondiscriminative
description of the functionality that fits all of the
overloaded methods. (or)
2. When all the overloaded methods offer the same
functionality, but some of them have default
arguments.
Overloading shoud be avoided in other situations.
Inheritance
• Inheritance in the object model is a means of defining
one class in terms of another. This is common usage for
most of us. For example, a student is a type of person.
There are certain characteristics that are true for all
persons, yet there are specific characteristics for
students.
• Section 5.2 is mostly about inheritance.
Inheritance(Cont)
• In following example, a Student is a type of Person.
Likewise, a Employee is a type of Person. Both
Student and Employee inherit all the attributes and
methods of Person. Student has a locally defined
student ID attribute. Employee has a locally defined
employee ID attribute.
• If you look at a Student object, you see attributes of
name, date of birth and student ID.
Inheritance(Cont)
Person
Name
Birthday
Student
Employee
Student ID
Employee
ID
Constructors in Extended Classes
Initializing an extended class takes place in two
phases.
1. Inherited fields from the superclass are
initialized by calling constructors from that
class
2. Constructors of the extended class must
initialize fields declared in that class.
Dynamic Binding
• Method invocations for object oriented
languages are bound at run time, not at
compile time. This is called Dynamic Binding.
Dynamic Binding is required because objects
can be instantiated at any time, not just when
the code itself is initialized. Dynamic binding
has the benefit of additional flexibility.
Definitions
• Variable –a storage location with an
associated type
• Object –an instance of a class
• Subtypes –properties of a subclass variable
that may differ from the properties of the
superclass variables
Subclasses
• A subclass can extend a superclass by
inheriting features and adding more features.
It can also specialize a superclass for a
particular purpose. Every instance of a
subclass is an instance of the superclass, but
the reverse is not true. Some features are
available in the subclass but not in the
superclass.
Polymorphic Assignment
• In static programming languages like c, the
left and right hand sides of an assignment
must be the same type. In object oriented
languages, polymorphic assignment is
allowed. The right hand side may be either
the same type as the left hand side or a
subtype of it.
Downcasting
• Although you can assign a subtype to a
supertype variable, if you are going to assign
the result to a variable of the subtype, you
must downcast it. This is because the
subtype does not know that the supertype
contains compatible information. Text
example:
• student3 = (Graduate) student2
Java Array types
• All Java arrays are objects, subtypes of
object. If a class or interface is a subtype of
another class or object, then an array of the
subclass is a subtype of an array of the
superclass. You would need to downcast
array members under the same conditions
that would require downcasts if it were not a
member of an array.
Overriding Methods
• A key idea in inheritance is overriding methods from a
superclass in a subclass to make them more specific.
(section 5.2.3)
• Overriding refers to the introduction of an instance
method in a subclass that has the same name,
signature and return type of a method in the
superclass. An overridden method replaces the
implementation of the method in the superclass for
that subclass.
Overriding vs. Overloading
• Overloading refers to methods of the same
class with the same name but different
signatures or return types.
• Overriding refers to methods of different
classes with the same name, signatures and
return types. The overriden method appears
in a subclass.
Final Methods
• A method that is declared as final cannot be
overridden. Such methods often collaborate with
other methods or classes and need to be protected
from possible failures that might be caused by
overriding. Final methods also allow the Java
compiler and JVM to optimize the byte code in ways
that would not be possible if it had to remain flexible
and available for overriding.
Implementing Interfaces
• Interfaces declare features but provide no
implementation. Classes that implement an
interface should provide implementations for
all features or methods declared in the
interface.
• If a class implements multiple interfaces, it
should override all of the abstract methods
declared in all of the interfaces.
Multiple Inheritance
• A key problem for inheritance in objectorientated languages is multiple inheritance.
The text explains the concept in terms of a
student employee class that inherits
characteristics from two superclasses,
student and employee. Java does not allow
multiple inheritance, but simulates it with
interfaces.
Interface Name Collisions
When methods from multiple interfaces have the same name in an
implementing class:
• Methods with different signatures are overloaded
• With same signature and return type, they are considered to be
the same method
• With the same signature and different return types, they will
generate a compilation error
• With same signature and return type bu different exceptions, they
are considered to be the same method and the throws list
contains all the exceptions.
Encapsulation
• Encapsulation is the object model concept of
including processing or behavior with the object
instances defined by the class. Encapsulation
allows code and data to be packaged together.
• The definition of methods for a class is an
integral part of encapsulation. A method is
programming code that performs the behavior an
object instance can exhibit.
Information Hiding
• Information hiding is an important part of
encapsulation. The idea is that if the inputs
and outputs of an object are fully specified,
the way that it is accomplished does not need
to be visible. However, as explained in
section 5.4, certain forms of hiding are
dangerous, as they can lead to unexplained
inconsistencies in output.
Hiding
• Hiding refers to introducing a field instance or class
or a class method in a subtype with the same name
as a field or class in the superclass.
• Instance Methods cannot be hidden. They can only
be overridden by a method with the same signature
and return type.
• Class methods and fields can only be hidden. They
can be hidden by class methods or fields of any
signature or type.
Animation
• The final section of chapter five, section 5.5 on
Animation Applets, is really a separate topic.
• However, animation is very commonly used,
especially on Web Sites, so it is important to
understand.
• Note that one of the most common uses of
animation is extremely obnoxious—to call
attention to advertisements.
Summary
• The author does an excellent job of calling
attention to key points in the summary of
each chapter. Make certain that you
understand each concept mentioned in the
summary.
Review Questions
• How does Java’s extensive class library represent a
different approach to software development from
earlier languages?
• What is the relationship between encapsulation and
information hiding?
• When does it make sense to overload methods?
• Does inheritance represent an “is a” relation?
Bibliography
• Jia, Xiaoping, Object Oriented Software
Development Using Java. Addison Wesley,
2003