EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Scala Abstract Class

By Shalu SharmaShalu Sharma

Home » Software Development » Software Development Tutorials » Scala Tutorial » Scala Abstract Class

Scala Abstract Class

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

How does Abstract Class Work in Scala

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.

Popular Course in this category
Scala Programming Training (3 Courses,1Project)3 Online Courses | 9+ Hours | Verifiable Certificate of Completion | Lifetime Validity | 1 Project
4.5 (5,556 ratings)
Course Price

View Course

Related Courses
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)All in One Software Development Bundle (600+ Courses, 50+ projects)

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:

Scala Abstract Class-1.1

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:

Scala Abstract Class-1.2

Recommended Articles

This is a guide to Scala Abstract Class. Here we also discuss the definition and how does abstract class works in scala along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. Scala Data Types
  2. Scala List
  3. Scala Companion Object
  4. Scala HashMap

Scala Programming Training (3 Courses)

3 Online Courses

9+ Hours

Verifiable Certificate of Completion

Lifetime Validity

1 Project

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Scala Tutorial
  • Advanced
    • Constructors in Scala
    • Scala Class Constructor
    • Scala Abstract Class
    • Scala String
    • Scala Collections
    • Scala High Order Functions
    • Scala Date Function
    • Scala Stream
    • Scala Class
    • Scala Function
    • Scala Closure
    • Scala Range
    • Scala Either
    • Scala Random
    • Scala Tuples
    • Scala Partial Function
    • Scala Split
    • Scala Finds
    • Scala List
    • Scala Trait
    • Scala Try Catch
    • Scala Regex
    • Scala Vector
    • Scala Seq
    • Scala Byte
    • Scala ListBuffer
    • Scala ArrayBuffer
    • Scala flatMap
    • Scala HashMap
    • Scala Map Function
    • Scala SortBy
    • Scala Some
    • Scala Generic
    • Scala getOrElse
    • Scala REPL
    • Scala Logging
    • Scala Lambda
    • Scala Queue
    • Scala Set
    • Scala Singleton
    • Scala groupBy
    • Scala reduce
    • Scala filter
    • Scala Option
    • Scala Native
    • Scala DataFrame
    • Scala Anonymous Function
    • Scala Read File
    • Scala Write to File
    • Scala Pattern Matching
    • Scala ClassTag
    • Scala Synchronized
    • Scala zipwithindex
    • Scala Interview Questions
  • Basics
    • What is Scala?
    • Uses of Scala
    • Scala Versions
    • Scala Data Types
    • Scala Companion Object
    • Scala Operators
    • Scala: The Functional Approach
    • Scala Object
    • Scala Implicit
    • Scala IDE
  • Control Flow
    • Scala if else
    • Break in Scala
    • Scala for Loop
    • Scala foreach
    • Scala While Loops

Related Courses

Scala Programming Training Course

Programming Language Course

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Scala Programming Training (3 Courses) Learn More