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:
abstract class Abstclass
{
def portal
def tutorial()
{
println( "Scala tutorial" )
}
}
class GFG extends Abstclass
{
def portal()
{
println( "Welcome!! GeeksforGeeks" )
}
}
object Main
{
def main(args : Array[String])
{
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:
trait mytrait
{
def portal
def tutorial()
{
println( "Scala tutorial" )
}
}
class GFG extends mytrait
{
def portal()
{
println( "Welcome!! GeeksforGeeks" )
}
}
object Main
{
def main(args : Array[String])
{
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. |
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 :
13 Feb, 2019
Like Article
Save Article