- Forward


An Introduction to Specialization/Generalization and Inheritance
With Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • A Question:
    • What is the relationship between "dogs" and "animals"?
  • Some Answers:
    • A dog is an animal
    • Expand
    • All dogs are animals
    • Expand
    • All dogs are animals but all animals are not dogs
    • Expand
    • The set of dogs is a proper subset of the set of animals
    • Expand
Review
Back SMYC Forward
  • What is a class?
    • A definition of a set that is written in terms of the properties of the elements (i.e., an intensive definition of a set)
    • The set of elements so defined
    • Expand
  • What is an object?
    • An element of the set
    • Expand
Review (cont.)
Back SMYC Forward
  • What is involved in designing and implementing a class?
    • Creating an intensive definition (i.e., a list of attributes and behaviors)
    • Expand
  • What is involved in designing and implementing the attributes?
    • Declaring the names and types of instance variables
    • Declaring the names and types of class variables
    • Expand
  • What is involved in designing and implementing the behaviors?
    • Declaring the signatures of instance and class methods
    • Implementing the bodies of instance and class methods
    • Expand
Subsets and Supersets
Back SMYC Forward
  • A Subset:
    • Has additional characteristics
    • Is a specialization
    • Mathematical Notation: \(A \subset B\) denotes that \(A\) is a (proper) subset of \(B\)
  • A Superset:
    • Ignores some characteristics
    • Is a generalization
    • Mathematical Notation: \(G \supset H\) denotes that \(G\) is a (proper) superset of \(H\)
Specialization/Generalization of Classes
Back SMYC Forward
  • The "is-a" relationship:
    • An object of class A "is-an" object of class B if A is/does everything that B is/does and more
  • Other Terminology:
    • Subclass/Superclass
    • Child Class/Parent Class
    • Derived Class/Base Class
Specialization of enums
Back SMYC Forward
  • Recall:
    • A definition of a set that is written by listing the elements (i.e., an extensive definition of a set)
  • What Would a Specialization Be?
    • A subset (i.e., a list of elements to omit from the parent)
  • The Implication:
    • Java does not allow enums to be specialized (though the "is a" relationship can be achieved in other ways as discussed later)
Inheritance
Back SMYC Forward
  • A Loose Definition:
    • Instances of the child class have the characteristics of the parent class without having to implement them
  • What Is/Isn't Inherited in Java?
    • The subclass inherits the attributes of the superclass
    • The subclass inherits the methods of the superclass
    • The subclass does not inherit constructors from the superclass
  • Visibility/Accessibility:
    • The subclass cannot directly access private members in the superclass
    • The superclass can restrict access to descendants by declaring members to be protected
Specialization/Generalization in UML
Back SMYC Forward

images/UML-specialization.gif

  • Visibility:
    • Protected accessibility/visibility is denoted using a #
  • Inherited Methods:
    • Inherited methods are (typically) not explicitly listed in the subclass
Class Hierarchies
Back SMYC Forward

An Example

images/specialization_vehicle.gif

The Syntax of Specialization in Java
Back SMYC Forward
  • Declaring a Subclass:
    • Use extends in the declaration of the class
  • Accessing Members in a Superclass from a Subclass:
    • Use super as the left-side operand of the . operator to eliminate ambiguity (if needed)
  • Calling the Constructor in a Superclass from a Subclass:
    • Call super() (which must be the first statement in a constructor of the subclass)
Specialization and Constructors in Java
Back SMYC Forward
  • Explicit Value Constructors in the Subclass:
    • Should include a call to an appropriate constructor in the superclass
    • If no version of super() is called explicitly, the default constructor in the superclass will be called (so, if there isn't one, the subclass won't compile)
  • Default Constructors in the Subclass:
    • Should include a call to the default constructor in the superclass
    • If no version of super() is called explicitly, the default constructor in the superclass will be called (so, if there isn't one, the subclass won't compile)
Common Uses of Specialization
Back SMYC Forward
  • Purpose:
    • Add attributes
    • Add behaviors
    • Modify existing behavior
  • Situations:
    • You only have the byte code (i.e., the .class file) for an existing class
    • You have the source code (i.e., the .java file) for an existing class but don't want to change it
    • You are designing classes "from scratch"
Adding Characteristics to an Existing Class
Back SMYC Forward
  • Existing Classes:
    • Part of the Java API (e.g., DecimalFormat java.text.DecimalFormat )
    • Written by a "third party"
  • If only:
    • it also had the following attributes
    • it also had the following methods
Adding Characteristics to an Existing Class (cont.)
Back SMYC Forward

An Example

javaexamples/oopbasics/FieldFormat.java
 
Will It Compile and Why/Why Not?
Back SMYC Forward
images/FieldFormat.gif
javaexamples/oopbasics/FieldFormatWhatHappensAndWhy.java
 
