Suppose that I have:
trait A
class B extends A
compiled into class files. Later I load those using reflection:
val a = Class forName "A"
val b = Class forName "B"
Could anyone tell me how to check whether b is the subtype of a?
Use the isAssignableFrom method in Class:
a isAssignableFrom b
This returns true if b is a subclass/subinterface of a or b == a.
You can just call the getInterfaces method on b and iterate through the array to see if any of them equals a.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With