The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework with a focus on inheritance.
- Inheritance in Java
- 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. In this video, we will talk about the abstract classes. An abstract class is used to implement common features of related classes. It is declared with the abstract keyword. If a class has at least one abstract method then the class must be declared abstract. Also, abstract classes may contain non-abstract methods and variables. You cannot instantiate an abstract class. This means that you cannot create the object of it. In order to use an abstract class, we have to inherit it from another class. You can think of an abstract class like a template, you must extend it and override the abstract methods. An abstract method is just a prototype, it has no body and contains the following attributes: A return type, a method name and a list of parameters. Methods and members of an abstract class can be defined with any visibility such as public, protected, private, etc.
In our example, Vehicle is an abstract class that contains only one abstract method, getMaxSpeed without a body. Because the implementation of this method is provided by the Car and Motorcycle classes, Car and Motorcycle classes have to override the abstract getMaxSpeed method of super class Vehicle. Let's look at some examples to understand the abstract class better. In the object-oriented programming project, right click on the source folder and select new class. I will specify the package name as abstractclass and class name is Vehicle and we can choose the abstract option here to create an abstract class or you can add this keyword after creating this class. All right, the Vehicle class is an abstract class now. Also this class will be the super class, abstract super class. Let's declare two instance variables named type and model with String type in the Vehicle class. I write String type and model.
Also, you cannot create an abstract variable if you add the abstract keyword at the beginning of the variable, you get a compilation error. Let's declare a public constructor named Vehicle and it takes two parameters, type and model, public vehicle(String type, string model). In the constructor method, we will assign parameter variable type to variable type of this class, this.type = type; and we will assign the parameter variable model to the variable model of this class, this.model = model; We use the "this" keyword to access the same class variables. Now you know the intended use of the "this" keyword. Okay, now let's declare two non-abstract void methods in the Vehicle class. The first one is public void start(), we will print the "Vehicle has started" message by using the println method. The second one is public void stop(), we will print the "Vehicle has stopped" message by using the print Ln method. Let's declare an abstract int method named getMaxSpeed without a body.
We must implement this method in the subclass we will create soon. Also, I'd like to mention one more thing here, you cannot define an abstract method as final or the access modifier cannot be private or default. So, if you use both the abstract and final on another access modifier here, you will get a compilation error. It says the abstract method getMaxSpeed in type Vehicle can only set a visibility modifier, one a public or protected. Because this method must be inherited by the subclass, as you can remember, you cannot inherit instances that you define as final or private. This is a very important point. Abstract classes can have a final method, but the keywords abstract and final cannot be used in the same method. Also, the access modifier of the abstract methods must be public or protected. Okay, I think you got it, so let's continue.
All right, now I'll save the code. Let's create a new subclass, I will right click on the abstractclass package and select new class. I will specify the class name as Car and click the 'Finish' button. Our subclass Car will extend the abstract super class Vehicle. This means that subclass Car inherits all methods and variables from super class Vehicle. So, after the class name I write extend Vehicle, but as you can see we get a compilation error. If you place the mouse pointer on Car class, Eclipse shows a quick fix dialog. We defined a constructor in the abstract super class Vehicle, so we must define an explicit constructor in the subclass Car and we must invoke the super class constructor from this constructor by using the super keyword. We've done this in our previous lessons. There are two ways to avoid this error. The first is to create an empty constructor in the Vehicle class because the empty constructor will be called automatically by the compiler, you won't get an error like this anymore.
The second is to create an explicit constructor in this class. Thanks to the super call statement in this constructor, we will be able to access the constructor parameters in the super class. As a result, if the super class you inherit has a parameterized constructor, you must either create an explicit constructor with a super call in the subclass or create an empty constructor in the super class to avoid this error in the subclass. Either way will fix the error. Of course, it would be more logical to apply whichever method your application necessitates that everything I just said is just to fix the error. So, this is also an important point to keep in mind. Okay, let's continue now. We will add the explicit constructor by selecting the add constructor option in the quick fix dialog of Eclipse.
As you can see, Eclipse automatically added the constructor with the super call expression, but as you can also see we are still getting an error. I will move the cursor on the Car class, Eclipse shows a quick fix dialog again. We must implement the abstract method getMaxSpeed in the Car class. I will select the add unimplemented methods option and Eclipse automatically added the getMaxSpeed method with override annotation. Okay, so now let's talk about the annotation a little bit. Annotations were introduced in Java 5. An annotation always starts with the @ symbol, @ followed by the annotation name. Whenever you override a method of the super class, it's best practice to always include the override annotation with the method name. Override annotation is also used for making code readable and avoiding maintenance issues. All right, now let's override other methods of super class Vehicle.
There is no obligation to override non-abstract methods of the abstract super class Vehicle, you only need to override abstract methods. There is no such requirement for non-abstract ones. If you write the name of the method you want to override, for example, let's write start. And if selected from here, the method will be overridden automatically with the override annotation. Also note that the keyword super is used here as well. Super specifies that this method comes from the super class and references the super class. Of course, we will soon delete the super statement and rearrange it according to the Car class. You can override a method like that or you can copy it from a super class and paste it here. After the pasting, you can add the override annotation manually. Let's override the second non-abstract method named stop manually. I will copy the non-abstract stop method of the super class vehicle and I will paste it into the subclass Car, and let's make changes for implementing properties of the Car class.
I will delete the super keyword here and I will use the "Car has started" message in the start method instead of "Vehicle has started" and I will use the "Car has stopped" message in the stop method instead of "Vehicle has stopped". And lastly, let's override annotation to these methods. Okay, also in the getMaxSpeed method, I will declare a final variable MAX_CAR_SPEED in uppercase letter and assign this variable 320. It's really good practice to use final variables using uppercase letters and underscores as separators. And now I will print "The max speed of car" message by using the println method. And after the plus sign, I write the MAX_CAR_SPEED final variable, and lastly I will return the variable MAX_CAR_SPEED and I will save the code. Okay, now let's create the main test class named AbstractTest in the same package. In the main method, I will create a Car object, Car myCar = new Car.
Also, I have to pass the necessary parameters. The first parameter Car represents the type of car, and the second parameter Ferrari represents the model of the car. And now let's call the override and start and stop methods from the subclass Car using the myCar object. And lastly, let's call the overridden abstract getMaxSpeed method from subclass Car. All right, let's run the code. You see the results in the console. Overridden start method was invoked and the "Car has started" message was displayed, and then overridden stop method was invoked and the "Car has stopped" message was displayed. And then overridden abstract getMaxSpeed method was invoked, and "The max speed of car is 320" message was displayed. So, that's pretty much how the abstract class is used. I think the abstract class is understood now. So, let's take a short break here my friends and I'll see you in the next lesson.
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.