In Scala, an abstract class is constructed using the abstract keyword. It contains both abstract and non-abstract methods and cannot support multiple inheritances.
Example:
// Scala program to illustrate how to // create an abstract class // Abstract class abstract class Abstclass { // Abstract and non-abstract method def portal def tutorial() { println("Scala tutorial") } } // GFG class extends abstract class class GFG extends Abstclass { def portal() { println("Welcome!! GeeksforGeeks") } } object Main { // Main method def main(args: Array[String]) { // object of GFG class var obj = new GFG (); obj.tutorial() obj.portal() } } |
Output:
Scala tutorial Welcome!! GeeksforGeeks
Like a class, Traits can have methods(both abstract and non-abstract), and fields as its members. Traits are just like interfaces in Java. But they are more powerful than the interface in Java because in the traits we are allowed to implement the members.
Example:
// Scala program to illustrate how to // create traits // traits trait mytrait { // Abstract and non-abstract method def portal def tutorial() { println("Scala tutorial") } } // GFG class extends trait class GFG extends mytrait { def portal() { println("Welcome!! GeeksforGeeks") } } object Main { // Main method def main(args: Array[String]) { // object of GFG class var obj = new GFG (); obj.tutorial() obj.portal() } } |
Output:
Scala tutorial Welcome!! GeeksforGeeks
| Traits | Abstract Class |
|---|---|
| Traits support multiple inheritance. | Abstract class does not support multiple inheritance. |
| We are allowed to add a trait to an object instance. | We are not allowed to add an abstract class to an object instance. |
| Traits does not contain constructor parameters. | Abstract class contain constructor parameters. |
| Traits are completely interoperable only when they do not contain any implementation code. | Abstract class are completely interoperable with Java code. |
| Traits are stackable. So, super calls are dynamically bound. | Abstract class is not stackable. So, super calls are statically bound. |
Recommended Posts:
- Abstract Classes in Scala
- Scala | Traits
- Traits As Stackable Modifications
- Scala | Abstract Type members
- Generic Classes in Scala
- Scala | Value Classes
- Difference between Kotlin and Scala
- Scala Tutorial – Learn Scala with Step By Step Guide
- Differnece Between Haskell and Scala
- Scala SortedMap addString() method with a start, a separator and an end with example
- Class and Object in Scala
- Scala Singleton and Companion Objects
- Scala | Case Class and Case Object
- while and do while Loop in Scala
- Scala | Format and Formatted Method
- Scala | Null, null, Nil, Nothing, None, and Unit
- Scala Console | println, printf and readLine
- Recursive Streams and collection in Scala
- Getters and Setters in Scala
- Scala Iterator mkString() method with a start, a separator and an end with example
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.

