Inheritance and Polymorphism

Created


Inheritance

Inheritance is a mechanism that allow classes to be derived from other classes, with the child classes (also called subclass or derived class) inheriting fields and methods from the parent classes (also called superclass or base class). The child sub-class can also define its own fields and methods. Inheritance is a one-way relationship.

Implementation

  • To inherit from a class use the extends keyword.
  • The sub-class must not redefine the field methods of the super-class unless for overriding.
  • Constructors of the superclass are not inherited, so you must define the non-default constructors of a subclass Invoke the constructors of the super-class by using the super keyword.
  • super is similar to this, but points to the parent class. Use this to access the parent class's fields and methods.
  • A sub-class inherits everything, inluding fields and methods that are declared as private.
  • Selective inheritance does not exist. You cannot select a subset of fields/methods to be inherited. You cannot change the visibility of inherited fields from public to private.
  • Inheritance is transitive, so you can have multi-level inheritance.

Multiple Inheritance Java does not support multiple class inheritance because of cases where a diamond is formed by inheritance. "Diamond of death." Each class can only have one parent.

Types

Type substitution allows you to use subtype in place of a type. A given type variable may be assgned a value of any subtype, and a method with a paramter of a given type may be invoked with an argument of any subtype of that type.

Student jane = new GradStudent("Jane", "jane@jhu.edu")

Appearant type answers the question: what can this object do? It is the decalred type. - Jane is not capable of GradStudnet methods.

Actual type: shows how an object behaves. It is the instantiated type.

  • In reality, GradStudent attributes and methods do exist. They are accessible if Jane is cast as a GradStudent type.

Up casting in implicit, (GradStudent to Student)

If you type cast something that cannot be cast it will crash and throw an ClassCastException. So, you can test if its a GradStudent with an instanceof operator.

Student s;
if (s instanceof GradStudent) {...}

The typecasting of a parent object to a child object is known as downcasting.

Polymorphism

Polymorphism means "having many forms."

List of substitution principle: Don't change behavior a method drastically when overriding .

How is polymorphism related to inheritance? It's equivalent to the difference between overriding and overloading.

Runtime vs Compile Time Polymorphism Static or compile-time polymorphism is when the compiler honors type substitution and implicitly casts from a subtype to the base type.

Dynamic or runtime polymorphism is when the Java Virtual Machine decides at runtime which method to dispatch. Method dispatch is the process to decide which implementation of a method to use where they're are multiple implementations available (as a result of method overriding).

  • It relates to the ability of a child class to override the behavior of its parent class.
  • The JVM method dispatch starts at the instantiated/actual type and then looks up the hierarchy until if finds an implementation.

@Override is an annotation, meta data. It signals to the compiler that this is an overwritten version of something up the hiearchy, so that the compiler will do a sanity check for you.

Overloading vs Overriding Overloading is when you reuse a function name but has a different type or number of parameters (compile time). Overloading is geared towards different options. Overriding is a geared towards a subtle change in behavior (runtime).

Why should you implement inheritance? "is-a" relationship gives you inheritance. Code reusse is an added advantage. "has-a" relationship gives you composition, it can be used to acievecode reuse and polymorphism.

The Object class Every class in Java implicityly extends the Object class, which contains the toString(), equals(), hashCode() method.