Skip to content

Commit 4a274d7

Browse files
committed
Classes and Objects code seperated
Companion class Example added
1 parent 3378a7d commit 4a274d7

16 files changed

+174
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Objects/EmployeeDesign.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.time._
2+
3+
class Employee protected(val firstName:String, val lastName: String, val title:String, val hireDate:LocalDate)
4+
5+
object Employee
6+
{
7+
def createEmployee(firstName:String, lastName:String, title:String) = new Employee(firstName, lastName, title, LocalDate.now)
8+
9+
def createEmployee(firstName:String, lastName:String, title:String, hireDate:LocalDate) = new Employee(firstName, lastName, title, hireDate)
10+
}
11+
12+
object EmployeeDesignRunner extends App
13+
{
14+
val e1 = Employee.createEmployee("Bruce", "Wayne", "BatMan")
15+
println(e1.hireDate)
16+
}
17+
18+
/**
19+
Sample Output
20+
21+
> scalac EmployeeDesign.scala
22+
23+
> scala EmployeeDesignRunner
24+
2017-04-30
25+
**/

Objects/MainRunner.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object MainRunner
2+
{
3+
def main(args:Array[String]):Unit = println("Hello from Scala Main Method!!!")
4+
}
5+
6+
/**
7+
Sample Output
8+
> scala MainRunner.scala
9+
Hello from Scala Main Method!!!
10+
11+
//compiling scala file
12+
scalac MainRunner.scala
13+
14+
//Running scala class using java
15+
> java -cp .;C:\scala\lib\scala-library.jar MainRunner
16+
Hello from Scala Main Method!!!
17+
**/

Objects/MainRunnerApp.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
If object extends from App, then defining main method is not required.
3+
*/
4+
object MainRunnerApp extends App
5+
{
6+
println("Hello from Scala App!!!")
7+
}
8+
9+
/**
10+
Sample output
11+
12+
> scalac MainRunnerApp.scala
13+
14+
> scala MainRunnerApp
15+
Hellow from Scala App!!!
16+
**/

Objects/ObjectScript.scala

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//Objects are like utility classes in java. methods in objects can be considered as static methods in jav
2+
//all objects in scala are singleton.
3+
/***
4+
Objects in scala can be used for:
5+
1. Singleton
6+
2. Factory pattern
7+
3. Pattern matching login
8+
4. Utility method which does not require instance or state
9+
5. Default values
10+
6. Main method
11+
**/
12+
13+
object MyObject
14+
{
15+
def add(x:Int, y:Int)= x+y
16+
}
17+
18+
println("10 + 20 = " + MyObject.add(10,20))
19+
20+
class Employee(val firstName:String, val lastName:String, val title:String)
21+
22+
//any object can extend from a class
23+
object NewEmployee extends Employee("James", "Bond", "Spy")
24+
25+
println("FirstName:" + NewEmployee.firstName)
26+
println("LastName:" + NewEmployee.lastName)
27+
println("Title:" + NewEmployee.title)
28+
29+
30+
/**
31+
Sample Output
32+
33+
10 + 20 = 30
34+
FirstName:James
35+
LastName:Bond
36+
Title:Spy
37+
**/

Objects/SecretAgent.scala

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
1.Class SecretAgent and object SecretAgent are called as companion object.
3+
2.For companion classes, create a class and an object with same name.
4+
3.Both class and object must present in same file.
5+
4.Companion object can't have main method
6+
5.Companion object can have access to private members in Companion class
7+
6.Companion classes have access to private members in Companion object.
8+
*/
9+
10+
class SecretAgent(val name:String)
11+
{
12+
def shoot(n:Int)
13+
{
14+
import SecretAgent._ //This means every method inside SecretAgent companion object is imported
15+
decrementBullets(n)
16+
}
17+
}
18+
19+
object SecretAgent
20+
{
21+
private var bullets:Int = 3000
22+
23+
private def decrementBullets(count:Int)=
24+
{
25+
if((bullets - count) <= 0 ) bullets = 0
26+
else
27+
bullets = bullets - count
28+
}
29+
30+
def getBullets:Int = bullets
31+
}
32+
33+
object SecretAgentRunner extends App
34+
{
35+
val bond = new SecretAgent("James Bond")
36+
val ethan = new SecretAgent("Ethan Hunt")
37+
val jason = new SecretAgent("Jason Bourne")
38+
39+
bond.shoot(800)
40+
ethan.shoot(400)
41+
jason.shoot(300)
42+
43+
println("Bullets Left are :" + SecretAgent.getBullets)
44+
}
45+
46+
/**
47+
Sample Output
48+
> scalac SecretAgent.scala
49+
50+
> scala SecretAgentRunner
51+
Bullets Left are :1500
52+
53+
**/

Objects/SuperHeroes.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Person(val name:String, private val superHeroName:String)
2+
3+
object Person
4+
{
5+
def showSuperHeroName(p:Person) = p.superHeroName //Companion object can have access to private members in Companion class
6+
}
7+
8+
object SuperHeroRunner extends App
9+
{
10+
val p1 = new Person("Peter Parker", "Spiderman")
11+
println(Person.showSuperHeroName(p1))
12+
}
13+
14+
/**
15+
Sample output
16+
17+
> scala SuperHeroes.scala
18+
19+
> scalac SuperHeroes.scala
20+
21+
> scala SuperHeroes
22+
23+
> scala SuperHeroRunner
24+
Spiderman
25+
26+
**/

0 commit comments

Comments
 (0)