Scala

You are currently auditing this course.
27 / 53

Scala - Class examples

  • A class can be defined as a template/blueprint/structure that describes the behaviors/states that are related to the class.
  • Different instances of a class are called objects.
    For example, a car be a class and different cars created/instantiated based on the car class will be objects of the car class.
  • The objects of a class are created using new keyword.

Example:

class Car(makeOfCar: String, modelOfCar: String, yearOfCar: Int) {
   var make: String = makeOfCar;
   var model: String = modelOfCar;
   var year: Int = yearOfCar;
   var color = "";
   def changeColor(colorOfCar: String) {
      color = colorOfCar;
      println ("New color of car:" + color);
   }
}

object Demo {
   def main(args: Array[String]) {
      var car1 = new Car("Honda", "City", 1996);
      var car2 = new Car("Honda", "Accord", 1999);
      var car3 = new Car("Honda", "Amaze", 2015);
      println("Car details:" + car1.make + " " + car1.model + " " + car1.year + " " + car1.color)
      println("Car details:" + car2.make + " " + car2.model + " " + car2.year + " " + car2.color)
      println("Car details:" + car3.make + " " + car3.model + " " + car3.year + " " + car3.color)
   }
}

No hints are availble for this assesment

Answer is not availble for this assesment

Loading comments...