- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Traits and Abstract Classes in Scala.
Traits
Traits are similar to interfaces in Java and are created using trait keyword.
Abstract Class
Abstract Class is similar to abstract classes in Java and are created using abstract keyword.
Example
Following is the program in Scala to show the usage of Traits and Abstract Classes.
trait SampleTrait { // Abstract method def test // Non-Abstract method def tutorials() { println("Traits tutorials") } } abstract class SampleAbstractClass { // Abstract method def test // Non-abstract meythod def tutorials() { println("Abstract Class tutorial") } } class Tester extends SampleAbstractClass { def test() { println("Welcome to Tutorialspoint") } } class TraitTester extends SampleTrait { def test() { println("Welcome to Tutorialspoint") } } object HelloWorld { // Main method def main(args: Array[String]) { var obj = new Tester() obj.tutorials() obj.test() var obj1 = new TraitTester() obj1.tutorials() obj1.test() } }
Output
Abstract Class tutorial Welcome to Tutorialspoint Traits tutorials Welcome to Tutorialspoint
Following are some of the important differences between Traits and Abstract Classes in Scala.
Sr. No. | Key | Trait | Abstract Class |
---|---|---|---|
1 | Multiple inheritance | Trait supports multiple inheritance. | Abstract Class supports single inheritance only. |
2 | Instance | Trait can be added to an object instance. | Abstract class cannot be added to an object instance. |
3 | Constructor parameters | Trait cannot have parameters in its constructors. | Abstract class can have parameterised constructor. |
4 | Interoperability | Traits are interoperable with java if they don't have any implementation. | Abstract classes are interoperable with java without any restriction. |
5 | Stackability | Traits are stackable and are dynamically bound. | Abstract classes are not stacable and are statically bound. |
- Related Articles
- What is the difference between interfaces and abstract classes in Java?
- Abstract Method and Classes in Java
- Abstract Classes in Java
- Abstract Classes in C#
- What is the Difference Between Scala and Python?
- Abstract Classes in Dart Programming
- Difference between abstract class and interface
- Pure Virtual Functions and Abstract Classes in C++
- Difference between Abstract Class and Interface in Java
- Difference between Abstract Class and Interface in C#
- What are abstract classes in Java?
- What are abstract classes in C#?
- Abstract Base Classes in Python (abc)
- Differences Between Scala and Golang
- Difference between Abstract Class and Interface in C# Program

Advertisements