Sunday, December 15, 2019

3. SCALA : OOPS

3. SCALA : OOPS


SCALA as OOP###########################


Rule1 : name of the class and file name is not mandatory

MainClass.scala
------------------------------------------------------------------------------------------------------------------------------
class SecondClass{

var a = 0
var b = 100
def getA():Int=
{
   a

}

def setA(input:Int)=
{
   a = input

}

def getB():Int=
{
   b

}

def setB(input:Int)=
{
   b = input

}


object xyz extends App{
var newObj = new SecondClass
println(newObj.getA)
println(newObj.getB)
newObj.setA(1000)
newObj.setB(2000)

println(newObj.getA)
println(newObj.getB)
}

}

------------------------------------------------------------------------------------------------------------


Rule2: Scala is going to give getter and setter for private variables

class SecondClass{

var a = 0
var b = 100

Var x new SecondClass
println(x.a)    //internally it access x.getA()
x.a = 100  //internally it access x.setA()
}

Rule3: private var c = 10  //anyhow variable is private, but even getter and setter also becomes private

//if we define variable, 
 -> variable becomes private
 -> we will get getter & setter by default

//if we define private variable
 -> variable is anyhow private

 -> we will get private getter & setter (we have to define our own public getter and setter to access)


Constructors-------------

Class Abc { var a = 0; var b = 0; } main{ var a = new Abc() }

1. Primary constructor 2. Auxilary connstructor

Rules for Auxilary: ----------------------- - access using this - it calls Primary const inside it OR call already defined auxilary constructor








SingleTon Class#################################

object MySingleton { def concat(str1:String, str2:String):String={ str1 + str2 } main{ MySingleton.concat("Hello","Pavan') //valid only if MySingleTon is object } }










Companion objets#######################

Mix of Static and Class level variables


Inheitance####################





Trait########################

-> Interface...in java -> if we want to enforce the method -> only signature no body (abstract method) -> if we have method with body -> then its concrete method


Case class##################

when we define row of table












No comments:

Post a Comment