Scala namespaces for definitions
Scala has just two namespaces for definitions in place of Java’s four. Java’s namespaces are fields, methods, types, and packages. Scala’s namespaces are:• values (fields, methods, packages, and singleton objects)
• types (class and trait names)
In Scala it is forbidden to define a field and method with the same name in the same class, whereas it is allowed in Java.
This Java class would compile just fine:
class CompilesFine {
private int f = 0;
public int f() {
return 1;
}
}
But the corresponding Scala class would not compile:
class WontCompile {
private var f = 0
def f = 1
}
The reason Scala places fields and methods into the same namespace is precisely so you can override a parameterless method with a val.Source: Overriding methods and fields
Login in to like
Login in to comment