Java Reference
In-Depth Information
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
You have to manually define the constructor that initializes all fields, two getters, and two
setters. A simple class now has more than 20 lines of code! Several IDEs and tools can help you
generate this code, but your codebase still has to deal with a large amount of additional code
that's not very useful compared to real business logic.
In Scala, constructors, getters, and setters can be implicitly generated, which results in code
with less verbosity:
15.3.2. Scala traits vs. Java 8 interfaces
Scala has another useful feature for abstraction called traits . They're Scala's replacement for
Java's interfaces. A trait can define both abstract methods and methods with a default
implementation. Traits can also be multiple inherited just like interfaces in Java, so you can see
them as similar to Java 8's interfaces that support default methods. Traits can also contain fields
like an abstract class, which Java 8 interfaces don't support. Are traits just like abstract classes?
 
Search WWH ::




Custom Search