fbpx
Select Page

About Scala

Scala is nowadays the most popular programming language which is functional and fully object-oriented that runs on the same java JVM. Scala was developed by Martin Odersky and the language has been around for about a decade.

For Free Demo classes Call: 8605110150

Registration Link: Click Here!

Following are some reasons for Scala popularity :

Easy to Code: Scala is a popular high-level language like Java, Python, C++, etc. Thus it becomes very easy to understand Scala code and syntax for all. For Java developers and programmers, this language is easier to learn as most of the syntax is same. All java packages are also available in scala so it can be easily integrated, imported and implemented. Scala is more useful, scalable and productive due to its flexibility.

Java Integration: Scala code can be compiled and executed from java also as it uses the same JVM for execution. Its framework, the compiler can use Java Libraries and packages, etc.

Application Development: Scala provides support for javascript code with a framework so web applications also can be compiled and developed using scala.

Companies are using: Popular companies like Apple, Twitter, Walmart, Google, Amazon, etc. moved their most of codes to Scala from other languages and are on upgrade phase due to scala’s efficiency and flexibility with backend operations.

Concurrent & Synchronize Processing: Scala allows us to write code or do programming patterns effectively for reducing line of code with type-safe way. It allows us to write codes in an effective immutable manner which helps us to apply parallelism. Spark development can be easily done via scala.

Other Framework support: The frameworks like Spark and Akka, Play, etc. are built upon language Scala. Scala is growing rapidly and the above frameworks are the most popular. Due to scala’s features, it’s widely used in Big data application for parallel programming and transformations. Scala mostly used for distributed applications based frameworks like Akka.

Easy Code writing: The code to write in scala is more readable and spot error-free. It is easy to write, compile, debug, and run the program and even easy to use in IDE’s like IntelliJ Idea, eclipse. The great feature of scala is functional programming which gives a wing to deal the same problem in a different angle. It also helps us to deploys concurrency which helps in parallelizing different tasks. Third-party libraries can be built for specific project tasks.

Image

The Object-Oriented Programming (OOP) Approach in Scala:


The Object-Oriented Programming concept was designed to overcome the drawback of the different programming methodologies. This new OOP approach brought a new revolution in programming technologies and languages. Object-oriented programming (OOP) allows us to write programs with classes and objects with importance given to data. So that state and behavior of these objects will be most similar to real-world entities. The primary purpose of the class is to hold data, information or state. The members of a class define the functions i.e. behavior of the class for the object. So that a class is called blueprint or template of the object. The object is also called as an instance of class or implementation of the class. The class is not visible i.e. It is logically represented and in other site object is real i.e. Physically present and have occurrence. The object is a real word entity.

For Free Demo classes Call: 8605110150

Registration Link: Click Here!

Let’s see the well-known features of OOP :

Abstraction: Data abstraction is the concept used to represent important features or functions of an object without showing background details or explanation.

Data Encapsulation: Encapsulation is also a concept to describes the idea of wrapping the data and the functionality that work on data within a class as a single unit. This is often used to hide the representation of an object internal information from the outworld.

Inheritance: Inheritance is the ability to reuse the properties and methods from one class to another class. The class on top is called parent class and the class which inherits the parent is called child class. They are also called as Base class and Derived class. So when we create derived a class, we do not need to write the same things like properties and behavior again. Inheritance allows us to reuse the code and reduce redundancy.

Polymorphism: It is the ability to process in more than one form i.e. Many forms. It allows us to do the same task in various different ways with different outcomes. It consists of two main concepts i.e. method overloading and method overriding also called static and dynamic polymorphism.

A package as the name given for a group of classes, traits and objects in scala. In scala we use packages to organize our classes, objects and traits. Object is also a class which is a single ton class in scala. As Scala do not provide static keyword so we have object term to specify pure singleton class where only one instance will be available. We have two types of packages in scala that is built-in packages and user defined packages which we create for our use. In the following examples demonstrated we have used some packages.

We use packages in Scala for following reasons:

Code Reusability: To write a project in scala, we many times feel that few things we writing again and over again repeatedly. Using packages, we can create box where all classes can be inserted to perform that same task. So that we can use it by importing that package and particular class.

Organization of application: In scala projects we can have hundreds of classes, traits and objects. It is a need of group them and put in to box for further use. The similar types of classes in a meaningful are in one package name and it can organize application based project better to quickly write and manage a code. Also we use packages to avoid naming confusion and same name class, package, trait can by put in different packages.

oops

For Free Demo classes Call: 8605110150

Registration Link: Click Here!

Scala installation and Modes:

On Ubunut/Linux:

Terminal > sudo apt-get update

> javac -version

> sudo wget www.scala-lang.org/files/archive/scala-2.11.0.deb

> sudo dpkg -i scala-2.11.0.deb

