Scala Object Oriented Programming | Scala Tutorial
Scala course with real-time projects Start Now!!
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
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.
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.
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.
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
Example- with a Constructor
Let’s take another example. This time we’ll use a primary constructor to initialize values.
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 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.
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.
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google
Please have a link to the next topic in the blog at the end of every blog.
I thought the same!
Please have and links on the top of a page