Chapter 7
Section 7.1
- a
class establishes structure and purpose of an object, but doesn’t reserve
memory space (unless member variables are static)
- classes
are the plan and objects carry out that plan
- inheritance
is idea of deriving a new class fro an existing one
- new
class automatically contains some or all variables and methods in original
class, then you can add more variables and methods to the inherited class
to define it better
- idea
of software reuse
- original
class is parent class or superclass or base class
- derived
class is child class or subclass
- listing
7.1, 7.2, 7.3, p. 324-326
- don’t
have to instantiate parent class to be able to instantiate child class
- inheritance
is only one-way
- child
class is more specific version of parent class
- not
all variables and methods will be inherited – private ones will not,
public ones will
- protected
is a third visibility modifier that makes variables and methods public to
inherited classes, but private to rest of world
- inherited
variables and methods have same visibility modifier as parent
- constructors
are not inherited – inherited class will have its own
- can
call parent constructor by using super – a general reference to parent
class
- listing
7.4, 7.5, 7.6, p. 328-330
- child
constructor is responsible for calling parent – must be first thing done
in child constructor – if you don’t do this, Java does it automatically
- this
is all single inheritance – Java doesn’t support multiple inheritance, but
can implement numerous interfaces
Section 7.2
- child
class can override a parent method by using the same name and signature of
parent’s method
- listing
7.7, 7.8, 7.9, p. 332-334
- can
also redefine variables from parent class – called shadowing – this isn’t
done often since inherited variables already exist for child
Section 7.3
- a
class can have as many children as needed, and the hierarchy levels can
keep growing as necessary
- children
of same parent are siblings, but they aren’t related by inheritance
- inheritance
is transitive through the tree, though
- in
Java all classes are derived from Object class
- abstract
class is generic class that can’t be instantiated, and has methods with no
definition – similar to an interface
- abstract
classes can have methods that aren’t abstract, though
- use
keyword abstract
- serve
as placeholders in class hierarchy
- listing
7.10,7.11, 7.12, p. 339-341
- usually
located high in a tree
- if a
child of an abstract class doesn’t define all abstract methods, it too is
then abstract
- can’t
use final or static with abstract classes – why?
Section 7.4
- a
reference declared to refer to some class object can be used to refer to
any related class objects – makes that reference polymorphic
- listing
7.13, 7.14, 7.15, 7.16, 7.17, 7.18, 7.19, p.345-354
Section 7.5
- if a
variable or method is inherited, it can be referenced directly by name
- if it
is not inherited (it’s private), it has to be referenced indirectly
- listing
7.20, 7.21, 7.22, . 355-357
Section 7.6
- one
interface can be derived from another – forms interface hierarchies
- child
interface inherits all abstract methods and constants or parent, so any
class implementing child has to implement all these methods
Section 7.7
- we
have used inheritance all along every time we define our classes to extend
Applet
- we
also extended listener classes
- we
could also extend an event adapter class
- listing
7.23, p. 360
Section 7.8
- GUI
component is object that represents a visual entity in an applet or
application
- applet
itself is a special component called a container, that can hold other
components
- components
generate certain events
- each event
has corresponding listener interface and adapter class
- listing
7.24, p. 364 – uses labels and text fields
- text
field generates ActionEvent when enter is hit in text field
- paint
method is defined in Component class
- listing
7.25, 7.26, p. 367-369 – uses canvas