Abstract Classes
In this page:
Abstract Classes
An abstract class cannot be instantiated directly and may declare abstract members (methods or fields with no implementation) that subclasses are required to provide. Abstract classes can still hold constructor parameters and concrete (fully-implemented) methods alongside abstract ones. A class can extend at most one abstract class, the same single-inheritance rule as regular classes.
Warning: Trying to write new Shape() on an abstract Shape class is a compile error.
Example: Abstract Classes
abstract class Shape {
def area(): Double
def describe(): Unit = {
println(s"Area is ${area()}")
}
}
class Square(side: Double) extends Shape {
def area(): Double = side * side
}
object Main extends App {
val sq = new Square(4)
sq.describe()
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: