Posts

Showing posts with the label Java

Case class-an Ultra POJO class in Scala

Image
POJO stands for Plain-Old Java Object, a class that will have set of fields and their getters and setters. Pojo class is primarily used as a data structure or data carrier across the layers in the J2EE applications. It is understood as more of a best practice than a language standard to define one using any special keyword like pojo.  Is there any equivalent of such class in Scala? yes there is. But not just for a data structure but with valiant features to cater in various usage.  Case comes with handy  Case class in Scala is used for the purpose of defining an object with immutable data. On the otherhand, when a class is defined as a Case class, a lot of boilerplate code is generated by the Scala compiler. The methods like apply, unapply, tupled, copy, set of accessors and mutators, and objects default methods like toString, hashCode, and equals are part of the generated boilerplate code. A question might pop-up in our mind, Isn’t a case class similar to j...

Trait, an interface in Scala

Trait is the new Interface Scala comes with a new keyword for interface called Trait . An Interface in Java helps us to declare methods and subsequently get implemented in classes. Similarly, Scala provides the same feature through Traits. The syntax for declaring the traits goes with a keyword Trait before the name of the interface. The following snippet shows the example of Trait BaseDoor and the implementation of the same in a class. Here, implementation is done with extends keyword trait BaseDoor{ def open def close def lock def unlock } class FrontDoor extends BaseDoor Strength of Traits over Interface a) Method implementation in Interface Traits comes with some more interesting features over Java interfaces. It’s not only allowing to declare methods but just like abstract class it also to allows implementation of methods. In Java the interfaces do not have method implementation. Only abstract class is allowed to have methods implemented, but Scala allows Trait...

Popular posts from this blog

Trait, an interface in Scala

Singleton class made easy in Scala

Quickstart with Angular 2/4