Scala | Abstract Type members
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 :
abstract class vehicle (name : String)
{
val category : String
def car _ type{ println(category) }
override def toString = s " The vehicle type is $category"
}
class car (name : String) extends vehicle (name)
{
val category = "car"
}
class bike (name : String) extends vehicle (name)
{
val category = "bike"
}
class bus (name : String) extends vehicle (name)
{
val category = "bus"
}
class truck (name : String) extends vehicle (name)
{
val category = "truck"
}
object AbstractFieldsDemo extends App
{
val car = new car( "Honda" )
val bike = new bike( "Yamaha" )
val bus = new bus( "Tata" )
val truck = new truck( "Ashok_Leyland" )
car.car _ type
bus.car _ type
truck.car _ type
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.
Last Updated :
16 May, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...