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
Prerequisites
- Basic Java knowledge
Hello there my friends. In the previous video we talked about the inheritance concept. In this video, we will practice how we can use inheritance in Java. First, let's remember inheritance through another example. We have a superclass Vehicle. All vehicles have a type, model, and maxSpeed. Also all vehicles can start () and stop (). So, this is the base class. We also have two classes named Car and Motorcycle. Car and motorcycle classes are subclasses of the Vehicle class. We use the extends keyword by the subclass to inherit the features of the super class. So, the Car and Motorcycle classes inherit variables and methods from the Vehicle class. There is no need to declare these variables or methods again for Car and Motorcycle classes. Car class has its own variable named isAutomatic and the Motorcycle class has its own variable named cylinderVolume.
All right, let's move on to the Eclipse and do some practice. Let's start to write a program that prints the properties of subclasses by using inheritance. First, I will create a super class in the Object-Oriented programming project. Right-click on the 'Source' folder and select New Class. I will specify the package name as inheritance and the class name can be Vehicle. This class will have some fields and a method. And using the inheritance, we will reuse these properties and other classes we will create. So, the Vehicle class will be a super class. Let's declare four instance variables with four access modifiers in the Vehicle class. First, protected String model; second private String type; third String color; fourth public int maxSpeed;. Now, let's declare a void type method print to display values of type, model, color, and maxSpeed variables. Public void print() { inside this method we will use the println() method. S.out. ("Type = " + type); and I will copy this line and paste it three times. The second print() method will print the model. The third print() method will print the color. And the last print() method will print the maxSpeed. Finally, I will save the code.
Okay, now let's start to declare subclasses. I will right-click on the 'Inheritance' package and select New Class. I will specify the class name as Car and I click the 'Finish' button. This class will be a subclass and it will inherit from the Vehicle class we created. In order to implement inheritance, we have to add the extends keyword after the class name. Okay, now our subclass Car extends superclass Vehicle. This means that subclass Car inherits the print() method and variables from super class Vehicle. Currently the Car class is empty, but we can also add some fields to this class, because this class can have its own properties. For example, let's add a Boolean variable. boolean isAutomatic; and let's print this on the console with the help of a method. Public void printGear() { and I will create a println() method. S.out. ("isAutomatic : " + and after the plus sign I write the isAutomatic); variable. Okay, now let's save the code again. Don't forget to save the code. Let's create another subclass. Right-click on the 'Inheritance' package and select New Class. I will specify the class name as Motorcycle. This class will also be a subclass and it will inherit from the Vehicle class we created. In order to implement inheritance, we have to add the extends keyword after the class name. Okay, now our subclass Motorcycle extends superclass Vehicle.
This means that subclass Motorcycle inherits the print() method and variables from the superclass Vehicle. Currently the Motorcycle class is empty, but we can also add some fields to this class, because this class can have its own properties. For example, let's add a string variable, String cylinderVolume;. And let's print this on the console with the help of a method; public void printVolume() { and I will create a println() method. S.out.("Cylinder Volume : " + and after the plus sign I write the cylinderVolume); variable. Okay, let's save the code again. Like I said, let's not forget to save the code.
Okay, let's create a new class for the test of inheritance. Right-click on the 'Inheritance' package and select New Class. I will specify the class name as InheritanceTest and select the checkbox to add the main method. Firstly, let's start to create a new Car object in the main method. Car myCar = new Car();. If you write myCar object and dot operator, then you will see the variables maxSpeed, color, model and print() method coming from superclass Vehicle. But if you notice we cannot see the type variable, because the access modifier of the type variable is private, and the private fields cannot be inherited. We can also see the isAutomatic variable and printGear() method of the Car class itself. Now let's continue. First, I will specify the value of the isAutomatic variable. Car.isAutomatic = true; and let's print it with the printGear() method. Car.printGear();. Secondly, let's specify the model.
Car.model, this property comes from the vehicle class and its model can be "Ferrari";. And then let's specify the color. Car.color, the color can be "Red";. And let's specify the maxSpeed. maxSpeed can be 300;. And lastly, let's call the print(); method from super class vehicle. Okay, let's run the code. You can see the results in the console. isAutomatic is true, the Model is Ferrari, the Color is Red, and maxSpeed is 300. Except for the isAutomatic, all of these properties are inherited from superclass Vehicle. All right, let's start to create a new Motorcycle object. Motorcycle myMotorcycle = new motorcycle ();. And I write myMotorcycle. After the dot, you can see the same properties and methods coming from the superclass Vehicle. And again, we cannot inherit the type property because it is private.
Now let's specify the properties. The model of the motorcycle can be "Kawasaki"; the color of the motorcycle can be "Blue"; and the maxSpeed can be "200";. And let's call the print(); method from the superclass vehicle. Also, I will specify the cylinderVolume. The cylinderVolume can be "900 cc"; and I will call the printVolume() method. The cylinderVolume variable and the printVolume() method are elements of the motorcycle subclass. Okay, let's run the code. You see the results in the console. The Model is Kawasaki, the Color is Blue, the MaxSpeed is 200, the Cylinder Volume is 900 cc. All right, I think you understand the concept of inheritance a little better now. Let's take a little break here and I'll see you in the next lesson, my friend.
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.