EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Scala Class

By Arpit AnandArpit Anand

Home » Software Development » Software Development Tutorials » Scala Tutorial » Scala Class

Scala Class

Introduction to Scala Class

Scala Class is basically a user defined blueprint having methods and fields into a single unit from which object can be created. Class is just like a home for the various methods and global variables defined that are used within its scope. We can create an object from the class using the new keyword (by initialing it calling ) using that for calling the methods inside a class. There are fields in a class providing the class state and there are methods defined inside the class that implements the behavior of a class. With the help of Object-defined, we can use all the functionalities defined in a Class.

Class Declaration

A class in scala is defined with the Keyword Class, followed by the name of the class. Starting with opening the curly braces it contains the body of the class ending with the braces closed thereafter. The class can have some optional parameters also with any superclass extension or trait defined. The public is the default modifier used for the class.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

class Class_name{
// body with method and fields.
}

Example

Class Animal
{
//Method Body
//Variable Declaration
}

Output:

Defined class Animal

Scala Class-1.1

How does Class Work in Scala?

After the declaration of class in scala, we have all the methods and variables that we need to use that. So we need to create an object when an object of a class is created the class is known to be as instantiated. Once that is done we will be able to take the values and use the method used in the class.

We can create more than one instance for a class, the new keyword is used to define objects in Scala, once that object is created we can access the methods inside the class and can implement it in our code.

Let’s see how to call the class methods with objects.

scala> class Animal{
| defAn()
| {
| println("hello")
| }
| }
defined class Animal

Popular Course in this category
Scala Programming Training (3 Courses,1Project)3 Online Courses | 9+ Hours | Verifiable Certificate of Completion | Lifetime Validity | 1 Project
4.5 (5,556 ratings)
Course Price

View Course

Related Courses
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)All in One Software Development Bundle (600+ Courses, 50+ projects)

Here we have declared a class named with Animal with a method An inside it.

scala>val e = new Animal()

e: Animal = Animal@45a4b042

Then Here in the vale, we are making a new object which can access the methods of the CLASS.

scala>e.An
hello

And then we will just call that An method which we can call with the object e and get the functionalities of that method. So it will print the Hello Message. Since the default access modifier for the elements in a class in public but we can make it private also to restrict the access for that within the class.

Scala Class-1.2

So memory is allocated and a reference is returned invoking the class constructor whenever a new object is created using the new operator. We can create anonymous objects also, anonymous objects are those which doesn’t have any reference.

So constructors are basically called up in a Scala Class when an object is instantiated. Scala Constructors being of two types primary and auxiliary have the same body as that of class so anything inside the class will be the part of that Constructor. So if no constructors are defined scala will automatically take this primary constructor whenever an object is created and that the constructor is called as Default constructor.

Few Points that we need to check about this default class constructor is:-

  • If values defined inside are var then we can change the value for that.
  • If the values defined are val inside then it will be considered as final and the values cannot be changed.
  • If there is nothing defined (val,var) it will have restricted visibility.
  • We can also use the access modifier while defining the values inside so that will work accordingly as per the definition of the access modifiers.

Let us check this with an example:

scala> class Animals(var a:Int , var b : Int){
| println("hello")
| }
defined class Animals

This will define a class with primary constructor Animals and whenever the object is created the resulted value is called.

scala>val b = new Animals(2,3)
hello
b: Animals = Animals@a8e6492

Whereas on the other hand if we want to have some other constructors apart from primary are known as an auxiliary constructor. We can create as many as an auxiliary constructor in our program but the primary constructor needs to be one.

The auxiliary constructor comes up with a different signature list. The auxiliary constructor comes up with a constructor call this.

classNo_Distinct(i : Int , j:Int)
{
vark :Int = 0
println("Hello")
println(i)
println(j)
println(k)
defthis(i:Int ,j:Int,k:Int)   //Auxiliary Constructor
{
this(i,j)        //Primary Constructor
this.k = k
}
}

If we will call this by creating the object, the Auxiliary Constructor will call both and will do what the method has in it.

Output:

Val o = new No_Distinct(3,4,0)

Scala Class-1.3

So from this, we see how Scala Class is useful and helpful in the object-oriented programming model.

Conclusion

Here from the above article, we came across the Scala Class and the feature it exhibits. Having a Class is a very important concept for the Scala Object-Oriented Programming model. Here with the help of Examples, we got to know about the methods a class can have with the functionalities it adheres.

Recommended Articles

This is a guide to Scala Class. Here we also discuss the introduction and how a class works in scala along with examples and its code implementation. You may also have a look at the following articles to learn more –

  1. Scala Abstract Class
  2. Scala if else
  3. Scala High Order Functions
  4. Scala Collections

Scala Programming Training (3 Courses)

3 Online Courses

9+ Hours

Verifiable Certificate of Completion

Lifetime Validity

1 Project

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Scala Tutorial
  • Advanced
    • Constructors in Scala
    • Scala Class Constructor
    • Scala Abstract Class
    • Scala String
    • Scala Collections
    • Scala High Order Functions
    • Scala Date Function
    • Scala Stream
    • Scala Class
    • Scala Function
    • Scala Closure
    • Scala Range
    • Scala Either
    • Scala Random
    • Scala Tuples
    • Scala Partial Function
    • Scala Split
    • Scala Finds
    • Scala List
    • Scala Trait
    • Scala Try Catch
    • Scala Regex
    • Scala Vector
    • Scala Seq
    • Scala Byte
    • Scala ListBuffer
    • Scala ArrayBuffer
    • Scala flatMap
    • Scala HashMap
    • Scala Map Function
    • Scala SortBy
    • Scala Some
    • Scala Generic
    • Scala getOrElse
    • Scala REPL
    • Scala Logging
    • Scala Lambda
    • Scala Queue
    • Scala Set
    • Scala Singleton
    • Scala groupBy
    • Scala reduce
    • Scala filter
    • Scala Option
    • Scala Native
    • Scala DataFrame
    • Scala Anonymous Function
    • Scala Read File
    • Scala Write to File
    • Scala Pattern Matching
    • Scala ClassTag
    • Scala Synchronized
    • Scala zipwithindex
    • Scala Interview Questions
  • Basics
    • What is Scala?
    • Uses of Scala
    • Scala Versions
    • Scala Data Types
    • Scala Companion Object
    • Scala Operators
    • Scala: The Functional Approach
    • Scala Object
    • Scala Implicit
    • Scala IDE
  • Control Flow
    • Scala if else
    • Break in Scala
    • Scala for Loop
    • Scala foreach
    • Scala While Loops

Related Courses

Scala Programming Training Course

Programming Language Course

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Scala Programming Training (3 Courses) Learn More