Thursday, August 1, 2013

Approach to Understanding Legal vs Illegal Class-Type Variable Assignments to Subclasses and Superclasses

Whew, what a jargon-filled post title!

Before getting started in this post, let us understand the post title in greater detail so that we are all on the same page:

Approach to Understanding Legal vs Illegal Class-Type Variable Assignments to Subclasses and Superclasses

Focusing on "Class-Type Variable Assignments": this is the assignment of a variable that is of some object-type defined by a class, i.e.

Foo foo1 = new Foo();

In the above code segment, the variable foo1 is defined as having Class-type Foo and is assigned a new object instance of Class-type Foo

Moving back to the post title, we will now focus on the idea of "Class-Type Variable Assignments" -that we just covered- "to Subclasses and Superclasses".  To understand this, let's focus again on our example:


Foo foo1 = new Foo();

Here our Class-type variable (which is of type Foo) matches the instance assigned to it (again, it is of type Foo).  Since the Class-type variable and the instance share the same Class, as opposed to subclass or superclass to one-another, let's consider a different example.  In the following example, assume that Class Dog is a subclass of Class Mammal.

Mammal animal1 = new Dog();

In the above example, we see that the Class-type variable animal1 of type Mammal is being assigned to an instance of the Dog Class, which is a subclass of Mammal (i.e. Mammal is the superclass of Dog).  This happens to be a legal assignment, but I am getting ahead of myself.  Consider the converse example:

Dog animal2 = new Mammal();

In the above example, we see that the Class-type variable animal2 of type Dog is being assigned to an instance of the Mammal Class, which is a superclass of Dog.  This happens to be an illegal assignment, but again I am getting ahead of myself.  The take-away at this point we should now understand what is meant by "Class-Type Variable Assignments to Subclasses and Superclasses".

As already noted, some of these assignments are legal, and some are illegal.  Therefore, putting it all together, this post is intended to explain my approach to understanding which of these assignments are legal and which are illegal.  After that verbose intro, let's get started!


In order to explain my approach, we'll keep with my previous example of Dog and Mammal: Class Mammal is a superclass of Dog, i.e. Class Dog extends Mammal, i.e. Dog is a subclass of Mammal.

Given this hierarchy of inheretance, I was able to come up with the following approach that makes a lot of sense to me, and can hopefully assist others:

  • When creating an instance of the Mammal class, the resultant Object knows everything there is to know about being a Mammal and expects to point to one.
  • When creating an instance of the Dog class, the resultant Object knows everything there is to know about being a Dog AND a Mammal and expects to point to both.
    • This behavior is due to the Dog being a subclass of the Mammal Class (Dog extends Mammal)
Keeping these ideas in mind,
Given:
Mammal animal1 = new Mammal();
Dog animal2 = new Dog();

consider the following cases:

  • animal1 = animal2;
  • animal2 = animal1;
  • animal2 = new Mammal();
  • animal1 = new Dog();
Case: animal1 = animal2;
Here, we are assigning a Dog (animal2) to a Mammal (animal1), i.e. we are pointing the Mammal to an object that knows everything there is to know about being a Mammal AND a Dog. For animal1, it is satisfied because its expectation to point to a Mammal is fulfilled! Therefore, the operation is legal.
Case: animal2 = animal1;

Here, we are assigning a Mammal to a Dog, i.e. we are pointing the Dog to an object that knows everything there is to know about being a Mammal ONLY. For animal2, it is unsatisfied because its expectation to point to a Mammal AND a Dog is unfulfilled! Therefore, the operation is illegal.

Case: animal2 = new Mammal();

Here, we are assigning a Mammal to a Dog, i.e. we are pointing the Dog to an object that knows everything there is to know about being a Mammal ONLY. For animal2, it is unsatisfied because its expectation to point to a Mammal AND a Dog is unfulfilled! Therefore, the operation is illegal.

Case: animal1 = new Dog();

Here, we are assigning a Dog to a Mammal, i.e. we are pointing the Mammal to an object that knows everything there is to know about being a Mammal AND a Dog. For animal1, it is satisfied because its expectation to point to a Mammal is fulfilled! Therefore, the operation is legal.

In Summary:
To ensure a variable assignment to an instance of a Class is legal, ensure the Class-type of the variable is of the same Class or some superclass of the instance's Class.

An exception to this rule is in the case of the Class-type being an Interface, in which assignment legality is a function of whether or not the instance Class implements the given Interface.

No comments:

Post a Comment