Title: Chapter 7: Class Inheritance
1Chapter 7 Class Inheritance
- Superclasses and Subclasses
- Keywords super and this
- Overriding methods
- The Object Class
- Modifiers protected, final and abstract
- Casting Objects
- Numeric Wrapper Classes
- Interfaces
- Inner Classes
- Class Design Guidelines
2Superclasses and Subclasses
3Creating a Subclass
- Creating a subclass extends properties and
methods from the superclass. You can also - Add new properties
- Add new methods
- Override the methods of the superclass
Cylinder Class
4Using the Keyword super
The keyword super refers to the superclass of the
class in which super appears. This keyword can be
used in two ways
- To call a superclass constructor
- To call a superclass method
5Example 7.1Testing Inheritance
- Objective Create a Cylinder object and explore
the relationship between the Cylinder and Circle
classes.
TestCylinder
Run
6Example 7.3 Overriding Methods in the Superclass
- The Cylinder class overrides the findArea()
method defined in the Circle class.
Test Modifying Methods
Run
7The Keyword this
- The keyword this refers to the current object.
- The keyword can be used in two ways
- To call a class constructor
- To pass the current class as an argumentto a
method
8Example of Using this
- class Circle
- private double radius
- Circle(double radius)
-
- this.radius radius
-
- Circle()
-
- this(1.0)
-
-
- public double findArea()
-
- return radiusradiusMath.PI
-
-
9The Object Class
- The Object class is the root of all Java classes.
- The toString() method returns a string
representation of the object. - The equals() method compares thecontents of two
objects.
10The protected Modifier
- The protected modifier can be applied on data and
methods in a class. - A protected data or a protected method in a
public class can be accessed by any class in the
same package or its subclasses, even if the
subclasses are in a different package.
11The final Modifier
- The final class cannot be extended
- final class Math ...
- The final variable is a constant
- final static double PI 3.14159
- The final method cannot bemodified by its
subclasses.
12The abstract Modifier
- The abstract class
- Cannot be instantiated
- Should be extended and implemented in subclasses
- The abstract method
- Method signature withoutimplementation
13Casting Objects
- It is always possible to convert a subclass to a
superclass. - For this reason, explicit casting can be
omitted. For example, - Circle myCircle myCylinder
- is equivalent to
- Circle myCircle (Circle)myCylinder
14Casting fromSuperclass to Subclass
- Explicit casting must be used when casting an
object from a superclass to a subclass. - This type of casting may not always succeed.
- Cylinder myCylinder (Cylinder)myCircle
15The instanceof Operator
- Use the instanceof operator to test whether an
object is an instance of a class - Circle myCircle new Circle()
- if (myCircle instanceof Cylinder)
-
- Cylinder myCylinder (Cylinder)myCircle
- ...
-
16Example 7.4Casting Objects
- Objective Use implicit casting to assign circles
and cylinders to an array and then use explicit
casting to access data and methods in the objects
when processing the array.
TestCasting
Run
17Class Inheritance Hierarchy
18Numeric Wrapper Classes
- Boolean
- Character
- Short
- Byte
- Integer
- Long
- Float
- Double
19The Integer Classand The Double Class
- Constructors
- Class Constants MAX_VALUE, MIN_VALUE
- Conversion Methods
20Class Design Guidelines
- Hide private data and private methods.
- Choose informative names and followconsistent
styles. - A class should describe a single entity or aset
of similar operations. - Group common data fields and operations shared by
other classes.
21Example 7.5Designing Abstract Classes
- Objective This example gives a generic class for
matrix arithmetic. This class implements matrix
addition and multiplication common for all types
of matrices.
GenericMatrix
22Example 7.6Extending Abstract Classes
- Objective This example gives two programs that
utilize the GenericMatrix class for integer
matrix arithmetic and rational matrix arithmetic.
TestIntegerMatrix
Run
TestRationalMatrix
Run
23Interfaces
- What Is an Interface?
- Creating an Interface
- Implementing an Interface
24Creating an Interface
- modifier interface InterfaceName
-
- constants declarations
- methods signatures
25Example of Creating an Interface
- public interface CompareObject
-
- public static final int LESS -1
- public static final int EQUAL 0
- public static final int GREATER 1
- public int compare(CompareObject otherObject)
26Example 7.7 Using Interfaces
- Objective Use the generic sorting method defined
in the CompareObject interface to sort an array
of circles in increasing order of their radii and
an array of cylinders in increasing order of
their volumes.
27Example 7.9 (cont.)
TestSortCircleCylinder
Run
28Inner Classes
- Inner class A class is a member of another
class. - Advantages In some applications, you can use an
inner class to make programs simple. - An inner class can reference the data and methods
defined in the outer class in which it nests, so
you do not need to pass the reference of the
outer class to the constructor of the inner class.
29Inner Classes (cont.)
- Inner classes can make programs simple and
concise. As you see, the new class is shorter and
leaner. - Many Java development tools use inner classes to
generate adapters for handling events.
Event-driven programming is introduced in Chapter
8, "Getting Started with Graphics Programming. - An inner class is only for supporting the work of
its containing outer class, and it cannot be used
by other classes.