Overview

Today’s notes will cover various OOP paradigms built into Java. These include inheritance, static and dynamic typing, abstract classes, and interfaces. Instead of just going a deep conversation about the details of each of these concepts, I feel that it is sufficient to discuss how each of them works. A discussion on why each of these concepts exists and is used will take ages. Everyone has their own opinion in the programming community and we will be here longer than one wants to. Lets get started.

Inheritance

With this block of code, we introduce three new terms. The description of the terms are as follows:

Now, this concept of inheritance gets interesting when we realize that there is nothing restricting a super class from inheriting from another class. This will result in an inheritance hierarchy and when designed correctly, these hierarchies give us programmers a lot of power. Do notice that inheritance reinforces and encourages us to be lazy. We don’t have to reinvent the wheel. Instead we can inherit from the wheel and make a mode of transport like a bike or car.

Some Rules

The notion of inheritance sounds great, but of course there are some rules attached. They can be summarized as:

Access Modifiers

image

Taking a look at the figure above, we have some clarification on the notion of access modifiers and the role they play in inheritance. The idea here is that sometimes, we want to hide data and information from people that do not need to use it. Hiding data does not imply that making a field attribute private is secure. If only computer security was as simple as making a field attribute private!

Polymorphism

Polymorphism is a term that is thrown around a lot right around this time in the course. Simply, it means that an object can be an instance of its respective class, an instance of its superclass, its superclasses superclass, etc... There seems to be some recursive nature to polymorphism, but let us not complicate things. We will keep it simple and consider polymorphism as an "is-a" relationship. In effect, a subclass is a superclass. More concretely, every dog is an animal. But, consider the converse of this statement. Is every animal a dog?

Case Study: Dogs and Animals

Assume the existence of some Animal class and a Dog class that extends from the Animal class. Now consider the following lines of code:

Which of these lines are legal?

Why did I ask these questions? We were trying to declare a new variable in each line. For each of these lines, we tried instantiate an object and claim it is-an instance of the variable.

Static and Dynamic Types

Now consider the following line once more:

Here the static type is Animal and the dynamic type is Dog. We can generalize this statement and say that the static type is on the left of the equals sign and the dynamic type is on the right. The rule here is that dynamic type cannot be a superclass of the static type and it must be of the same type or a subclass. Animal is a superclass of Dog and therefore this is a compilation error.

There is an analogy often used to descire the notion of static and dynamic types. We have a box that we try to put dogs in. Any breed of dog can be placed into the box. But if we do not know the type of animal, we cannot confidently say it is a dog; therefore, we are not allowed to place it into the box for dogs. The label on the box is the static type. The type of the animal we are placing into the box is the dynamic type.