Classes, Instances, Objects, Companion Object, Scala Application

Class in scala is used to wrap instance level functionality.

class Foo //class

Instance

val foo = new Foo // instance

Note: In scala, we do not call instance of a class as an Object because Object has a special meaning.

Object Scala Object is a Singleton Instance by definition.
SCALA DOES NOT HAVE CLASS LEVEL FUNCTIONALITY (like “static” in java) – Equivalet in scala is Object
Everytime an object is defined, this object will be of its own type (“type person”) and it itself is the only instance.
Objects can be defined in a similar way to that of the classes with the only difference being the constructor parameters.

object Person {
  val N_HANDS = 2 // static in java
}
val person1 = Person // refers to the only instance on Object
val person2 = Person
println(person1 == person2) // returns true

Companion Object
We can define class with the same name as that of object to seperate instance level functionality (Class) from static/class level functionality (Object), and when class and object exists with same name in one file, they are called COMPANION OBJECTS or COMPANIONS

object Person {
  val N_HANDS = 2 // static in java
}
class Person(name: String, val age: Int) {
  // where as name and age are instance specific and hence they are referred in class members
}

Note:
In Java we access static member via class name and instance member via object reference
But in Scala, we access class level memebers via Object and instance level members via the instance of a Class, It turns out to be more Object Oriented than JAVA eventhough its created for functional programming

Scala Application
When a scala object inherts scala.App trait it becomes scala application. The App trait can be used to quickly turn objects into executable programs.

object Main extends App {
  Console.println("Hello World: " + (args mkString ", "))
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Mawazo

Mostly technology with occasional sprinkling of other random thoughts

amintabar

Amir Amintabar's personal page

101 Books

Reading my way through Time Magazine's 100 Greatest Novels since 1923 (plus Ulysses)

Seek, Plunnge and more...

My words, my world...

ARRM Foundation

Do not wait for leaders; do it alone, person to person - Mother Teresa

Executive Management

An unexamined life is not worth living – Socrates

javaproffesionals

A topnotch WordPress.com site

thehandwritinganalyst

Just another WordPress.com site

coding algorithms

"An approximate answer to the right problem is worth a good deal more than an exact answer to an approximate problem." -- John Tukey