Home / Interview / Scala :: General Questions

Interview :: Scala

21) What is method overloading in Scala?

Scala provides method overloading feature which allows us to define methods of the same name but having different parameters or data types. It helps to optimize code. You can achieve method overloading either by using different parameter list or different types of parameters.

Example

22) What is this in Scala?

In Scala, this is a keyword and used to refer a current object. You can call instance variables, methods, constructors by using this keyword.

Example

23) What is Inheritance?

Inheritance is an object-oriented concept which is used to reusability of code. You can achieve inheritance by using extends keyword. To achieve inheritance, a class must extend to other class. A class which is extended called super or parent class. A class which extends class is called derived or base class.

Example

24) What is method overriding in Scala?

When a subclass has the same name method as defined in the parent class, it is known as method overriding. When subclass wants to provide a specific implementation for the method defined in the parent class, it overrides a method from the parent class.

In Scala, you must use either override keyword or override annotation to override methods from the parent class.

Example

25) What is final in Scala?

Final keyword in Scala is used to prevent inheritance of super class members into the derived class. You can declare the final variable, method, and class also.

Scala Final Variable Example

26) What is the final class in Scala?

In Scala, you can create a final class by using the final keyword. A final class can't be inherited. If you make a class final, it can't be extended further.

Scala Final Class Example

27) What is an abstract class in Scala?

A class which is declared with the abstract keyword is known as an abstract class. An abstract class can have abstract methods and non-abstract methods as well. An abstract class is used to achieve abstraction.

Example

28) What is Scala Trait?

A trait is like an interface with partial implementation. In Scala, the trait is a collection of abstract and non-abstract methods. You can create a trait that can have all abstract methods or some abstract and some non-abstract methods.

Example

29) What is a trait mixins in Scala?

In Scala, "trait mixins" means you can extend any number of traits with a class or abstract class. You can extend only traits or combination of traits and class or traits and abstract class.

It is necessary to maintain the order of mixins otherwise compiler throws an error.

Example

30) What is the access modifier in Scala?

Access modifier is used to define accessibility of data and our code to the outside world. You can apply accessibly to class, trait, data member, member method, and constructor, etc. Scala provides the least accessibility to access to all. You can apply any access modifier to your code according to your requirement.

In Scala, there are only three types of access modifiers.

  1. No modifier
  2. Protected
  3. Private