© Toby Weston 2018
Toby WestonScala for Java Developershttps://doi.org/10.1007/978-1-4842-3108-1_11

11. Inheritance

Toby Weston
(1)
London, UK
 
In this chapter we’ll look at inheritance in Scala: how you create subclasses and override methods, the Scala equivalent of interfaces and abstract classes, and the mechanisms Scala offers for mixing in reusable behavior. We’ll finish by discussing how to pick between all the options.

Subtype Inheritance

Creating a subtype of another class is the same as in Java. You use the extends keyword and you can prevent subclassing with the final modifier on a class definition.
Let’s suppose we want to extend the basic Customer class from earlier and create a special subtype to represent a DiscountedCustomer. A shopping ...

Get Scala for Java Developers: A Practical Primer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.