Scala – Objects & Classes

In this tutorial, we will learn about objects and classes in scala which comes under OOPs concepts. In OOP, it is very important to understand what is an object and a class. Let’s understand them in details below.

What is an Object?

OK, so you will also find objects around you. In real world scenario, We can say everything is an object like car, fan, pen etc.An object has 2 characteristic i.e. State and behavior and it can be physical and logical. Let’s understand with examples.

A car has a state (name, color,model etc.) and behavior (driving,applying break, horn etc). Same way pen has a state (amount of ink,lid with on and off feature) and behavior (can be used to write etc.). These are the physical entities.

If we talk about non-physical things or logical objects, you can consider your bank account. A bank account can not be touched physically but has a state (the amount of money in it) and behaviour ( depositing or withdrawing money).

How does it relate to the programming model?

Object oriented programming is programming that involves intellectual objects that helps us to achieve or resolve business problems in real world.

In real world, you will find many objects of the same kind. There are many cars in the world, but each car is built from same set of blueprints and have same components.Class is a blueprint, from which individual objects are created.Consider a class name as CARS, Your car A is an instance of the class CARS , similar way your neighbour’s car B is also instance of the class CARS.

Class Objects

Let’s understand it with programming perspective. Once you define class named as Car, you can create object BMW from the class blueprint with the keyword new.Through that object, you can use all functionalities of the defined class.

In the above program, an object ‘bmw’ is created through class blueprint ‘Car’. There are few points to remember –

  • All Variables must be initialized in the class.
  • If no scope is specifies, it is public.
  • You must create an object which will contain the main method. This is the start point of the program.

Will the below program fail, as object does not have main method defined?

Answer is NO. Because Object is extending Trait App which converts objects into executable program. We will see later what is Trait. As of now, you can just think Traits like interfaces in Java.

Another Example of class is given below, where constructor is created in the class definition. It is called primary constructor. We will learn about constructor in coming chapters.

Scala Anonymous object:

In Scala, we can create anonymous object. An object which does not have any reference name, is called the anonymous object. These objects can be created if we don’t want to use them further. As we have modified the above code and called the method carFeature directly with anonymous object new car.

Scala – OOPs concepts
Scala - Singleton Object
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

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