Spark and Scala - Module 3
Spark and Scala - Module 3
ᗍ Classes in Scala
ᗍ Properties with Getters and Setters
ᗍ Properties with only Getters
ᗍ Object-Private Fields
ᗍ Constructors
ᗍ Nested Classes
ᗍ Singletons
ᗍ Companion Objects
ᗍ Apply Method
ᗍ Packages
ᗍ Imports and Implicit Imports
ᗍ Extending a Class
ᗍ Overriding Methods
ᗍ Type Checking and Casting
ᗍ Super Construction
ᗍ Abstract Classes
ᗍ Introducing Traits in Scala
• Fields
• Constructors
• Methods
• Superclasses (inheritance)
• Interfaces implemented by the class, etc.
However, as convention
class add{
var x:Int=10
var y:Int=20
def add(a:Int,b:Int)
{
a=x+1
println(“Value of a after modification :”+a);
}
}
Var p=new add()
p.add(5,10);
a. 5
b. 11
c. 16
d. Error
class add{
var x:Int=10
var y:Int=20
def add(a:Int,b:Int)
{
a=x+1
println(“Value of a after modification :”+a);
}
}
Var p=new add()
p.add(5,10);
a. 5
b. 11
c. 16
d. Error
Error
ᗍ Scala provides the getters and setters for every field by default
ᗍ We define a public field
ᗍ Scala generate a class for the JVM with a private size variable and public getter and setter methods
ᗍ If the field is declared as private, the getters and setters would be private
ᗍ The getters and setter methods in previous case would be:
1. age and age_=
2. Example:
Example:
ᗍ In Scala, the getters and setters are generated for each property
ᗍ For private properties, the getter and setter are private
ᗍ For a val, only getters are generated
ᗍ In Scala you can’t have a read-only property (i.e. only getter, no setter)
ᗍ No getters and setters are generated for object-private fields
Option a and c
• Primary Constructors
• Auxiliary Constructors
Auxiliary Constructor:
ᗍ The auxiliary constructors in Scala are called this. This is different from other languages, where constructors have
the same name as the class
ᗍ Each auxiliary constructor must start with a call to either a previously defined auxiliary constructor or the primary
constructor
Primary Constructor:
ᗍ Every class in Scala has a primary constructor
ᗍ Primary constructor isn’t defined by this method
The parameters for primary constructor are placed immediately after the class name:
The primary constructor executes all the statements in the class definition, as explained below:
ᗍ Scala allows inner classes to be defined, which is to say, classes that are declared inside another class
ᗍ Unlike Java, these classes are not scoped to the containing class, but to the containing object
Example:
ᗍ In many programming languages, we typically have both instance methods and static methods in same class
Here is an example:
ᗍ The class and it’s companion objects need to be in same source file
ᗍ The class and it’s companion object can access each other’s private features
ᗍ The companion object of the class is accessible, but NOT in scope
Example:
a. True
b. False
a. True
b. False
True
ᗍ A Package is a special object which defines a set of member classes, objects and packages
ᗍ In Scala, packages serve the same purpose as in Java: to manage the names in a large program
ᗍ To add the items to a package, they can be included in package statements
Example:
package Skillspeed {
package Courses{
package Scala{
class HelloScala
}} }
Scope Rules:
ᗍ Scope rules for packages in Scala are more consistent than Java
ᗍ Scala packages just like all other scopes
ᗍ Member names could be accessed from enclosing scope, i.e.
package Skillspeed {
package Test1 {
object Hi {
def sayHi = "Hi" }
package Test2 {
class Hello {
def sayHello()
{Hi.sayHi} }}}}
Is equivalent to
Note: In the above example, everything belongs to package Skillspeed.Courses.Scala, but the package
Skillspeed.Courses.Scala is also opened up, so it’s contents could also be referred
Package Visibility
ᗍ In Java, we typically control the access of the class members by public, private or protected
ᗍ In Scala, the same effect could be achieved through qualifiers
Example:
package Skillspeed.Courses.Scala {
class HelloScala {
private[Scala] def sayHi = “Hi !”
} }
package com.spark.skillspeed
class Courses {
private[spark] def sayHi = “Hi !”
}
ᗍ In Scala, imports can be anywhere, instead of being at the top of the file, unlike Java
ᗍ Just like Java, new methods and fields can be introduced or superclass methods or fields could be overridden in
subclasses
ᗍ A class can be declared as final to avoid it being extended
ᗍ Unlike Java, individual field or method could also be marked as final to avoid them being overridden
if (a.isInstanceOf(Person )) {
val b = a.asInstanceOf(Person) // b has the type Person
…
}
if(a.getClass == classOf[Person]) {
……
}
ᗍ Putting the class and constructor together makes a very short code in Scala
ᗍ The Emp class has three parameters, out of which, two are passed to its Superclass Person
ᗍ Traits encapsulate methods and fields definitions we can reuse by mixing them into classes we define
ᗍ Unlike classes inheritance that allow each class to inherit one class only, a class can mix in any number of traits
Trait Definition:
The syntax is the same syntax we use when defining a class. The only difference is using the trait keyword instead of
class