Class and Object keyword In Scala Programming


Scala is hybrid language contain both functional and object oriented functionality. Scala was created by Martin Odersky.

Scala run on JVM, and you can also use java api with in scala program.

You can write Scala program with two keyword 1. class and 2. object.

Example:

class Car{
   def run(){
     println("running");
   }
}
object App{
   def main(args: Array[String]) {
      println("hello World");
  
      val car = new Car();
      car.run();
   }

}

We know class is a blueprint of object than what is object keyword in scala?

Scala class same as Java Class but scala not gives you any entry method in class, like main method in java. The main method associated with object keyword. You can think of the object keyword as creating a singleton object of a class that is defined implicitly.

Scala program run through object main method. In above example i have called Car class run method from app object main method.

Note: Object (keyword) not contain any constructor.

Inheritance between class and object 

Class can not inherits object but object can inherit a class.

If a class contain main method and this class inherited by any object, so class main method will be use as entry method.

You can also override main method in object and use as entry point.

In below example BMW main will execute.

Example:1

class Car{

}

object BMW extends Car{

    def main(args : Array[String]): Unit = {
      println("BMW main");
    }

}

In below example car main method will execute

Example:2

class Car{
   def main(args : Array[String]): Unit = {
      println("Car main");
   }
}

object BMW extends Car{

}

You need to use override keyword with method definition, if  class and object contain same method otherwise scala will throw exception.

In below example BMW  main will call.

Example 3:

class Car{
 def main(args : Array[String]): Unit = {
      println("Car main");
   }
}

object BMW extends Car{
 override def main(args : Array[String]): Unit = {
      println("BMW  main");
   }
}

Companion Classe and Companion Object

If you define class and object in same file with same name, they known as companion class and object.

object App {
     def main(args: Array[String]): Unit = {
        var carObj = new Car("BMW");
        Car.printCarName(carObj);
 
        carObj.setName("Audi");
        Car.printCarName(carObj);
 
     }
}

//Companion Class
class Car(n: String){
 var name = n;
 
   def setName(nameTemp: String){
      name = nameTemp;
   }
 
}


//Companion Object
object Car{
 
  def printCarName(car: Car){
     println(car.name);
  }
 
}
Output:

BMW

Audi

If you go through above example, you will find printCarName method call by class name Car which is defined in object Car and class Car method setName  called by Car instance.

Its showing static method property if we compare this with java. We can use companion objects to store state and methods that are common to all instances of a class.


KNOLDUS-advt-sticker

Advertisements
This entry was posted in Scala and tagged , , . Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s