> scala

 

Testing: scala > print(“welcome!!”)

Windows: Install jdk8 and Set environment variable for java

Advance System Setting –> Environment Variables –> System Variable –> New

name: JAVA_HOME

Value: C:\Program Files\java\jdk1.8

Path     ;%JAVA_HOME%\bin

Testing Command Prompt: > javac -version

> java -version

Command Prompt: > scala -version

> scalac -version

We can execute scala program in two modes: Interactive mode and script mode.

scala > println(“Hi”)

scala > 2+2

scala > :quit

Script Mode:

//First Program of Scala through script mode

object Sample{

def main(args: Array[String]){

println(“Welcome to Scala!!”)

}

}

> scalac Sample.scala        –> Generate .class file which is byte code and will run

> scala Sample

Examples;

Following are some programs have demonstrated to work with oop program in scala. You will easily understands the concepts after implementing code. 

Program1:

package com.sevenmentor

class Calculator(val a:Double,val b:Double){

var x:Double =a

var y:Double =b

var result:Any=0

def add(){

result=a+b

}

def sub(){

result=a-b

}

def multi(){

result=a*b

}

def div(){

result=a/b

}

def per(){

result=(a/b)*100

result = result +”%”

}

 

def showResult(){

println(“Result is:”+result)

}

}

 

object Demo {

def main(args: Array[String]){

var c = new Calculator(50,100)

c.add()

c.showResult()

c.sub()

c.showResult()

c.multi()

c.showResult()

c.div()

c.showResult()

c.per()

c.showResult()

}

}

Program2:

package mypackage

class Employee{

var eid:Int=0;

 

def show():Unit = {

println(“I am an Employee”)

}

}

class Developer extends Employee{

var p = new Project()

def setEid(eid1:Int):Unit = {

eid = eid1;

}

def getEid(){

println(“EID:”+eid)

}

}

class Project{

var pname:String=”Not Assigned”

 

def setProject(pname1:String){

pname=pname1

}

 

def getProject(){

println(“Project Name:”+pname)

}

}

 

object Test {

 

def main(args: Array[String]){

var e = new Employee()

e.show()

var d = new Developer()

d.show()

d.setEid(101)

d.getEid()

 

d.p.setProject(“ECommereceHadoop”)

d.p.getProject()

 

var d1 = new Developer()

d1.p.getProject()

}

}

Program3:

abstract class Employee{

def eidValue:Unit

def show():Unit = println(“I am an Employee”)

}

 

class Developer(ename:String,eid:Int) extends Employee{

def eidValue():Unit = {

println(“Eid:”+eid)

}

override def show():Unit = println(“I am an Developer”)

}

object Demo{

def main(args:Array[String]){

val d = new Developer(“xyz”,101)

d.eidValue()

d.show()

}

}

 

Program4:

package mypackage

abstract class Animal{

def show():Unit = println(“I am a Animal”)

}

trait Pet{

def name:String

def showName:Unit

}

trait Rich{

def height:Int

def showHeight:Unit

}

trait Poor{

def weight:Int

def showWeight:Unit

}

class Dog extends Animal with Pet with Rich with Poor{

def name = “Rocky”

def height=3

def weight=5

def showName():Unit = println(“I am:”+name)

def showHeight():Unit = println(“I am:”+height+”Ft Size”)

def showWeight():Unit = println(“I am :”+weight+”Kg”)

override def show():Unit = println(“I am a Dog”)

}

 

class Cat extends Animal with Pet with Rich{

def name = “Micky”

def height=2

def showName():Unit = println(“I am a:”+name)

def showHeight():Unit = println(“I am a:”+height+”Ft Size”)

override def show():Unit = println(“I am a Cat”)

}

object Demo {

def main(args: Array[String]){

val d = new Dog()

d.show()

d.showName()

d.showHeight()

d.showWeight()

 

val c = new Cat()

c.show()

c.showName()

c.showHeight()

}

}

 

Program5: Statement:

Create Human abstract class with abstract show method and behave as a normal method

Create two traits Personality with setSkills method and Achievements with setAchievements method.

Create employee class implements and override all above methods.

 

package mypackage

 

abstract class Human{

def show():Unit

def behave():Unit = println(“Being Human”)

}

trait Personality{

def setSkills(skills:String):Unit

}

trait Achievements{

def setAchievements(achieve: String):Unit

}

 

class Employee extends Human with Personality with Achievements{

//reader suppose to fill these blocks with a statement

}

object Demo {

def main(args: Array[String]){

// reader suppose to fill these area

}

}

Call the Trainer and Book your free demo Class now!!!

call icon

© Copyright 2019 | Sevenmentor Pvt Ltd.

 

Author:

Sachin Patil

Pin It on Pinterest

× How can I help you?