Abstract Classes in Scala
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:
// Scala program to illustrate how to // create an abstract class // Abstract class abstract class myauthor { // abstract method def details() } // GFG class extends abstract class class GFG extends myauthor { def details() { println("Author name: Ankita Saini") println("Topic name: Abstract class in Scala") } } object Main { // Main method def main(args: Array[String]) { // objects of GFG class 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.
- Like Java, in Scala, we are not allowed to create the instance of the abstract class. If we try to create objects of the abstract class, then the compiler will give an error as shown in the below program.
Example:// Scala program to illustrate// the concept of abstract class// Abstract classabstractclassmyauthor{// abstract methoddefdetails()}objectMain {// Main methoddefmain(args:Array[String]) {// Object of myauthor classvarobj=newmyauthor()}}chevron_rightfilter_noneOutput:
prog.scala:18: error: class myauthor is abstract; cannot be instantiated
var obj = new myauthor()
^
one error found - In Scala, an abstract class can also contain fields. These fields are accessed by the abstract class methods and by the methods of the class which inherit abstract class. As shown in the below program.
Example:// Scala program to illustrate// the concept of abstract class// Abstract class with fieldsabstractclassGeek{varname:String="GeeksforGeeks"vartutorial:String="Scala"defportal()}// GFG class extends abstract classclassGFGextendsGeek{// Abstract class method accessing// fields of the abstract classdefportal(){println("Portal name: "+ name)}// GFG class method accessing// fields of the abstract classdeftutdetails(){println("Tutorial name: "+ tutorial)}}objectMain{// Main methoddefmain(args:Array[String]){// objects of GFG classvarobj=newGFG()obj.portal()obj.tutdetails()}}chevron_rightfilter_noneOutput:
Portal name: GeeksforGeeks Tutorial name: Scala
- Like Java, In Scala, an abstract class can also contain a constructor and a constructor of an abstract class is called when an instance of a inherited class is created. As shown in the below program.
Example:// Scala program to illustrate// the concept of abstract class// Abstract class with constructor// And the constructor conatin two argumentsabstractclassmyauthor(name:String,topic:String){defdetails()}// GFG class extends abstract classclassGFG(name:String, topic:String)extendsmyauthor(name, topic){defdetails(){println("Author name: "+ name)println("Topic name: "+ topic)}}objectMain{// Main methoddefmain(args:Array[String]){// objects of GFG classvarobj=newGFG("Ankita","Abstract class")obj.details()}}chevron_rightfilter_noneOutput:
Author name: Ankita Topic name: Abstract class
- An abstract class can also contain only non- abstract method. This allows us to create classes that cannot be instantiated, but can only be inherited. As shown in the below program.
Example:// Scala program to illustrate// the concept of abstract class// Abstract class with// non-abstract methodabstractclassmyauthor{// Non-abstract methoddefdetails(){println("Welcome to GeeksforGeeks")}}// GFG class extends abstract classclassGFGextendsmyauthor{}objectMain{// Main methoddefmain(args:Array[String]){// objects of GFG classvarobj=newGFG()obj.details()}}chevron_rightfilter_noneOutput:
Welcome to GeeksforGeeks
- In Scala, an abstract class can contain final methods (methods that cannot be overridden). For example, the following program compiles and runs without an error. In Scala, final method is created using final keyword.
Example:// Scala program to illustrate// the concept of abstract class// Abstract class with the final methodabstractclassmyauthor{finaldefmymethod(){println("Final method")}}// GFG class extends abstract classclassGFGextendsmyauthor{}objectMain{// Main methoddefmain(args:Array[String]){// objects of GFG classvarobj=newGFG()obj.mymethod()}}chevron_rightfilter_noneOutput:
Final method
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.
Recommended Posts:
- Difference Between Traits and Abstract Classes in Scala
- Scala | Abstract Type members
- Scala | Value Classes
- Generic Classes in Scala
- Set in Scala | Set-1
- Scala | Either
- Scala Map
- Set in Scala | Set-2
- Scala | Literals
- Scala | Option
- StringBuilder in Scala
- Scala | Variances
- How to get all the values from a Scala map
- Scala | Multithreading
- Scala | Annotation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



