inheritance

What def, val, and var fields in Scala traits look like after they’re compiled (including the classes that extend them) alvin April 14, 2019 - 6:05pm
Table of Contents1 - def field in trait2 - val field in trait (abstract)3 - val field in trait (concrete)4 - var field in trait (abstract)5 - var field in trait (concrete)6 - An abstract class in the middle7 - A trait in the middle8 - Summary

I generally have a pretty good feel for how Scala traits work, and how they can be used for different needs. As one example, a few years ago I learned that it’s best to define abstract fields in traits using def. But there are still a few things I wonder about.

Today I had a few free moments and I decided to look at what happens under the covers when you use def, val, and var fields in traits, and then mix-in or extend those traits with classes. So I created some examples, compiled them with scalac -Xprint:all, and then decompiled them with JAD to see what everything looks like under the covers.

I was initially going to write a summary here, but if you want to know how things work under the hood, I think it helps to work through the examples, so for today I’ll leave that as an exercise for the reader.

A Java Factory Pattern example

Java Design Patterns FAQ: Can you provide an example of the Factory Pattern in Java?

Sure. In this article we'll look at a small-but-complete example of the Factory Pattern ("Factory Design Pattern") implemented in Java.

An article on the problems with OOP

There seems to be a lot of OOP-bashing lately, which I’m not a fan of, but this article titled Goodbye, OOP makes decent points about the problems with inheritance, encapsulation, and polymorphism. IMHO, OOP still makes sense in certain areas, including GUIs like Java Swing and JavaFX, so I’m not ready to throw it out completely or bash it.

How to dynamically add a Scala trait to an object instance

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 8.8, “How to dynamically add a Scala trait to an object instance.”

Problem

Rather than add a trait to an entire class, you just want to add a trait to an object instance when the object is created.

Solution

Add the trait to the object when you construct it. This is demonstrated in a simple example:

How to define a Scala trait so it can only be subclassed by a certain type alvin June 14, 2015 - 4:13pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 8.6, “How to mark a Scala trait so it can only be subclassed by a certain type.”

Problem

You want to mark your trait so it can only be used by types that extend a given base type.

Solution

To make sure a trait named MyTrait can only be mixed into a class that is a subclass of a type named BaseType, begin your trait with a this: BaseType => declaration, as shown here:

Scala: How to limit which classes can use a trait by inheritance alvin June 14, 2015 - 4:11pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 8.5, “How to limit which classes can use a trait by inheritance.”

Problem

You want to limit a trait so it can only be added to classes that extend a superclass or another trait.

Two Java inheritance tests / interview questions

Summary: Two Java inheritance tests that you might run into during a Java programming job interview.

This is a fun test to give to newbie Java developers. Just read the following code, and assuming that this code is compiled and runs, what do you think it will print?

Java ‘instanceof’ inheritance examples alvin September 3, 2009 - 6:42pm

In other tutorials I've written about the Java "instanceof null" behavior, and the Java "instanceof interface" behavior, and it occurred to me that I've never written anything about how the instanceof operator works with Java class inheritance.

Java instanceof interface example

Java instanceof FAQ: How does the Java instanceof operator work with a Java Interface?

The instanceof operator supports inheritance, which I can demonstrate through a simple "instanceof interface" example.