Java Koans – Beginner – Part 2

These are some notes from the Java Koans that is part of the assignment from the Advance Java Programming Class from PSU CS410J.

AboutInheritance

  • overridden methods may returns subtype
class Parent {
  public String doStuff() { return "parent"; }
}
class Child extends Parent {
  // override Parent.doStuff()
  public String doStuff() { return "child"; }   
  // overload Child.doStuff()
  public String doStuff(String s) { return s; } 
}

abstract class ParentTwo {
  abstract public Collection<?> doStuff();
}

class ChildTwo extends ParentTwo {  
  // must implement doStuff()
  public Collection<?> doStuff() { return Collections.emptyList(); };
}

AboutCasting

  • Downcast and Upcasting explain in here.
  • In term of the code in koans, Child extends from Parent extends from Grandparent mean child is a Child, Parent and Grandparent. However, parentReference is still a Child when upcast from Child to Parent because Child property is only hidden until being it is being downcast back to Child again.
Child child = new Child();
// upcasting always allow
Parent parentReference = child;
assertEquals(child instanceof Child, true);
assertEquals(parentReference instanceof Child, true);
assertEquals(parentReference instanceof Parent, true);
assertEquals(parentReference instanceof Grandparent, true);
  • Here, since child was originally a Child, we can downcast it back to Child with explicit downcast.
// implicit upcast
Grandparent child = new Child(); 
// explicit downcasts
Parent parentReference = (Parent) child;  
Child childReference = (Child) parentReference; 
assertEquals(childReference instanceof Child, true);
assertEquals(childReference instanceof Parent, true);
assertEquals(childReference instanceof Grandparent, true);

Child child2 = new Child()
// implicit upcast
Parent parentReference2 = chlid2;
// access Child.complain() instead of Parent.complain()
assertEquals(parentReference2.complain(), "Are we..");

// implicit upcast
Grandparent parent = new Parent();
// access Parent.complain() by explicit downcast the object back to Parent before invoking complain()
assertEquals("TPS...", ((Parent) parent).complain());

AboutConditionals

  • For nested if-else statement, else is bound to the closest if when there is no curly bracket.

AboutConstructors

  • All inherited constructors are first called before its own constructor. Example from another source as well.
class A {
  String someString = "a";
   public A() { someString += "x"; }
}
 
class B extends A {
  public B() { someString += "g"; };
}
 
@Koan
public void simpleConstructorOrder(){
  assertEquals(new B().someString, "axg");
}

AboutEnums

  • The private member of enum acts like the private member of inner class when access by the containing class. A better example.

AboutExceptions

  • finally is always ran! So return statement in finally will always be the return value.

AboutMethodPreference

  • The signature of the arguments will upcast to Object when they don’t fit. If comment out first line in class A, the first assert statement will equals to “Integer”. If comment out second line and uncomment the first line in class A, the second assert statement will equals to “int”.
class A {
  public String doStuff(int i) { return "int"; }
  public String doStuff(Integer i) { return "Integer"; }
  public String doStuff(Object i) { return "Object"; }
  public String doStuff(int...i) { return "int vararg"; }
  public String doStuff(Object...i) { return "obj vararg"; }
}

// assert statements
assertEquals(new A().doStuff(1), "int");
assertEquals(new A().doStuff(Integer.valueOf(1)), "Integer");
assertEquals(new A().doStuff(1l), "Object");
assertEquals(new A().doStuff(1, Double.valueOf(2)), "obj vararg");

AboutOperators

  • full AND: &
  • short circuit AND: &&
  • write (true == a) to ensure it is a predicate and not an assignment (a = true).

AboutArrays

  • use Arrays.equals() to compare element by element equality between two arrays.
Advertisements