0% found this document useful (0 votes)
8 views47 pages

Spark and Scala - Module 3

This document outlines Module 3 of a course on Apache Spark and Scala, focusing on introducing traits and object-oriented programming (OOP) concepts in Scala. It covers various topics including classes, properties with getters and setters, constructors, nested classes, singletons, companion objects, and packages. The session aims to provide a comprehensive understanding of these concepts as they apply to Scala programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views47 pages

Spark and Scala - Module 3

This document outlines Module 3 of a course on Apache Spark and Scala, focusing on introducing traits and object-oriented programming (OOP) concepts in Scala. It covers various topics including classes, properties with getters and setters, constructors, nested classes, singletons, companion objects, and packages. The session aims to provide a comprehensive understanding of these concepts as they apply to Scala programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Apache Spark and Scala

Module 3: Introducing Traits and OOPS in Scala

© 2015 BlueCamphor Technologies (P) Ltd.


Course Topics

Module 1 Module 2 Module 3 Module 4


Getting Started / Scala – Essentials and Introducing Traits and Functional Programming
Introduction to Scala Deep Dive OOPS in Scala in Scala

Module 5 Module 6 Module 7 Module 8


Spark and Big Data Advanced Spark Understanding RDDs Shark, SparkSQL and
Concepts Project Discussion

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 2


Session Objectives
This session will help you to understand:

ᗍ 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

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 3


Classes in Scala
ᗍ Classes in Scala are static templates that can be instantiated into many objects at runtime

ᗍ A Class can contain information about:

• Fields
• Constructors
• Methods
• Superclasses (inheritance)
• Interfaces implemented by the class, etc.

Simple class definition in Scala: class Myclass{}

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 4


Classes in Scala (Cont’d)

ᗍ Class in Scala is very much similar to Java or C++


ᗍ Class contains fields and methods

ᗍ In Scala a class is NOT declared as public


ᗍ A source file can contain multiple classes
ᗍ All of the classes could be public

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 5


Classes in Scala (Cont’d)
ᗍ Previous class could be used in usual way

ᗍ Parameter less method could be called with or without parentheses

ᗍ Using any form is programmer’s choice

However, as convention

• Use () for mutator method


• Use no parentheses for accessor method

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 6


Check your Understanding – 1
What is the output of the following code ?

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

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 7


Check your Understanding – Solution
What is the output of the following code ?

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

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 8


Properties with Getters and Setters

ᗍ Getters and Setters are better to expose class properties


ᗍ In Java, we typically keep the instance variables as private and expose the public getters and setters

ᗍ Scala provides the getters and setters for every field by default
ᗍ We define a public field

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 9


Properties with Getters and Setters (Cont’d)

ᗍ 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:

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 10


Properties with only Getters

ᗍ Sometimes we need read-only properties


ᗍ There are two possibilities:
• The property value never changes
• The value is changed indirectly
ᗍ For the first case, we declare the property as val. Scala treats it as final variable and thus generates only getter, no
setter
ᗍ In second case, you need to declare the field as private and provide the getter, as explained below:
Semicolons are optional in Scala

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 11


Object – Private Fields
In Scala (and other languages as well), a method can access the private fields of its class

Example:

ᗍ We can declare the variables as object-private by private[this] qualifier


ᗍ Now the methods can only access the value field of current object
ᗍ For the class-private field, private getter and setter are generated
ᗍ For object-private field, NO getter and setter methods are generated

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 12


Summarizing Properties

ᗍ 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

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 13


Check your Understanding – 2
Which of the following statements are correct about getter() with properties?

a. The property value is changed indirectly


b. The property value is changed directly
c. The property value never changes
d. Option a and c

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 14


Check your Understanding – Solution
Which of the following statements are correct about getter() with properties?

a. The property value is changed indirectly


b. The property value is changed directly
c. The property value never changes
d. Option a and c

Option a and c

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 15


Constructor

ᗍ Constructors in Scala are a bit different than in Java


ᗍ Scala has 2 types of constructors

• 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

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 16


Constructor (Cont’d)

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:

class Greet(message : String ) {


// … code
}

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 17


Constructor (Cont’d)

The primary constructor executes all the statements in the class definition, as explained below:

The println statement is executed for every object creation

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 18


Check your Understanding – 3
Syntax of primary constructor is:

a. Greet(message : String) b. class Greet


{ {
// …code //…code
} }

c. class Greet(message : String ) d. public Greet(message : String)


{ {
// … code //…code
} }

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 19


Check your Understanding – Solution
Syntax of primary constructor is:

a. Greet(message : String) b. class Greet


{ {
// …code //…code
} }

c. class Greet(message : String ) d. public Greet(message : String)


{ {
// … code //…code
} }

class Greet(message : String )


{
// … code
}

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 20


Nested Classes

ᗍ 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:

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 21


Nested Classes (Cont’d)

Accessing using Objects

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 22


Nested Classes (Cont’d)

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 23


Singletons

