Scala | Abstract Type members

16 May, 2019
A member of a class or trait is said to be abstract if that particular member does not have a complete definition in the class. These Abstract members are always implemented in any sub-classes of the class under which it is defined. These kinds of declaration are allowed in many programming languages and is one of the key feature of object oriented programming languages. Scala also allows to declare such methods as shown in the example below:
abstract class Sample{
  def contents: Array[String]
  def width: Int = a
  def height: Int = b
}

Thus in the above class Sample we have declared three methods: contents, width and height. The implementation of the last two methods is already defined whereas in the first method, contents, does not have any kind of implementation mentioned. This method is thus an abstract member of the class Sample. It is important to note that a class with abstract members must itself be declared abstract. This abstract keyword in front of the class denotes that the class will for sure have a abstract member with no implementation.Another example of how to write the abstract members within a class:
abstract class Example{
   type T
   def transform(x: T): T
   val initial: T
   var current: T
}

In the above example the abstract class is declared which defines an abstract type T, an abstract method transform, an abstract value initial and an abstract value current.
Below is the example of Abstract type member:
Example :
// Scala program of abstract type member   // Declaring an abstract classabstract class vehicle (name:String) {        // This is an abstract member     // with undefined implementation     val category: String               // A function that is used to print     // the value of the abstract member    def car_type{ println(category) }      override def toString = s" The vehicle type is $category"}  // Now extend the classes bike, car, // truck and bus and provide values // for the variable type  class car (name:String) extends vehicle (name){    // assigning the value of     // the abstract member as car    val category = "car"             }class bike (name:String) extends vehicle (name){    // assigning the value of     // the abstract member as bike    val category = "bike"         }class bus (name:String) extends vehicle (name){    // assigning the value of     // the abstract member as bus    val category = "bus"             }class truck (name:String) extends vehicle (name){    // assigning the value of     // the abstract member as truck    val category = "truck"         }  object AbstractFieldsDemo extends App{    // assigning the name as Honda in the abstract    // class where the category value is car    val car = new car("Honda")           // assigning the name as Yamaha in the abstract     // class where the category value is bike    val bike = new bike("Yamaha")           // assigning the name as Tata in the abstract     // class where the category value is bus    val bus = new bus("Tata")               // assigning the name as Ashok_Leyland in the    // abstract class where the category value is truck    val truck = new truck("Ashok_Leyland")            // implementing the function      // car_type for the object car    car.car_type          // implementing the function     // car_type for the object bus    bus.car_type          // implementing the function     // car_type for the object truck    truck.car_type               // implementing the function     // car_type for the object bus    bike.car_type         println(car)    println(bus)    println(truck)    println(bike)}
Output
car
bus
truck
bike
The vehicle type is car
The vehicle type is bus
The vehicle type is truck
The vehicle type is bike

In the above example the trait vehicle has a abstract val category along with a simple concrete method named car_type and an override of the toString method. And then the classes car, bike, Truck and Bus extend the class vehicle and provide values for the field category.We see in the above code how the undefined implementation of the abstract member is being used to assign values and change the assigned values for every object of different kind. Like in this example we stored multiple value of category for different kinds of vehicle types.
Thus, for a conclusion the abstract data members are those which have an unknown implementation.
Picked
Scala
Scala-OOPS
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.