3. SCALA : OOPS
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)
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









No comments:
Post a Comment