Inheritance - Part 1
Inheritance - Part 1
1h 46m

In this course, we will learn the concepts of microservice and spring framework with a focus on inheritance.

Learning Objectives

  • Inheritance in Java

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud


  • Basic Java knowledge

Hello there, my friends. In this section, we will learn object-oriented programming and its implementations. So, let's begin with inheritance. So, what exactly is inheritance in Java?

Inheritance is a fundamental concept of Object-Oriented Programming. It's used when there is an is a relationship between two objects. For example, a mountain bike is a bicycle, a road bike is a bicycle, a tandem bike is a bicycle. Different kinds of objects often have a certain amount in common with each other. So, let's talk about inheritance through a real-life example. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles: current speed, current pedal cadence, current gear. Yet each also defines additional features that make them different. Tandem bicycles have two seats and two sets of handlebars.

Road bikes have dropped handlebars. Some mountain bikes have an additional chain ring, giving them a lower gear ratio. As a result, Object-Oriented Programming allows classes to inherit commonly used states and behavior from other classes. In this example, bicycle now becomes the super class of mountain bike, road bike, and tandem bike. In the Java programming language, each class is allowed to have one direct super class and each super class has the potential for an unlimited number of sub classes. Got it? The syntax for creating a sub class is simple. After your class name, use the extends keyword, followed by the name of the class to inherit from. You can see the syntax on the slide. This gives mountain bike all the same fields and methods as bicycle, yet allows its code to focus exclusively on the features that make it unique. This makes code for your subclass is easy to read. Also, inheritance enables us to reuse existing code, which is pretty cool. Or we can say that inheritance supports the concept of reusability, i.e., when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class.

By doing this, we are reusing the fields and methods of the existing class. Now, let's look at the other concepts of inheritance in the object class. In Java, all classes can inherit variables and methods from other class. A class that is derived from another class is called a Subclass. Also a Derived Class, Extend Class, or Child Class. The class from which the Subclass is derived is called a Superclass. Also a Base Class or Class. Except for object, which has no Super Class, every class has one and only one direct Super Class; Single inheritance. In the absence of any other explicit super class, every class is implicitly a subclass of object. Classes can be derived from classes that are derived from classes that are derived from classes and so on and so on, and so on, and so on. And ultimately derived from the top-most class, object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to object. The idea of inheritance is simple, but powerful.

When you want to create a new class and there's already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write and debug them yourself. A subclass inherits all the members: Fields, methods, and nested classes from its super class. Constuctors are not members, so they are inherited by sub classes, but the instructor of the super class can be invoked from the subclass. The object class, which is defined in the java.lang package, defines and implements behavior common to all classes, including the one that you write. In the Java platform, many classes derived directly from the object. Other classes derived from some of those classes and so on forming a hierarchy of classes. At the top of the hierarchy, object is the most general of all classes. Classes near the bottom of the hierarchy provide more specialized behavior. So, what can we do in a subclass? A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the sub classes in the same package as its parent, it also inherits the package private members of the parent. A subclass does not inherit the private members of its parent class. However, if the super class has public or protected methods for accessing its private fields, these can also be used by the subclass. A nested class has access to all the private members of its enclosing class. Both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the super class. You can use the inherited members as is, replace them, hide them, or supplement them with new members. The inherited fields can be used directly, just like any other fields. You can declare a field in the subclass with the same name as the one in the super class, thus hiding it, but that's not recommended.

You can declare new fields in the subclass that are not in the super class. The inherited methods can be used directly as they are. You can write a new instance method in the subclass that has the same signature as the one in the super class, thus overriding it. You can write a new static method in the subclass that has the same signature as the one in the super class, thus hiding it. You can declare new methods in the subclass that are not in the super class. You can write a subclass constructor that invokes the constructor of the super class either implicitly or by using the keyword super. Now let's look at the casting of an object in inheritance. We've seen that an object is of the data type of the class from which it was instantiated. For example, if we write MountainBike, myBike = new MountainBike();, then myBike is of type MountainBike. Mountain bike is descended from bicycle and object.

Therefore, a mountain bike is a bicycle and is also an object, and it can be used wherever bicycle or object objects are called for. But the reverse is not necessarily true. A bicycle may be a mountain bike, but it isn't necessarily. Similarly an object maybe a bicycle or a mountain bike, but it isn't necessarily. Casting shows the use of an object of one type in place of another type. Among the objects permitted by inheritance and implementations. For example, if we write Object, myObject = new MountainBike(), then myObject is both an Object and a MountainBike until such time as myObject is assigned another object that is not a mountain bike. This is called implicit casting. If on the other hand, we write; MountainBike, myBike = myObject; we would get a compile time error because myObject is not known to the compiler to be a mountain bike. However, we can tell the compiler that we promised to assign a MountainBike to myObject  by explicit casting. You can see it on the slide.

The cast inserts a runtime check that myObject is assigned a MountainBike, so that the compiler can safely assume that myObject is a MountainBike. If the my object is not a mountain bike at runtime, an exception will be thrown. In this case, you can make a logical test as to the type of particular object using the instance of operator. This can save you from a runtime error, owing to an improper cast. For example, you can create an if statement like this. If myObject is an instance of the MountainBike, you can assign myObject to with the explicit casting. Here, the instance of operator verifies that myObject refers to a MountainBike, so that we can make the cast with the knowledge that there will be no runtime exception thrown. Got it? So, I've given you a lot of information about inheritance so far, but we haven't put it into practice yet. So, let's take a short break here. And in our next lesson we will practice inheritance. I'll see you in the next video, my friend.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.