Scala Object Oriented Programming | Scala Tutorial

1. Objective

In our last Scala tutorial, we discussed Scala Lists with an example. Now you have a good understanding of Scala. In this Scala tutorial, we will discuss Scala Object Oriented Programming. Moreover, we are going to learn Scala Class, Scala Objects, Scala examples. Lastly, we will discuss Anonymous Object.

So, let’s begin Scala Object Oriented Programming.

Scala Object Oriented Programming

Scala Object Oriented Programming

2. Scala Object Oriented Programming

As we know, Scala is purely object-oriented. This means we can create classes and then objects from those classes. Now, let’s see what classes and objects are.

3. Scala Class

Think of a class like a blueprint for all objects of its kind. In other words, it is a collection of objects of the same kind.
A class may hold the following members:

  • Data members
  • Member methods
  • Constructors
  • Blocks
  • Nested classes
  • Information about the superclass

..and more.
Let’s explore Scala operator
In Scala, we must initialize all instance variables. Also, the access scope is public by default. We define the main method in an object. This is where the execution of the program begins.

4. Scala Objects

Moving on to objects, if a class is a blueprint, objects are prototypes of that blueprint. An object is essentially a real-world entity with state and behaviour. An object’s state is its data values at any point in time. Its behaviour is the functionality it performs.
So, Scala object is an instance of a class and is a runtime entity. For a class fruit, an Orange can be an object.

5. Scala Examples

Let’s take an example of Scala Class.

scala> class car{
    | var fuel:Float=0
    | var color:String="Black"
    | }

Defined class car
Now, let’s declare an object for this.

scala> var Brio=new car()
Brio: car = car@7645b7d

6. Example- with a Constructor

Let’s take another example. This time we’ll use a primary constructor to initialize values.

Join DataFlair on Telegram
scala> class person(SSN:Int,Name:String){
    | def sayhi(){
    | println("I'm "+Name+" and my Social Security Number is "+SSN)
    | }
    | }

Defined class person
Scala String Method with Syntax and Method
Now, we create an object for this.

scala> val Ayushi=new person(798798,"Ayushi Sharma")
Ayushi: person = person@73a116d

And finally, we call the method sayhi() on this.

scala> Ayushi.sayhi()

I’m Ayushi Sharma and my Social Security Number is 798798.

Scala Quiz

7. Scala Anonymous Objects

When we want to work with an object that we won’t need to use again, we can declare it anonymously.
Let’s define a new class to say Hi.

scala> class A{
    | def sayhi(){
    | println("Hi")
    | }
    | }

Defined class A
Now, we create an anonymous object of this and call sayhi() on it.

scala> new A().sayhi()
Hi

So, this was all about Scala Object Oriented Programming. Hope you like our explanation.

8. Conclusion

Now that we’re done with the basics of Scala Object Oriented Programming. Hence, in this article, we have discussed Scala Object Oriented Programming, Scala Class, Scala examples. At last, we saw how to create an anonymous object. Furthermore, if you have any query, feel free to ask in the comment section.

For reference

1 Response

  1. Vikash Gupta says:

    Please have a link to the next topic in the blog at the end of every blog.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.