Scala | Abstract Type members
Last Updated :
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
// Scala program of abstract type member
// Declaring an abstract class
abstract 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.
Similar Reads
Abstract types vs Generics in Scala In this discussion, we will explore abstract types and generics in Scala, highlighting their differences. Abstract types are types that include abstract members, while generics are types that can take other types as parameters. Let's delve into the disparities between these two concepts. Scala Abstr
2 min read
Abstract Classes in Scala Abstraction is the process to hide the internal details and showing only the functionality. In Scala, abstraction is achieved by using an abstract class. The working of the Scala abstract class is similar to Java abstract class. In Scala, an abstract class is constructed using the abstract keyword.
5 min read
Data Types in Scala A data type is a categorization of data which tells the compiler that which type of value a variable has. For example, if a variable has an int data type, then it holds numeric value. In Scala, the data types are similar to Java in terms of length and storage. In Scala, data types are treated same o
3 min read
Scala AnyRef type The Scala kind hierarchy starts with Any, that's the supertype of all types. The direct subclasses of Any are AnyVal and AnyRef. Whereas AnyVal represents price kinds (which includes Int, Double, and so on.), AnyRef represents reference types. AnyRef serves because the fundamental type and root deta
3 min read
Type Casting in Scala A Type casting is basically a conversion from one type to another. In Dynamic Programming Languages like Scala, it often becomes necessary to cast from type to another.Type Casting in Scala is done using the asInstanceOf[] method. Applications of asInstanceof methodThis perspective is required in ma
4 min read