Skip to content

Commit

Permalink
Day 2 Final question
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonHGR committed May 9, 2023
1 parent b54039d commit e44128c
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
73 changes: 73 additions & 0 deletions Questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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!
27 changes: 27 additions & 0 deletions src/main/java/defaultmethods/Examples.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
40 changes: 40 additions & 0 deletions src/main/java/recordstuff/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit e44128c

Please sign in to comment.