1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @run main overrideSealedTest
  27  */
  28 
  29 // Test that a method in a sealed class or interface can be overridden.
  30 public class overrideSealedTest {
  31 
  32     sealed class Rectangle permits Square {
  33         public String draw() { return "rectangle"; }
  34     }
  35 
  36     class Square extends Rectangle {
  37         public String draw() { return "square"; }
  38     }
  39 
  40     Rectangle r = new Rectangle();
  41     Rectangle rs = new Square();
  42     Square s = new Square();
  43 
  44 
  45     public sealed interface Shape permits Circle {
  46         default String name() { return "shape"; }
  47     }
  48 
  49     class Circle implements Shape {
  50         public String name() { return "circle"; }
  51     }
  52 
  53     Shape sc = new Circle();
  54     Circle c = new Circle();
  55 
  56 
  57     public static void main(String... args) {
  58         overrideSealedTest ost = new overrideSealedTest();
  59         if (ost.r.draw() != "rectangle")
  60             throw new RuntimeException("Bad value returned by draw(): " + ost.r.draw());
  61         if (ost.rs.draw() != "square")
  62             throw new RuntimeException("Bad value returned by draw(): " + ost.rs.draw());
  63         if (ost.s.draw() != "square")
  64             throw new RuntimeException("Bad value returned by draw(): " + ost.s.draw());
  65 
  66         if (ost.sc.name() != "circle")
  67             throw new RuntimeException("Bad value returned by name(): " + ost.sc.name());
  68         if (ost.c.name() != "circle")
  69             throw new RuntimeException("Bad value returned by name(): " + ost.c.name());
  70     }
  71 }