Updated April 6, 2023
Definition of Scala Abstract Class
The concept of abstract class comes from Abstraction. It is a process in which we show only the services to the user and hide the implementation of those services from the user. They just use these services and perform their task irrespective of what is happing in the background. The whole idea of Scala abstract class is the same as Java abstract class, it is also used to achieve abstraction in Scala.
Syntax:
abstract class your_class_name{
// define methods
}
The above syntax shows how we can define an abstract class in Scala it is quite similar to java. In Scala, we use the abstract keyword to create any abstract class. In this class, we can define our own methods that can be abstract or non-abstract in nature. In support of both the method type i.e. abstract and non-abstract.
abstract class Demo{
}
How does Abstract Class Work in Scala?
Abstraction is the process of hiding the complexity from the user and shows them only the services that we have created for them. Complexity contains the coding part and actual implementation of the service which internally uses many of the other services which the user is not aware of, it is not the duty of the user to know the internal working.
Abstraction can be achieved in two ways.
- Abstract method
- Non-abstract method
- Interface
But as we can see abstract class does not provide us full abstraction because it contains abstract and non- abstract methods. Here we will talk about the abstract and non-abstract methods in detail.
1. Abstract Methods
Those methods which do not contain the method implementation with them are known as abstract methods. We do no need to provide the method body when we are defining an abstract methods inside a class.
Syntax:
abstract class Demo {
def getMessage()
}
In the above example, we are just defining the method signature into the class. We are not responsible to provide the implementation for this method. The classes which are extending this class can override this method and provide the implementation as they needed according to their need. So in this manner, we achieve abstraction in Scala somewhat similar to java’s only difference about the syntax that we are following.
2. Non-abstract Methods
These methods are those methods that define their implementation as well at the time of declaring themselves. They contain their body that means the actual logic inside them, so we can say that this logic is not hiding with the user because we are providing the implementation so they cannot give us full abstraction.
Syntax:
abstract class Demo {
def getMessage(){
// you can write your logic here
}
}
In the above example, we are defining the body of the method as well. So, in this manner, we can define the non-abstract methods in Scala inside a class.
3. Interface
But by using the interface we can achieve 100% abstraction. In Scala, we use trait keyword to define an interface just like java.
Syntax:
trait Demo {
def start(): Unit
def end(): Unit
}
so the above code shows that if any class is trying to implement this interface then they need to provide the implementation of these methods as well. So now we will look at some practical usage of Abstraction in Scala. suppose we have one method that is required by so many classes that are the child class for this Parent class. But these classes do not want to use the logic that is being written because the logic or implementation or we can say the business requirements is different for all the childes. Every child wants their specific implementation of that method. So now the abstraction comes into the picture.
What we can do in this case is like we can create one Parent Class and define one abstract method in it and now the classes which are going to extend this Parent class can provide their own implementation of this method by overriding them. So now the implementation of each child would be hidden from other classes as well thus helps in security as well.
Examples of Scala Abstract Class
We have one Bank as a parent bank for all its sub banks. But the benefits they are providing to their customers vary from each other like the rate of interest they are giving is different from bank to bank. so, in this case, we can have our print class as Bank and all other child classes are like SBI, ICICI, IDBI, BOI, and many more.
1. Specific Implementation to Method
Code:
abstract class Bank
{
def rateofInterest()
}
class SBI extends Bank
{
def rateofInterest()
{
println("At SBi rate of intereset is 5%")
}
}
class ICICI extends Bank {
def rateofInterest(){
println("At ICICI rate of interest is 3%")
}
}
class IDBI extends Bank {
def rateofInterest(){
println("At IDBI rate of intereset is 2%")
}
}
class BOI extends Bank {
def rateofInterest(){
println("At BOI rate of intereset is 6%")
}
}
object Main
{
// This method will execute the method code by calling them form object
def main(args: Array[String])
{
// objects of All child class
var obj = new SBI()
obj.rateofInterest()
var obj1 = new ICICI()
obj1.rateofInterest()
var obj2 = new IDBI()
obj2.rateofInterest()
var obj3 = new BOI()
obj3.rateofInterest()
}
}
Output:
2. Maintain security
abstract class Animal
{
def differentAnimalType()
}
class Cat extends Animal
{
def differentAnimalType()
{
println("Hello i am Cat here //")
}
}
class Dog extends Animal {
def differentAnimalType(){
println("Hello i am Dog here //")
}
}
class Snack extends Animal {
def differentAnimalType(){
println("Hello i am Snack here //")
}
}
object Main
{
def main(args: Array[String])
{
// objects of GFG class
var obj = new Cat()
obj.differentAnimalType()
var obj1 = new Dog()
obj1.differentAnimalType()
var obj2 = new Snack()
obj2.differentAnimalType()
}
}
Output:
Recommended Articles
We hope that this EDUCBA information on “Scala Abstract Class” was beneficial to you. You can view EDUCBA’s recommended articles for more information.