Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Creating a Class in Scala

Sumit Rawal answered on May 23, 2023 Popularity 7/10 Helpfulness 2/10

Contents


More Related Answers

  • What are case classes in Scala?
  • scala tutorial
  • Scala is Object-Oriented#
  • Functions in Scala#
  • Control Structures in Scala
  • Scala’s Implementation#
  • Scala Tutorial - Learn How To Create And Extend Trait In Sca
  • Difference between object and class in Scala
  • Create your own Types in scala Using Classes:
  • Create your own Types in scala Using Case Classes

  • Creating a Class in Scala

    0

    Introduction

    In a previous lesson, you were introduced to classes and objects. We looked at some of Scala’s built-in classes such as Seq. But you can make your own classes and, in this chapter, we will cover user-defined classes and learn how to write our very own classes and objects. We will pick up where we left off in the introductory lesson; it is recommended you go over the lesson once more before moving on.

    Without further ado, let’s create our own Person class.

    Defining a Class

    To define a class in Scala, the class keyword is used, followed by an identifier (class name) of our choosing.

    Let’s map the syntax to our Person class.

    1

    class Person

    At this point, our class doesn’t have any properties or methods.

    Defining the Properties of a Class

    Properties are variables and in Scala, they are called fields. They are also known as instance variables because every instance gets its own set of the variables.

    All the members of a class, including fields, are placed in a block.


    Our Person class has three fields: name, gender, and age. 

    Popularity 7/10 Helpfulness 2/10 Language scala
    Source: Grepper
    Tags: class scala
    Link to this answer
    Share Copy Link
    Contributed on May 23 2023
    Sumit Rawal
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.