All you need to know about Classes and Objects in Scala

Reading Time: 3 minutes

Class

A class is a user-defined blueprint or prototype which creates the objects. Or in other words, a class combines the fields and methods(member function which defines actions) into a single unit. Classes in Scala are blueprints for creating objects. They can contain methods, values, variables, types, objects, traits, and classes which are collectively called members.

Declaration of class

In Scala, a class declaration contains the class keyword, followed by an identifier(name) of the class.

In general, class declarations can include these components, in order: 

  • Keyword class: We use class keyword to declare the type class.
  • Class name: The name should begin with a initial letter (capitalized by convention).
  • Superclass(if any):The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
  • Traits(if any): A comma-separated list of traits implemented by the class, if any, preceded by the keyword extends. A class can implement more than one trait.
  • Body: The class body is surrounds by { } (curly braces).

Syntax:  

class Class_name{
// methods and fields
}

Extending the class

Extending a class in Scala user can design an inherited class. To extend a class in Scala we use extends keyword. there are two restrictions to extend a class in Scala which are as follows:

  • We use override keyword to override the method.
  • Only the primary constructor can pass parameters to the base constructor.

Syntax:

class base_class_name extends derived_class_name
{
    // Methods and fields
}
Output:-

Implicit Classes

Implicit classes allow implicit conversations with the class’s primary constructor when the class is in scope. An implicit class is a class that can be marked with an ‘implicit’ keyword.

Syntax − The following is the syntax for implicit classes. Here implicit class is always in the object scope where all method definitions are allowed because the implicit class cannot be a top-level class.

Syntax

object <object name> {
   implicit class <class name>(<Variable>: Data type) {
      def <method>(): Unit =
   }
}

Objects

It is a basic unit of Object-Oriented Programming and represents real-life entities. A typical Scala program creates many objects, which as you know, interact by invoking methods. An object consists of :  

  • State: It is represented by attributes of an object. It also reflects the properties of an object.
  • Behavior: It is represented by methods of an object. It also reflects the response of an object with other objects.
  • Identity: It gives a unique name to an object and enables one object to interact with other objects.

In Scala, We create an object by using the new keyword. The syntax of creating an object in Scala is: 

Syntax:  

val obj = new Person1()

Initializing an object

The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor. 

Output:-

Anonymous Object

Anonymous objects are objects that instantiate but do not contain any reference, you can create an anonymous object when you do not want to reuse it. 

Output:-

Conclusion

By Reading this blog you will get to know about what is class and what are objects in Scala. How can we Extend the class and How to initialize the objects in Scala

References:-

https://www.geeksforgeeks.org/class-and-object-in-scala/

https://www.tutorialspoint.com/scala/scala_classes_objects.htm

https://blog.knoldus.com/