Abstract Classes and Methods

Start course
Overview
Difficulty
Beginner
Duration
2h 28m
Students
16
Description

In this course, we'll learn about object-oriented programming and its implementations.

Learning Objectives

  • Inheritance
  • Method Overriding
  • Super and This Keywords
  • Final Keyword
  • Abstract Classes
  • Interfaces
  • Polymorphism 
  • Encapsulation

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there, in this video we will talk about the Abstract Classes. An abstract class is used to implement common features of related classes. It's 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 may think an abstract class is like a template, so 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 et cetera. 

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 make 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 abstract class, and class name as vehicle. And we can choose the abstract option here to create an abstract class, or you can add this keyword after creating this class. 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 in parenthesis, string type and string model. In the constructor method, we'll assign parameter variable type to variable type of this class, this.type equal type. And we'll assign parameter variable model to variable model of this class, this.model equal model. We use the this keyword to access the same class variables. Now, you know the intended use of the this keyword. Let's declare two non-abstract void methods in the vehicle class. The first one is public void start. We'll print the vehicle has started message by using the printLn method. The second one is public void stop. We'll print the vehicle has stopped message by using the printLn 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 would 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 or other 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 of public or protected because this method must be inherited by the subclass. If you remember, you cannot inherit instances that you define as final or private. This is an 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. Yes, let's continue now. Now, I will save the code. Yes, let's create a new subclass. I'll right click on the abstract class package and select new class. I'll 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 dialogue. 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'll 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 we'll fix the error. 

Of course, it would be more logical to apply whichever method your application necessitates, what I said is just to fix the error. This is also an important point to keep in mind. Let's continue now. We'll add the explicit constructor by selecting the add constructor option in the quick fix dialogue of eclipse. As you can see, eclipse automatically added the constructor with the super call expression but as you can see we are still getting an error. I'll move the cursor on the car class, eclipse shows a quick fix dialogue again. We must implement the abstract method getMaxpSeed in the car class. I'll select the add unimplemented methods option and eclipse automatically added the getMaxSpeed method with override annotation. Now, let's talk about the annotation a little bit. Annotations were introduced in Java 5. 

An annotation always starts with the at 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. 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 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 this super statement and rearrange it according to the car class. You can override a method like that or you can copy it from the 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 if you want. I'll copy the non-abstract stop method of the super class vehicle and I'll 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. Also, in the getMaxSpeed method, I'll declare a final variable max car speed in uppercase letter and assign this variable to 320. It's really good practice to use final variables using uppercase letters and underscores as separators. And now I'll print the max speed of car message by using the printLn method. And after the plus sign, I write the car max speed, final variable. And lastly, I'll return the variable max car speed, and I will save the code. Now, let's create the main test class named abstract test in the same package. In the main method, I'll create a car object. Car, myCar equal 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 overridden start and stop methods from subclass car using the myCar object. And lastly, let's call the overridden, abstract getMaxSpeed method from subclass car. 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. Yes, this is how the abstract class is used. Let's take a look at the things to keep in mind about abstract classes. An instance of an abstract class cannot be created. You can create constructors in the abstract classes. We can have an abstract class without any abstract method. There can be a final method in an abstract class, but an abstract method cannot be declared as final or private. We are not allowed to create objects for an abstract class. We can define static methods in an abstract class. We can use the abstract keyword for declaring top level classes, outer classes, as well as inner classes as abstract. If a class contains at least one abstract method, then compulsory should declare a class as abstract. I think the abstract class is understood. Let's take a short break here. See you in the next lesson.

 

About the Author
Students
81
Courses
18
Learning Paths
1

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.

Covered Topics