Title: Creating Classes from Other Classes
1Creating Classes from Other Classes
2Chapter Contents
- Inheritance
- Invoking constructors from within constructors
- Private fields and methods of the base class
- Overriding, overloading methods
- Protected access
- Multiple inheritance
- Type compatibility and base classes
- The class Object
- Abstract classes and methods
- Polymorphism
3Composition
- When a class has a data field that is an instance
of another class - Example an object of type Student.
A "has a" relationship
fig 2-1
Fig. 2-1 A Student object composed of other
objects
4Adapters
- Use composition to write a new class
- Has an instance of an existing class as a data
field - Defines new methods needed for the new class
- Example
public class NickName private Name nick
public NickName() nick new Name()
// end default constructor public void
setNickName(String nickName)
nick.setFirst(nickName) // end setNickName
public String getNickName() return
nick.getFirst() // end getNickName // end
NickName
5Inheritance
- A general or base class is first defined
- Then a more specialized class is defined by
- Adding to details of the base class
- Revising details of the more general class
- Advantages
- Saves work
- Common properties and behaviors are define only
once for all classes involved
6Inheritance
- Inheritance allows a software developer to derive
a new class from an existing one - The existing class is called the parent class, or
superclass, or base class - The derived class is called the child class or
subclass. - As the name implies, the child inherits
characteristics of the parent - In programming, the child class inherits the
methods and data defined for the parent class
7Inheritance
- Inheritance should create an is-a relationship,
meaning the child is-a more specific version of
the parent - Inheritance relationships are often shown
graphically, with the arrow pointing to the
parent class
8Inheritance
An "is a" relationship
Fig. 2-2 A hierarchy of classes.
9Inheritance
- A hierarchy of student classes.
- A child class of one parent can be the parent of
another child, forming class hierarchies
10Class Hierarchies
- Two children of the same parent are called
siblings - Good class design puts all common features as
high in the hierarchy as is reasonable - Class hierarchies often have to be extended and
modified to keep up with changing needs - There is no single class hierarchy that is
appropriate for all situations
11The Object Class
- All objects are derived from the Object class
- If a class is not explicitly defined to be the
child of an existing class, it is assumed to be
the child of the Object class - The Object class is therefore the ultimate root
of all class hierarchies - The Object class contains a few useful methods,
such as toString(), which are inherited by all
classes
12References and Inheritance
- An object reference can refer to an object of its
class, or to an object of any class related to it
by inheritance - For example, if the Holiday class is used to
derive a child class called Christmas, then a
Holiday reference could actually be used to point
to a Christmas object - Holiday day
- day new Christmas()
13Deriving Subclasses
- In Java, the reserved word extends is used to
establish an inheritance relationship - class Car extends Vehicle
- // class contents
-
14Calling the Base Class's Constructor
- Constructors usually initialize data fields
- In a derived class
- The constructor must call the base class
constructor - Can use the reserved word super as a name for the
constructor of the base class - When super is used, it must be the first action
in the derived constructor definition - Must not use the name of the constructor
15Accessing Inherited Data Fields
- Private data field in base class
- Not accessible by name within definition of a
method from another class including a derived
class - Still they are inherited by the derived class
- Derived classes must use public methods of the
base class - Note that private methods in a base class are
also unavailable to derived classes - But usually not a problem private methods are
used only for utility duties within their class
16Overriding Methods
- A child class can override the definition of an
inherited method in favor of its own - That is, a child can redefine a method it
inherits from its parent - The new method must have the same signature as
the parent's method, but can have different code
in the body - The object type determines which method is
invoked
17Overriding Methods
- When a derived class defines a method with the
same signature as in base class - Same name
- Same return type
- Same number, types of parameters
- Objects of the derived class that invoke the
method will use the definition from the derived
class - It is possible to use super in the derived class
to call an overridden method of the base class
18Overriding Methods
Fig. 2-4 The method toString in CollegeStudent
overrides the method toString in Student
19Overriding Methods
- Multiple use of super
- Consider a class derived from a base that
itself is derived from a base class - All three classes have a method with the same
signature - The overriding method in the lowest derived class
cannot invoke the method in the base class's base
class - The construct super.super is illegal
20The super Reference
- Constructors are not inherited, even though they
have public visibility - Yet we often want to use the parent's constructor
to set up the "parent's part" of the object - The super reference can be used to refer to the
parent class, and is often used to invoke the
parent's constructor
21The super Reference Revisited
- The super reference can be used to invoke any
method from the parent class - This ability is often helpful when using
overridden methods - The syntax is
- super.method(parameters)
22Overloading a Method
- When the derived class method has
- The same name
- The same return type but
- Different number or type of parameters
- Then the derived class has available
- The derived class method and
- The base class method with the same name
- Java distinguishes between the two methods due to
the different parameters
23Overloading a Method
- A programmer may wish to specify that a method
definition cannot be overridden - So that the behavior of the constructor will not
be changed - This is accomplished by use of the modifier
final
public final void whatever() . . .
24Overloading vs. Overriding
- Don't confuse the concepts of overloading and
overriding - Overloading deals with multiple methods in the
same class with the same name but different
signatures - Overriding deals with two methods, one in a
parent class and one in a child class, that have
the same signature - Overloading lets you define a similar operation
in different ways for different data - Overriding lets you define a similar operation in
different ways for different object types
25Protected Access
- A method or data field modified by protected can
be accessed by name only within - Its own class definition
- Any class derived from that base class
- Any class within the same package
26Protected Access
- The visibility modifiers determine which class
members get inherited and which do not - Variables and methods declared with public
visibility are inherited, and those with private
visibility are not - But public variables violate our goal of
encapsulation - The protected visibility modifier allows a member
to be inherited, but provides more protection
than public does
27Protected Access
Fig. 2-5 Public, private, protected, and package
access
28Multiple Inheritance
- Some languages allow programmer to derive class C
from classes A and B - Java does not allow this
- A derived class can have only one base class
- Multiple inheritance can be approximated
- A derived class can have multiple interfaces
- Described in Chapter 3
29Object Types of a Derived Class
- Given
- Class CollegeStudent,
- Derived from class Student
- Then a CollegeStudent object is also a Student
object - In general An object of a derived class is also
an object of the base class
30The Class Object
- Every class is a descendant of the class Object
- Object is the class that is the beginning of
every chain of derived classes - It is the ancestor of every other class
- Even those defined by the programmer
31Abstract Classes and Methods
- Some base classes are not intended to have
objects of that type - The objects will be of the derived classes
- Declare that base class to be abstractpublic
abstract class Whatever . . . - The designer often specifies methods of the
abstract class without a body public abstract
void doSomething() - This requires all derived classes to implement
this method
32Polymorphism
- A polymorphic reference is one which can refer to
one of several possible methods - Suppose the Holiday class has a method called
celebrate, and the Christmas class overrode it - Now consider the following invocation
- day.celebrate()
- If day refers to a Holiday object, it invokes
Holiday's version of celebrate if it refers to
a Christmas object, it invokes that version
33Polymorphism
- In general, it is the type of the object being
referenced, not the reference type, that
determines which method is invoked - Note that, because all classes inherit from the
Object class, an Object reference can refer to
any type of object
34Polymorphism
- When one method name in an instruction can cause
different actions - Happens according to the kinds of objects that
invoke the methods - Example
UndergradStudent ug new UndergradStudent(. .
.)Student s ug // s and ug are
aliasess.displayAt(2)ug.displayAt(4)
The object still remembers it is of type
UndergradStudent
35Polymorphism
- Which displayAt is called
- Depends on the invoking object's place in the
inheritance chain and is not determined by the
type of the variable naming the object
Fig. 2-6 The variable s is another name for an
undergraduate object.
36Encapsulation
- Hides the fine detail of the inner workings of
the class - The implementation is hidden
- Often called "information hiding"
- Part of the class is visible
- The necessary controls for the class are left
visible - The class interface is made visible
- The programmer is given only enough information
to use the class
37Encapsulation
Fig. 3-1 An automobile's controls are visible to
the driver, but its inner workings are hidden.