Create Or Identify An Inheritance

Remember, that inheritance refers to the fact that children classes inherit properties and methods of their parent classes unless they defined they defined their own.
Because inheritance is a key concept in any OOP languages, it is important to know how to create or identify an inheritance. Inheritance is used all of the time in an OOP language even if you are not aware of it.

The best way to identify an inheritance is to use the phrase “IS A” in a sentence to see if it make sense. For example:

Like real life, you can also have multiple levels of inheritance by using the phrase WHICH IS A after the first phrase:

Inheritance let you know that there are some SHARED properties and methods BEWTEEN objects.

CAUTION: You need to be careful when working with classes to not confuse classes that may be related but not have inheritance. For example, there may be a relationship between a Bank and a Bank Account class but it does not pass the “IS A” test: A Bank IS A Bank Account (NO!!). While there is a RELATIONSHIP, there is not an INHERITANCE.
In a UML diagram, inheritance is shown with hollow arrows that points to the object it is inheriting FROM which is its parent class or sometimes called superclass. The child class is called the subclass.

MY CODE:

In Java, you can write the following code to create a new class of animals called lion, tiger and bear:

class Lion extends Animal {
// states and behaviors defining a Lion
}
class Tiger extends Animal {
// states and behaviors defining a Tiger
}
class Bear extends Animal {
// states and behaviors defining a Bear
}
NOTE:
While each child class focus on what is unique to it, it is important to know or document the states and behaviors of its parent super class (e.g., Animal) since it is not obvious from these classes what they are. For example, a bird is an animal, yet you may not want to include it as a class because you want to limit the type of animals to those with four legs instead of animals that can fly or have wings.



EXAMPLE:
A BankAccount is the parent class (superclass) to the CheckAccount, SavingAccount, and InvestmentAccount which are the subclasses or children classes.

Once you have created a class that inherits all of the properties and methods of its superclass, you can then ADD additional properties and methods to make is a new class.


Sometimes, you may need to NOT ADD BUT CHANGE one or more of the properties or methods that was inherited from the superclass which a new version. This is called OVERRIDING to make the new class more flexible. Often, inheritance is used not to replace but to add additional behaviors to the inherited class.


After a while, identifying and creating an inheritance becomes obvious. For example, if you created an album, book and movie class, you may immediately notice that they SHARE common properties (e.g., title and price) and methods (e.g., purchase and download).

You could then create a superclass by identifying what is COMMON between them and name the superclass Product and place those in that superclass:



NOTE: Sometimes you identify the inheritance from the bottom-up and sometimes from the top-down.

OOP languages use inheritance as part of their class libraries to provide the framework for your app instead of having to write a lot of classes yourself which would be very time consuming.  For example, in Java, there is a per-defined class called FileDialog:




NOTE: Like most OOP languages, the Object is the top-level class. All objects inherit from the Object class.