ᗍ Scala doesn’t have the concept of static methods or fields


ᗍ Instead a Scala class can have what is called a singleton object, or sometime a companion object
ᗍ A singleton object definition looks like a class definition, except instead of the keyword class you use the keyword
object
ᗍ An object defines a single instance of a class
Example:

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 24


Singletons (Cont’d)

ᗍ If we need new eno number, we can call sample.Display()


ᗍ Constructor of Singleton Object is executed when the object is first used
ᗍ An object has all the features of a class
ᗍ There is only one exception: Parameters can't be provided to the constructor

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 25


Singletons (Cont’d)
Singletons can be used in Scala as:

ᗍ When a singleton instance is required for co-ordinating a service


ᗍ When a single immutable instance could be shared for efficiency purposes
ᗍ When an immutable instance is required for utility functions or constants

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 26


Companion Objects
ᗍ When a singleton object is named the same as a class, it is called a companion object. A companion object must
be defined inside the same source file as the class

ᗍ 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

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 27


Apply Method

ᗍ Scala objects typically have an apply method


ᗍ The general form of Apply method is: object(arg1)
ᗍ This is same as object.apply(arg1)

Example:

Now the new Array object can be created as follows:

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 28


Check your Understanding – 4
Companion singletons provide an equivalent to Java's static methods

a. True
b. False

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 29


Check your Understanding – Solution
Companion singletons provide an equivalent to Java's static methods

a. True
b. False

True

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 30


Packages

ᗍ 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
}} }

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 31


Packages (Cont’d)

ᗍ The class HelloScala can be accessed from anywhere as Skillspeed.Courses.Scala.HelloScala


ᗍ Unlike, classes, a package can be defined in multiple files
ᗍ Conversely, a single file can have more than one package

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 32


Packages (Cont’d)

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} }}}}

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 33


Packages (Cont’d)

Top of File Notation:


Instead of nested notation, we could have the package notation at the top of file also
Example:

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

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 34


Packages (Cont’d)

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 !”
} }

The visibility could also be extended to enclosing package:

package com.spark.skillspeed
class Courses {
private[spark] def sayHi = “Hi !”
}

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 35


Imports and Implicit Imports
ᗍ Packages/ classes can be imported in Scala

ᗍ Import serve the same purpose as in Java:


To use short names instead of long ones

ᗍ All the members of a package can be imported as:


• import java.awt._

• Note that “_” is used instead of “*”

ᗍ In Scala, imports can be anywhere, instead of being at the top of the file, unlike Java

ᗍ We can use selectors to import only few members of a package like:


• import java.awt.{Color, Font}

ᗍ Every Scala program implicitly starts with:


• import java.lang._
• import scala._
• import Predef._

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 36


Check your Understanding – 5
import p1.p2.z means:

a. All members of p1 are members of z


b. All members of p2 are members of z
c. The member z of p2, itself member of p1
d. All of the above

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 37


Check your Understanding – Solution
import p1.p2.z means:

a. All members of p1 are members of z


b. All members of p2 are members of z
c. The member z of p2, itself member of p1
d. All of the above

The member z of p2, itself member of p1

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 38


Extending a Class
Just like Java, classes can be extended using extends keyword

class Employee extends


Person{
//code
}

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 39


Extending a Class (Cont’d)

ᗍ 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

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 40


Overriding Methods
When overriding a method we should use the override modifier
ᗍ The override modifier is useful in following scenarios:

• When name of the method being overridden is misspelled


• When a wrong parameter type is provided
• When a new method is introduced in superclass which clashes with a subclass method

ᗍ Invoking superclass method is same as in Java, by super keyword:


ᗍ The following code sample shows how to override the toString() method

Ex: public class Employee extends Person {


……
override def toString=super.toString + “Hi …! “
}

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 41


Type Checking and Casting

ᗍ isInstanceOf method is used to decide whether an object belongs to a class


ᗍ asInstanceOf method is used to convert a reference to a subclass reference

if (a.isInstanceOf(Person )) {
val b = a.asInstanceOf(Person) // b has the type Person

}

ᗍ classOf method is used to determine the class of a given reference

if(a.getClass == classOf[Person]) {
……
}

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 42


Superclass Construction
ᗍ All the classes have a primary constructor and many auxiliary constructors, and all the auxiliary constructor should
either call a primary constructor or previous auxiliary constructor

ᗍ It means an auxiliary constructor can never invoke a superclass constructor directly

ᗍ 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

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 43


Abstract Classes
ᗍ Just like Java, you can use the abstract keyword for a class, which can’t be instantiated:

ᗍ Here we declared a method to generate id, but didn’t provide implementation


ᗍ Each concrete subclass of Emp should provide the implementation of id
ᗍ In a subclass, we don’t need to specify override while defining an abstract superclass method:

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 44


Introducing Traits in Scala

ᗍ 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

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 45


Questions

© 2015 BlueCamphor Technologies (P) Ltd. www.skillspeed.com Slide 46

You might also like