What is Abstract Class in Scala?
In Scala, an abstract class serves as a supertype that contains information about subtype classes. To achieve abstraction, the abstract class is created. Abstraction means hiding the information of the implementation and showing only the specific functionality to the end-users. We can only inherit one abstract of a class to instantiate correctly. An abstract class cannot be instantiated directly. To construct an abstract class, we have to represent a class with the keyword Abstract.
Syntax of the Abstract Class in Scala
The syntax of Scala Abstract Class looks this way.
The abstract is represented with the keyword “abstract” with the class name. In abstract class, we have stated abstract methods. The abstract method is without having code in its body.
How to Use Abstract Class in Scala in Ubuntu 20.04
Let’s take an example and see how to form an abstract class along with an abstract method in Scala.
Example # 1: Creating an Abstract Class in Scala
We can create an abstract class in a program by using the “Abstract” keyword with the class name specified. Let’s have some hands-on creating the abstract class.
In the above code, we have declared a class with the keyword “abstract ” and named that class as “Student”. In the abstract class, we have defined abstract methods as “information”. When we define an abstract method in an abstract class, then we don’t need any implementation there. After creating the Abstract class, we have created a regular class named “Degree”. This class “Degree” extends the abstract class. Here, the abstract method “information” displays the student name and Degree name. Then, we have a main method in which we have to define the “Degree” class object. We have created a variable obj and assigned that to the instance of the “Degree” class. We have used a new keyword to instantiate the class. We are using the obj instance with the “information” method called.
We have successfully created an abstract class and defined abstract methods in a regular class. The output of the abstract method of abstract class through print statement is on the terminal screen.
Example # 2: Creating an Abstract Class Instance in Scala
In Scala, we can’t instantiate an Abstract class explicitly. If we try to do so, we will have a compilation error. Below, we are trying to instantiate an Abstract class by creating an instance of the Abstract to know what error message it will give.
In the above code implementation, we have created an abstract class with the “abstract” keyword and given the name of the class as “University”. Then, without a body, we have defined the Abstract method. The abstract method is employed in the main method. We created an object of the “University” class in the main function of the abstract class. Then, we have assigned that object to the instance of the abstract class “University” with the new keyword. This will instantiate the abstract class.
We have an error message as an output that we cannot instantiate the abstract class. As a result, we are unable to generate objects of abstract classes.
Example # 3: Creating fields in Abstract Class in Scala
We can also define fields in abstract class. The abstract class accessed these fields and then, the abstract method of the class inherited the abstract class. Below is the implementation of the code shown.
In the above code, we have created an Abstract class with the keyword “abstract” and named that class “teachers”. In the abstract class, the fields contain a variable as “teacher_name” which is initialized with the string value and has a set datatype string. The second field is also with the variable defined as “teacher_subject” which is also set as a string data type and initialized with the string value. Here, the “teacher_detail” is an abstract method of abstract class “teachers”. Now, “CS” is declared as a class that extends the abstract class. In this class, we are accessing the abstract class method and fields of the abstract class by calling the abstract method. We created an object to instantiate the class. Then, we are using an obj instance with the class method of “CS”.
We have the teacher’s name and teacher’s details as an output below.
Example # 4: Creating a Constructor in Scala
We can make an abstract class constructor in the code. We can use the constructor to initialize an object but the object cannot be built there. When we create an instance of an inherited class, then we can call a constructor of an abstract class.
In the above code, we have declared an abstract class as “Book” along with the constructor. The constructor of an abstract class takes two arguments as “Title” and “Author” then, in the abstract class, we have defined abstract method detail. Now, extend the abstract class with the class “Novel”. The class “Novel” will access the abstract method and return the constructor valuesDisplaying the output of the above implementation code.
As we have discussed the importance of the Abstract class, it is very useful in Scala. We have a brief introduction and definition of an Abstract class in Scala. Also, we have gone through the different example code implementations. We had a deep discussion on the Abstract class in Scala. You will have a strong grip on the Abstract class in Scala after reading the article.