diff --git a/core/lib/Drupal/Core/Template/TwigSandboxPolicy.php b/core/lib/Drupal/Core/Template/TwigSandboxPolicy.php
index 5423bde5976488f831aa14e0e48afe7bdb18265b..a7bb43fc69626282b78a0a33f5ce8f4c4271839e 100644
--- a/core/lib/Drupal/Core/Template/TwigSandboxPolicy.php
+++ b/core/lib/Drupal/Core/Template/TwigSandboxPolicy.php
@@ -81,8 +81,10 @@ public function checkPropertyAllowed($obj, $property) {}
    * {@inheritdoc}
    */
   public function checkMethodAllowed($obj, $method) {
-    if (isset($this->whitelisted_classes[get_class($obj)])) {
-      return TRUE;
+    foreach ($this->whitelisted_classes as $class => $key) {
+      if ($obj instanceof $class) {
+        return TRUE;
+      }
     }
 
     // Return quickly for an exact match of the method name.
diff --git a/core/tests/Drupal/Tests/Core/Template/TwigSandboxTest.php b/core/tests/Drupal/Tests/Core/Template/TwigSandboxTest.php
index 80eed7a1319c4fffa2749e80eef7d9dcadc7b1b0..fc35ec57848dc4886e566ae76b591b322ca754ea 100644
--- a/core/tests/Drupal/Tests/Core/Template/TwigSandboxTest.php
+++ b/core/tests/Drupal/Tests/Core/Template/TwigSandboxTest.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\Tests\Core\Template;
 
+use Drupal\Core\Template\Attribute;
 use Drupal\Core\Template\TwigSandboxPolicy;
 use Drupal\Core\Template\Loader\StringLoader;
 use Drupal\Tests\UnitTestCase;
@@ -64,6 +65,13 @@ public function getTwigEntityDangerousMethods() {
     ];
   }
 
+  /**
+   * Tests that white listed classes can be extended.
+   */
+  public function testExtendedClass() {
+    $this->twig->render('{{ attribute.addClass("kitten") }}', ['attribute' => new TestAttribute()]);
+  }
+
   /**
    * Tests that prefixed methods can be called from within Twig templates.
    *
@@ -133,3 +141,5 @@ public function testEntitySafeMethods() {
   }
 
 }
+
+class TestAttribute extends Attribute {}