How to identify inheritance in object-oriented programming (with examples in java and python)

Inheritance in object-oriented programming is a powerful tool available to programmers. However, at the beginning is difficult for some programmers to use it in the right way.

Inheritance in object-oriented programming is a type of relationship between two classes. The right way to identify whether there is inheritance or not is by using the semantic meaning of the relation. Shortly, class A inherits from class B if the sentence “all A is a B” makes sense. Let’s find out together what is the meaning of this, as well as how to implement this type of relation in two programming languages: Java and Python.

Inheritance in object-oriented programming

What is inheritance in object-oriented programming?

Inheritance in object-oriented programming is a powerful mechanism. It gives us the ability to reuse code and therefore, make our code clearer and easier to maintain. Sometimes is confused with one of the principles of object-oriented programming. However, it is not a principle on its own, it is one of the most important techniques that help us to follow one of the principles: code reuse.

We can think about inheritance almost in the same way that we inherit things (surname, traditions, etc.) from our parents. In that case, we refer to ourselves like the children and the people that we inherit from as our parents.

Similarly, when class B inherits from class A, we say that B is the child class and A is the parent class. Another notation that is commonly used is A is the base class and B is a derived class.

How to identify inheritance in object-oriented programming?

The big question is when class B must inherit from class A? In other words, how can we identify inheritance?

There are several ways that this topic is explained in different books. However, you will be able to find that there is one approach that is always valid and is to make the following question:

  • Are all B’s A’s?

If the answer to this question is yes, then class B must inherit from class A. Also, you can formulate the sentence “all B’s are A’s”. If that sentence makes sense, then B must inherit from A.

Let’s see three examples.

We have a class that represent people (class Person) and another class that represent students (class Student).

Let’s formulate the question “Are all people students?”. The answer is no, therefore the class Person cannot inherit from the class student.

Let’s check another question “Are all students people?”. The answer is yes, therefore the class Student must inherit from the class Person.

As you can see, if you follow this method is quite easy to identify inheritance.

A wrong approach that you will see in several places

A word of caution. Many resources explain inheritance stating that class B inherits from a class A if the attributes of class A are a subset (less in amount) of the attributes of class B. This approach is wrong. It can be easily proved with the following example.

Consider the classes Ellipse and Circle. Which one is the parent class and which one the child class?

The class Circle is defined by two attributes, centre and radius. The class Ellipse is defined by a centre and two points called the minor radius and mayor radius.

If we use the number of attributes on each class to decide if there is inheritance, the class Ellipse inherits from the class Circle.

The right way

Let’s test this result using the semantic meaning of the relationship.

The question is: “Are all ellipses circles?” The answer is no. Otherwise, we will be contradicting the mathematical definition.

Let’s check the opposite: “Are all circles ellipses?”. The answer is yes. A circle is a special case of an ellipse when the minor radius and the mayor radius are equal.

To summarise this section, the right way to determine whether class B inherits from class B is using the semantic meaning of the relation. Write the question “Are all B’s A’s” and if the answer is true, B inherits from A. If the answer is false, then B does not inherit from A.

Inheritance Java example

In this section Ill show how to write an example in Java.

class Person{
    protected String name;
    protected String surname;
    protected String idNumber;
    public Person(String name, String surname, String idNumber){
        this.name = name;
        this.surname = surname;
        this.idNumber = idNumber;
    }
}

class Student extends Person{
    private String degreeEnrolled;
    public Student(String name, String surname, String idNumber, String degreeEnrolled){
        super(name, surname, idNumber);
        this.degreeEnrolled = degreeEnrolled;
    }
    public String getStudentName(){
        return this.name;
    }
    public String getDegree(){
        return this.degreeEnrolled;
    }
}

What is new in this code that you didn’t use before?

The first thing is the keyword “protected”. This is the attribute of visibility in Java that we use when there is inheritance.

It means that the “protected” attribute will only be accessible within the class Person, and within the class that inherits from the class Person (Student in this case).

The next one is the keyword “extends”.

It means that the class Student inherits from the class Person. It guarantees that everything available in the base class (methods and attributes) is also available in the child class.

The keyword “super” is the way to invoke the constructor of the base (parent) class from the derived (child) class.

When we invoke the constructor, we must pass the parameters defined in the base class constructor.

As you can see in the example, we are avoiding two repetitions:

  1. Declaring all the attributes twice.
  2. Implementing the initialisation of the attributes twice.

Another repetition we can avoid: any method implemented in the base class, can be reused in the child class if it makes sense.

If we need to change the logic of the method, then we can re-implement the method in the child class with the new logic. This is called method overriding.

Inheritance Python example

class Person:
    def __init__(self, name, surname, idNumber):
        self.name = name
        self.surname = surname
        self.idNumber = idNumber


class Student (Person):
    def __init__(self, name, surname, idNumber, degreeEnrolled):
        super().__init__(name, surname, idNumber)
        self.degreeEnrolled = degreeEnrolled

    def __str__(self):
        return self.name + self.surname

As you can see before, this is how inheritance is implemented in Python.

There are no attribute visibility modifiers (public, protected or private) in Python. However, there is a code-style convention that mimics the meaning of visibility keywords. You can access any attribute in python because they are public.

In Python, we don’t have the keyword extends. Instead, we write the base class between parentheses. That is how inheritance is specified in Python.

To invoke the constructor of the base class, we use the same keyword than in Java (super). In this case, we add the name of the constructor (__init__) and then, the parameters.

Conclusions

As a summary, the right way to identify inheritance in object-oriented programming is through the semantic meaning of the relation.

We write the question “Are all B’s A’s?”. If the answer is yes, then B inherits from A. If the answer is no, it will be wrong to decide that B inherits from A.

Notice that ALL B’s has to be A’s, not only some B’s.

Always remember the best way to learn programming is learning the fundamentals.

H@ppy coding!!!