distinction between Class and Object in Scala

I - Overview

Objects is  concrete and existing .Objects have states , behaviors and identity
Classes in Scala are static templates that can be instantiated into many objects at runtime.A class is a group of objects that has common properties. It is a template or blueprint from which objects are created :
    => Create object from Class with the keyword new
    => An object is an instance of a class
    => You are an object in Human class

Ex : val you = new Human (you : object ,Human : class) 


II - Detail

1. Class
Create class with the keyword class :
class Human(
 val firstName : String,
 val lastName : String
)

2.Object
Create objects from the class blueprint with the keyword new ( create instane of class) :
val reader = new Human("Brad","Pitt")

Create objects with the keyword object :
object HoaiPT {
 val firstName : String = "Hoai"
 val lastName : String = "PT"
}
This kind of Object  is call singleton objects
HoaiPT object is an instane of class that can have only one instance

III - Time for comment

. . .
Just all for today!
Next time we will discuss more detail about Trait,Class and Object in scala and how to use them.
See you






Comments