Saturday, 18 May 2019

Data Abstraction in Scala

Abstract Classes
Abstraction is the process to hide the internal details and showing only the functionality. In Scala, abstraction is achieved by using an abstract class.
In Scala, an abstract class is constructed using the abstract keyword. It contains both abstract and non-abstract methods and cannot support multiple inheritances. A class can extend only one abstract class.
Syntax:
abstract class class_name
{
// code..
}
The abstract methods of abstract class are those methods which do not contain any implementation. Or in other words, the method which does not contain body is known as an abstract method.
Example:
// Abstract class
abstract class mylife
{
    // abstract method
    def details()
}
// UVPM class extends abstract class
class UVPM extends mylife
{
    def details()
    {
        println("Life: UVPM")
        println("Topic name: Abstract class in Scala")
    }
}
object Main 
{
    // Main method
    def main(args: Array[String]) 
    {
        // objects of UVPM class
        var obj = new UVPM()
        obj.details()
    }
}

Like Java, in Scala, we are not allowed to create the instance of the abstract class.
In Scala, an abstract class can also contain fields. These fields are accessed by the abstract class methods and by the methods of the class which inherit abstract class(like inheritance).
Like Java, In Scala, an abstract class can also contain a constructor and a constructor of an abstract class is called when an instance of a inherited class is created. As shown in the below program.
// Abstract class with constructor and the constructor conatin two arguments
abstract class mylife(name: String, topic: String)
{
    def details()
}
// UVPM class extends abstract class
class UVPM(name: String, topic: String) extends mylife(name, topic)
{
    def details()
    {
        println("Life : " + name)
        println("Topic name: " + topic)
    }
}
object Main 
{ 
    // Main method
    def main(args: Array[String])
    {
        // objects of UVPM class
        var obj = new UVPM("UVPM", "Life is a class")
        obj.details()
    }
}

An abstract class can also contain only non- abstract method. This allows us to create classes that cannot be instantiated, but can only be inherited. As shown in the below program.
// Abstract class with non-abstract method
abstract class mylife
{
    // Non-abstract method
    def details()
    {
        println("Welcome to UVPM")
    }
}
// UVPM class extends abstract class
class UVPM extends mylife{}
object Main 
{
    // Main method
    def main(args: Array[String])
    { 
        // objects of UVPM class
        var obj = new UVPM()
        obj.details()
    }
}
//Output:
Welcome to UVPM

In Scala, an abstract class can contain final methods (methods that cannot be overridden).
// Abstract class with the final method
abstract class mylife
{
    final def mymethod()
    {
        println("Final method")
    }
}
// UVPM class extends abstract class
class UVPM extends mylife{}
object Main 
{
    // Main method
    def main(args: Array[String]) 
    {        
        // objects of UVPM class
        var obj = new UVPM()
        obj.mymethod()
    }
}

No comments:

Post a Comment

Use filter method to filter a Scala collection

Use filter method to filter a Scala collection To use filter on your collection, give it a predicate to filter the collection elements as...