# Java: Problems - Inheritance

## Class definitions

Questions below apply to the following class definitions.

    import java.awt.event.*;

    class A implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
        }
    }

    class B extends A { }

## Assignment compatibility

Consider each of the following assignment statements in isolation from the others. Java allows assignment statements (without casting) that are "obviously" legal based on class and interface inheritance - identical or upward assignments. It also allows assignments which contain downcasts if the downcasts are possibly legal. Downcasts always require run-time tests for legality.

Given the following declarations.

    //-- Declarations and initializations.
    ActionListener action;
    A      a = new A();
    B      b = new B();
    Object obj = b;

Each assignment statement can be put into one of three categories, given the declarations and assignments above.

- OK - This assignment is completely legal.
- Cast - If there is a cast of the value on the right that could be made so that the compiler would accept it, and it would execute correctly at run time given the values above.
- Bad - This assignment could not be legal, regardless of any additional casting.

Put a check mark in the appropriate category for each assignment. Assume each question is independent of the others.

| OK | Cast | Bad | Assignment
--- | --- | --- | --- | ---
1 |  |  |  | `a = b;`
2 |  |  |  | `b = a;`
3 |  |  |  | `a = obj;`
4 |  |  |  | `obj = a;`
5 |  |  |  | `action = a;`
6 |  |  |  | `action = b;`
7 |  |  |  | `a = action;`