diff --git a/Questions.txt b/Questions.txt index 5fe81b2..d218ef0 100644 --- a/Questions.txt +++ b/Questions.txt @@ -501,3 +501,76 @@ D) at line n1 } E) at line n1 Client {} + +Q) +interface Int1 { + default void doStuff() { + System.out.println("Int1.doStuff()"); + } +} +class Cl1 implements Int1 { + void doStuff() { + System.out.println("Cl1.doStuff"); + } +} +What is the result of a call: +new Cl1().doStuff(); + +A) Compilation fails +B) RuntimeException +C) Int1.doStuff() +D) Cl1.doStuff() +E) Int1.doStuff() + Cl1.doStuff() + +Q) Given: +interface IntX { + default void doStuff() { + System.out.println("IntX.doStuff()"); + } +} +interface IntY { + default void doStuff() { + System.out.println("IntY.doStuff()"); + } +} +class ClQ implements IntX, IntY {} + +What is the result of a call: +new ClQ().doStuff(); + +A) Compilation fails +B) RuntimeException +C) IntX.doStuff() +D) IntY.doStuff() +E) C1QdoStuff() + +Q) Given: +class Parent { + Parent(int x) { y += x; out.print(", P-c: y is " + y); } + static { out.print(", P-si: x is " + Parent.x); } + static int x = 99; + int y = 100; + { y++; out.print(", P-i: y is " + y); } +} +class Child extends Parent { + int x = 200; + static int y = 300; + Child() { super(x); } + Child(int x) { this(); } + { out.print(", C-i x: is " + x); } + static { out.print(", C-si: y is " + y); } +} +class InitOrder3 { + public static void main(String[] args) { + out.print("Hello!"); + new Child(-1); + out.println(" Goodbye!"); + } +} +What is the result? +A) Compilation fails +B) Exception at runtime +C) Hello!, P-si: x is 0, C-si: y is 300, P-i: y is 101, P-c: y is 401, C-i x: is 200 Goodbye! +D) , P-si: x is 0, C-si: y is 300Hello!, P-i: y is 101, P-c: y is 401, C-i x: is 200 Goodbye! +E) Hello!, P-si: x is 99, C-si: y is 300, P-i: y is 101, P-c: y is 100Goodbye! diff --git a/src/main/java/defaultmethods/Examples.java b/src/main/java/defaultmethods/Examples.java new file mode 100644 index 0000000..db011fe --- /dev/null +++ b/src/main/java/defaultmethods/Examples.java @@ -0,0 +1,27 @@ +package defaultmethods; + +interface IntX { +// void doStuff(); + default void doStuff() { + System.out.println("IntX.doStuff()"); + } +} +interface IntY { +// default void doStuff() { +// System.out.println("IntY.doStuff()"); +// } + void doStuff(); +} +class ClQ implements IntX, IntY { + @Override + public void doStuff() { + IntX.super.doStuff(); +// IntY.super.doStuff(); + } +} + +public class Examples { + public static void main(String[] args) { + new ClQ().doStuff(); + } +} diff --git a/src/main/java/recordstuff/Example.java b/src/main/java/recordstuff/Example.java index 91584de..2cc6e15 100644 --- a/src/main/java/recordstuff/Example.java +++ b/src/main/java/recordstuff/Example.java @@ -6,6 +6,8 @@ // has the type of the component (if varags then array) // components create final fields with the same name // components cannot have the name of an Object method (e.g. wait, toString :) +// all records are subtypes of java.lang.Record, but MUST NOT use extends +// and are implicitly final record Client(String name, int creditLimit) { // these two are "components" // line n1 // not permitted to add any instance fields @@ -19,6 +21,44 @@ public String toString() { // return super.toString(); // NOPE, there is no super... return "I'm a client with credit " + creditLimit; } + + // explicit canonical constructor, must not be less accessible + // than the class (default canonical constructor has same access as class + // MUST initialize the fields from the components + // argument names MUST match the components!!! +// public Client(String name, int credit) { // NOPE + Client(String name, int creditLimit) { +// super(); // NOPE! This must be left to the system + // most likely reason to create explicit constructor would be + // validation: + if (name == null || name.length() == 0) { + throw new IllegalArgumentException("bad name"); + } + + this.name = name; + this.creditLimit = creditLimit; +// this.creditLimit = credit; // this can't happen + } + + // because hand-coded constructor most likely just does validation, + // we can define "compact constructor" +// Client { // must not have both this compact AND explicit canonical +// if (name == null || name.length() == 0) { +// throw new IllegalArgumentException("bad name"); +// } +// name = "Mr/Mrs/Ms/Mx " + name; // "Normalization" +// // compact constructor MUST NOT mention the fields +// // this code "runs into" the default canonical constructor :) +// } + + // are allowed overloaded constructors + // which are allowed to be less accessible? + // all routes through construction MUST end up at the + // canonical constructor (whichever form it took) + // that's where the final fields are initialized + private Client(String name) { + this(name, 1000); + } } public class Example {