Scala is, an object oriented language. This means that you can define classes in Scala, and instantiate objects of these classes.We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object. Since, many houses can be made from the same description, we can create many objects from a class.
In scala ,a class declaration contains the class keyword, followed by an identifier (name) of the class. In general, class declarations can include these components, in order:
How to define a Class
class MyClass
{
}
- A class is a blueprint for objects.
- Once you define a class, you can create objects from the class with the keyword new.
- A class definition contains field declarations and method definitions from which fields are used to store the state of an object and methods provides the access to fields and alter the state of an object etc.
- Fields
- Constructors
- Methods
- Superclasses (inheritance)
- Interfaces implemented by the class etc
In scala ,a class declaration contains the class keyword, followed by an identifier (name) of the class. In general, class declarations can include these components, in order:
- Keyword class: A class keyword is used to declare the type class.
- Class name: The name should begin with a initial letter (capitalized by convention).
- Superclass(if any):The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
- Traits(if any): A comma-separated list of traits implemented by the class, if any, preceded by the keyword extends. A class can implement more than one trait.
- Body: The class body is surrounded by { } (curly braces).
How to define a Class
class MyClass
{
}
A field is a variable that is accessible inside the whole object. This is contrast to local variables, which are only accessible inside the method in which they are declared. Here is a simple field declaration:
class MyClass
{
var myField : Int = 0;
}
This declaration defines a field of type Int and initializes it to the value 0.Scala is type inference can figure out the type of a variable, based on the value assigned to it. Therefore, you could actually omit the type in the field declaration above, like this:
class MyClass
{
var myField = 0;
}
Since 0 is by default assumed to be an Int, the Scala compiler can infer the type of the myField based on the 0 assigned to it.
Objects
It is a basic unit of Object Oriented Programming and represents the real-life entities. A typical Scala program creates many objects, which as you know, interact by invoking methods. An object consists of :
The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor.In Scala, an object of a class is created using the new keyword. The syntax of creating object in Scala is:
var obj = new Dog();
Example
eg.
class cmp
{
def comp (a:Int, b:Int)
{
if (a > b)
println(s"the value $a is greate" )
else
println(s"the value $b is greater" )
}
}
object test {
def main(args: Array[String]){
var obj= new cmp();
var obj1= new cmp();
obj.comp(100,4)
obj.comp(2,3)
}
}
object test10 {
class dog (name:String,breed:String,age:Int,color:String) {
println("My name is:" + name + " my breed is:" + breed);
println("I am: " + age + " and my color is :" + color);
}
def main(args: Array[String]){
var obj=new dog("tuffy", "papillon", 5, "white");
}
}
class MyClass
{
var myField : Int = 0;
}
This declaration defines a field of type Int and initializes it to the value 0.Scala is type inference can figure out the type of a variable, based on the value assigned to it. Therefore, you could actually omit the type in the field declaration above, like this:
class MyClass
{
var myField = 0;
}
Since 0 is by default assumed to be an Int, the Scala compiler can infer the type of the myField based on the 0 assigned to it.
Objects
It is a basic unit of Object Oriented Programming and represents the real-life entities. A typical Scala program creates many objects, which as you know, interact by invoking methods. An object consists of :
- State: It is represented by attributes of an object. It also reflects the properties of an object.
- Behavior: It is represented by methods of an object. It also reflects the response of an object with other objects.
- Identity: It gives a unique name to an object and enables one object to interact with other objects.
The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor.In Scala, an object of a class is created using the new keyword. The syntax of creating object in Scala is:
var obj = new Dog();
Example
eg.
class cmp
{
def comp (a:Int, b:Int)
{
if (a > b)
println(s"the value $a is greate" )
else
println(s"the value $b is greater" )
}
}
object test {
def main(args: Array[String]){
var obj= new cmp();
var obj1= new cmp();
obj.comp(100,4)
obj.comp(2,3)
}
}
object test10 {
class dog (name:String,breed:String,age:Int,color:String) {
println("My name is:" + name + " my breed is:" + breed);
println("I am: " + age + " and my color is :" + color);
}
def main(args: Array[String]){
var obj=new dog("tuffy", "papillon", 5, "white");
}
}
No comments:
Post a Comment