Object Oriented Concept - Inheritance
Tags: oop, objectoriented, inheritance, concept
Published: 2023-08-28 13:53:09

On programming language the term inheritance comes with same idea as the real world, this concept basically let’s you inherit from your parent what they have for you to use.

In development is the same thinking, you inherit the behavior and values (methods and attributes or variables) from your parent class, that often is called superclass, but why would you do so?

Let’s say we are in a medieval age and there is a man who is a blacksmith and knows how to make any generic item using a hammer and an anvil and some fire. He works a lot and is getting a lot of new requests, so to make sure he can attend the demand he found a wife, got three kids and started to teach those kids.

Each of the kids would work on different areas, Bob would work with making armor, Daniel with weapons while Joe would work on construction materials.


![father working as blacksmith](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smaf1x9k0mm56fyl3icl.jpg)

The simple story that I would explains the logic behind and why inheritance. The three kids from the blacksmith inherited the values from its father and each of them use the same knowledge and also extends with their own way of doing things, each on a different way.

If I would bring this to the programming world would be something like:

```java
package org.example.ex;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

public class Father {

    <T> T forge(Class<T> forgedItemClass, final List itemDetails) throws InstantiationException,
            IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        /* ... any extra step related to generic forge needed here */
        return forgedItemClass.getDeclaredConstructor().newInstance();
    }

}

class Bob extends Father {

    public <T> T forgeArmor(Class<T> armorClass, List details) throws InstantiationException,
            IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        /* ... any extra step related to armor forge needed here */
        return forge(armorClass, details);
    }
}

interface Armor {
    void protect();
}
```

In this coding example Father has method forge and Bob uses the inherited method from Father in forgeArmor method.

But inheritance can go even further. Let’s say times go by and Bob gets more proficient than his father and got a better way to forge it’s own and also a forge with an specific hammer.

To translate that into coding, that would be:

```java
class Bob extends Father {

    public <T> T forgeArmor(Class<T> armorClass, List details) throws InstantiationException,
            IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        /* ... any extra step related to armor forge needed here */
        return forge(armorClass, details);
    }

    <T> T forge(Class<T> forgedItemClass, final List itemDetails) throws InstantiationException,
            IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        /* ... any extra step related to new proficient generic forge needed here */
        return forgedItemClass.getDeclaredConstructor().newInstance();
    }

    <T> T forge(Class<T> forgedItemClass, final List itemDetails, Hammer hammerType) throws InstantiationException,
            IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        /* ... any extra step related to new proficient generic forge needed here */
        return forgedItemClass.getDeclaredConstructor().newInstance();
    }
}

interface Hammer {
    
}
```

So now when Bob forges it’s armor it uses his forge instead of his Father, this is called polymorphism, and in this case overrides Father method forge with its own method.

But how about the forge method with hammer argument? isn’t the same name? yes it is, this is also polymorphism but through overloading, which is same method but different arguments.

What if Bob gets his family and don’t want to his kids to change his knowledge to how to forge? then he could be final on his word and his kids would never be able to override what’s done, that can be done with final keyword on the method:

```java
class Bob extends Father {

    public final <T> T forgeArmor(Class<T> armorClass, List details) throws InstantiationException,
            IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        /* ... any extra step related to armor forge needed here */
        return forge(armorClass, details);
    }

    final <T> T forge(Class<T> forgedItemClass, final List itemDetails) throws InstantiationException,
            IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        /* ... any extra step related to new proficient generic forge needed here */
        return forgedItemClass.getDeclaredConstructor().newInstance();
    }
}
```

To conclude, inheritance is the concept of inherit from the father, superclass, behavior and values to the children, subclass. Besides that the subclass can update the superclass behavior through override or use same method name and add new arguments through overload.

---

That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.

Until next post! :)