Java Reference
In-Depth Information
In comparison with Java, in Scala you don't need to manually provide the curried form of a
function as done here. Scala provides a convenient function-definition syntax to indicate that a
function has multiple curried argument lists.
15.3. Classes and traits
We now look at how classes and interfaces in Java compare to Scala. These two constructs are
paramount to design applications. You'll see that Scala's classes and interfaces can provide more
flexibility than what Java offers.
15.3.1. Less verbosity with Scala classes
Because Scala is also a full object-oriented language, you can create classes and instantiate them
to produce objects. At its most basic form, the syntax to declare and instantiate classes is similar
to Java. For example, here's how to declare a Hello class:
class Hello {
def sayThankYou(){
println("Thanks for reading our book")
}
}
val h = new Hello()
h.sayThankYou()
Getters and setters
It becomes more interesting once you have a class with fields. Have you ever come across a Java
class that purely defines a list of fields, and you've had to declare a long list of getters, setters,
and an appropriate constructor? What a pain! In addition, you'll often see tests for the
implementation of each method. A large amount of code is typically devoted to such classes in
Enterprise Java applications. Consider this simple Student class:
public class Student {
private String name;
private int id;
public Student(String name) {
this.name = name;
 
Search WWH ::




Custom Search