Modifying Behavior of an Existing Class
Back SMYC Forward
  • Existing Classes:
    • Part of the Java API (e.g., StringTokenizer java.util.StringTokenizer )
    • Written by a "third party"
  • If only:
    • it did this instead
  • What Can the Subclass Do?
    • Override methods in the superclass (i.e., include a method with the same signature but a different implementation)
Modifying Behavior of an Existing Class (cont.)
Back SMYC Forward

An Example

javaexamples/oopbasics/CaseInsensitiveStringTokenizer.java
 
Will It Compile and Why/Why Not?
Back SMYC Forward
images/CaseInsensitiveStringTokenizer.gif
javaexamples/oopbasics/CaseInsensitiveStringTokenizerWhatHappensAndWhy.java
 
Adding/Modifying Characteristics "From Scratch"
Back SMYC Forward
  • Situation:
    • You are implementing all of the classes
    • You have the source code
  • Why Use Specialization?
    • The base class has already been thoroughly tested
    • The derived class adds significant complexity that is not required by all objects
Adding Characteristics "From Scratch"
Back SMYC Forward

An Example

javaexamples/oopbasics/emergency1/EmergencyMessage.java
 
Adding Characteristics "From Scratch" (cont.)
Back SMYC Forward

An Example (cont.)

javaexamples/oopbasics/emergency1/Alert.java
 
Modifying Behavior "From Scratch"
Back SMYC Forward

An Example

javaexamples/oopbasics/im1/Chirp.java
 
Modifying Behavior "From Scratch" (cont.)
Back SMYC Forward

An Example (cont.)

javaexamples/oopbasics/im1/ExpandedChirp.java
 
Modifying Behavior "From Scratch" (cont.)
Back SMYC Forward

An Example (cont.)

javaexamples/oopbasics/im1/ChirpDriver.java
 
Shadowing
Back SMYC Forward
  • An Observation:
    • It is possible to have an attribute in the derived class with the same type and name as an attribute in the base class (which is called shadowing)
  • Best Practice:
    • Where possible, shadowing should be avoided because it is confusing
  • Shadowing Private Attributes:
    • You may not always know the private attributes in the base class (especially if you don't have the source code for the base class)
Conventions when Overriding Methods
Back SMYC Forward
  • UML:
    • Overridden methods are (typically) explicitly listed in the subclass
  • Java:
    • Overridden methods are often annotated with @Override
One Common Mistake
Back SMYC Forward
  • The Situation:
    • A method in the derived class needs to call the method it overrides in the base class
  • The Mistake:
    • Forgetting to use super.
  • The Symptom:
    • A stack overflow (i.e., the method in the derived class calls itself until it runs out of memory)
Another Common Mistake
Back SMYC Forward
  • The Situation:
    • An assignment statement doesn't compile (and you think it should or, at least, really want it to)
  • A "Rookie" Mistake:
    • Use a typecast
  • Why This Makes the Compiler Happy:
    • The compiler believes the programmer
A Common Mistake (cont.)
Back SMYC Forward
Chirp c; ExpandedChirp e; c = new Chirp("TTFN"); e = (ExpandedChirp)c;
  • Why won't the last line compile without the type cast?
    • A Chirp is not an ExpandedChirp and methods might be called on e that it doesn't have
      Expand
  • Why will the last line compile as is?
    • The compiler believes the programmer
      Expand
  • What will happen at run-time?
    • A ClassCastException will be thrown
      Expand
Static Members
Back SMYC Forward
  • In a Base Class:
    • Are attributes/methods of both the base class and any derived classes
  • When This Can Be Confusing:
    • When you have multiple derived classes specialize the same base class (because they will "share" the same static members)
Preventing Specialization in Java
Back SMYC Forward
  • A Particular Method:
    • A method that is declared to be final can't be overridden by a subclass
  • An Entire Class:
    • A class that is declared to be final can't be specialized (i.e., a class that extends a final class will not compile)
Preventing Specialization in UML
Back SMYC Forward
  • A Particular Method:
    • A common convention is to include {leaf} after the return type
  • An Entire Class:
    • A common convention is to include {leaf} after the name
A Peculiarity of Specialization in Java
Back SMYC Forward
  • The Object java.lang.Object Class:
    • All classes that don't explicitly extend a class implicitly extend the Object class
  • Implication:
    • All objects have clone(), equals() and toString() methods, though they don't always do what you want
A Java Detail we Skipped Earlier
Back SMYC Forward
  • Recall:
    • Some exceptions are checked and some are unchecked
  • How To Distinguish:
    • Unchecked Exceptions - Descendants of RuntimeException java.lang.RuntimeException
    • Checked Exceptions - All other descendants of Exception java.lang.Exception
Some Other Details we have "Danced Around"
Back SMYC Forward
  • Recall:
    • In JUnit, assertEquals() works with int values, double values, objects, etc...
    • The printf() method in the PrintWriter class is passed a format string and then a variable number of arguments of any type
  • How?
    • assertEquals() is overloaded and one of the versions has Object parameters
    • The parameters of printf() are String and Object...
There's Always More to Learn
Back -