Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala's sealed abstract vs abstract class

What is the difference between sealed abstract and abstract Scala class?

like image 867
Łukasz Lew Avatar asked Jun 13 '10 15:06

Łukasz Lew


People also ask

What is the difference between abstract class and sealed class?

1)Sealed class cannot be inherited by a normal class. 1)Abstract class must be inherited by a class. 2)Instance must be used for Sealed class for accessing its public methods. 2)Instance cannot be created for Abstract class and it should be inherited for accessing its abstract methods.

Can abstract class be a sealed class?

When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.

What is difference between abstract class and trait in Scala?

Abstract Class supports single inheritance only. Trait can be added to an object instance. Abstract class cannot be added to an object instance. Trait cannot have parameters in its constructors.

Can we define abstract or static class as sealed?

static class cannot be marked sealed because it is made sealed by compiler by default. Static classes are sealed and therefore cannot be inherited. static class cannot be marked as abstract , because it would be pointless. abstract class makes sense when you want all derived classes to implement same part of the logic.


1 Answers

The difference is that all subclasses of a sealed class (whether it's abstract or not) must be in the same file as the sealed class.

like image 164
sepp2k Avatar answered Sep 21 '22 23:09

sepp2k