Abstraction is the process to hide the internal details and showing only the functionality. In Scala, abstraction is achieved by using an abstract class. The working of the Scala abstract class is similar to Java abstract class. In Scala, an abstract class is constructed using the abstract keyword. It contains both abstract and non-abstract methods and cannot support multiple inheritances. A class can extend only one abstract class.
Syntax:
abstract class class_name
{
// code..
}
The abstract methods of abstract class are those methods which do not contain any implementation. Or in other words, the method which does not contain body is known as an abstract method.
Syntax:
def function_name()
Example:
abstract class myauthor
{
def details()
}
class GFG extends myauthor
{
def details()
{
println( "Author name: Ankita Saini" )
println( "Topic name: Abstract class in Scala" )
}
}
object Main
{
def main(args : Array[String])
{
var obj = new GFG()
obj.details()
}
}
|
Output:
Author name: Ankita Saini
Topic name: Abstract class in Scala
Following are some important observations about abstract classes in Scala.
When to use abstract class in Scala:
An abstract class is useful:
- When we want to construct a base class which needs constructor arguments.
- When our code will be called from Java code.
Note:Traits are also used to achieve abstraction.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
31 Oct, 2019
Like Article
Save Article