How to Implement Scala Abstract Class with Examples

Scala course with real-time projects Start Now!!

In this Scala tutorial, we will discuss Scala abstract class. Moreover, we will study different examples of Scala Abstract Classes. In addition, we will look at the importance of Abstract Class in Scala Programming Language. Along with this, we will discuss how to implement Scala Abstract Class.

So, let’s discuss What is Abstract Class In Scala.

Why do we need Scala Abstract Class?

Abstraction is often the answer to TMI (Too Much Information). It applies to almost everything in life and keeps us from insanity. And in case you were wondering, abstraction is hiding unnecessary details and putting forward only what interests the subject. So, how does this relate to Scala? Let’s find out.

Scala abstract class is one that can have both abstract and non-abstract methods. An abstract method is one without a definition right away. So that means, abstract class lets us declare a method for a class without a definition. We can tell the compiler that the class is supposed to have a particular method, but we may not be sure what to put in that right away. Or we may also want to use this for inheritance.

When more than one class inherits from a superclass, the superclass may not want to provide a definition for a certain method, as we may not want to create an instance of it to call the method on. We can then declare the method abstract. Also, you cannot instantiate Scala abstract class. In simple words, you cannot create an object of it. A class can only inherit from one Scala abstract class.

Let’s Explore Scala Iterator in Detail

How to Implement Scala Abstract Class?

To implement Scala abstract class, we use the keyword ‘abstract’ against its declaration. It is also mandatory for a child to implement all abstract methods of the parent class.

scala> abstract class Person{
| def greet()
| }
defined class Person
scala> class Student extends Person{
| def greet(){
| println("Hi")
| }
| }
defined class Student
scala> var s=new Student()
s: Student = Student@3ceeb6ba
scala> s.greet()

Hi
We can also use traits to implement abstraction; we will see that later.

Failed Attempts

The following codes will not work, and hence, they tell us about the nature of Scala abstract classes.

a. Instantiating

scala> abstract class Person{
| def greet()
| }
defined class Person
scala> var p=new Person()
<console>:12: error: class Person is abstract; cannot be instantiated
var p=new Person()
^

Let’s Discuss Scala String Interpolation – s String, f String and raw string Interpolator

b. Implementing less than all abstract methods

scala> abstract class Person{
| def greet()
| def greethello()
| }
defined class Person
scala> class Student extends Person{
| def greethello(){
| println("Hello")
| }
| }
<console>:12: error: class Student needs to be abstract, since method greet in class Person of type ()Unit is not defined
class Student extends Person{

c. Two levels deep

Although not exactly a failed attempt, but if a subclass must skip the implementation of an abstract method from its parent, it must itself be marked abstract.

scala> abstract class Person{
| def greet()
| def greethello()
| }
defined class Person
scala> abstract class Student extends Person{
| def greet(){
| println("Hi")
| }
| def greethello()
| }
defined class Student

In this example, we cannot instantiate either of these classes since both are abstract.
Let’s Discus Scala Access Modifiers: Public, Private and Protected Members

Scala Abstract Class Example

In this example, we also include a constructor and variables.

scala> abstract class Person(age:Int){
| var SSN:String=""
| def greet()
| def greethello(){
| println("Hello")
| }
| }
defined class Person
scala> class Student(age:Int) extends Person(age){
| SSN="323-44-798"
| def greet(){
| println("Hi, I'm a student")
| println("I am "+age+" years old")
| println("My social security number is "+SSN)
| }
| }
defined class Student
scala> var s=new Student(18)
s: Student = Student@13de62ef
scala> s.greet()

Hi, I’m a student
I am 18 years old
My social security number is 323-44-798

scala> s.greethello()

Hello
Let’s Look at Scala do while Loop – A Quick and Easy Tutorial

So, this was all about Abstract Class in Scala. Hope you like our explanation.

Conclusion

Hence, in this Scala abstract class tutorial, we study how to use the ‘abstract’ keyword to declare a class abstract. In addition, we talked about Scala abstract class example and how can we implement Scala abstract classes. Furthermore, if have any query, tell us in the comments.

Related Topic- Scala While Loop with Syntax and Example

For reference

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

DataFlair Team

The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.

Leave a Reply

Your email address will not be published. Required fields are marked *