Thursday, January 22, 2015


primitive data types:
      autoboxing and unboxing
      cf: int vs Integer

      class variables (static keyword)
      instance variables ( ~static, no-static keyword)
      class methods (static)
      instance methods (~static)
      class initialization block
      instance initialization block

     both static and non-static can be overloaded.
     you can over load public static void main(String[] args)
     you can hide the main method in the subclass
     param types; order of params; types of params
     same return type:
               public int foo() {}
               public float foo {}
               foo(); // which foo
     most specific method overloaded

     no return type
     not inherited (thats why super())
     void Class() {}, different from the constr.
     compiler automatically inserts super()
     no constructor: compiler generates default constructor, which calls super().
     error: Implicit super constructor xyzClass() is undefined for default constructor. Must define an explicit constructor

constructor chaining:

private consructors: (workaround for static class ala c#)
    to avoid creating a new instance
    singleton instance

intialization block:
    instance: {   }
    class: static { }
    order: in the textual order
    class int blk runs when class is loaded to JVM
    instance int blk run when a new instance is created

constructors, intialization block:
     instance intialization block
     constructor code
     order: compiler inserts super(); then inserts instance intialization block; before the constructor code

anon constructors = intialization block
       the above link shows the importance of anonymous class
access modifiers: public, protected, default (isn't a keyword), private
     public > protected  > default > private
     default = package private

      = new
     Class1 x = new Class1
      same variables, lexical scope
      this.x = x
      instance.x = local x

     Class1 extends Class2;
     Class 2 x  = new Class1

     allowed if there's a possibilty to succeed at runtime

abstract, final:
     abstract: no new, but can be extended/subclassed
     final: new (can be instantiated), but can't be extended

interface (is an abstract class)
     consonants: static final
     abstract methods
     a way for multiple inheritance

calling instance method vs. class method:
     Class1 A = new Class1()
      A.method() //instance method
      Class1.Staticmethod //class method
      Class1.x //class var
      A.x //instance var

      member methods
      member variable
      member class

      class v
      instance v
      local v (inside a method)
      shadowing/hiding (no overriding)

nested class:
     static: nested static class
     non-static: inner class

nested static class:
      can't access outer class instance variables/methods.

inner class:
     member class: inner class
     method class: class within a method: local method
     anonymous class

anonymous class:
     an object, but not a class
     no name
     when only a single instance of the class is needed.
     new keyword
     class definition + object creation combined
     accesses static final fields outside of the scope
     one element array trick: MyType[] localVar = {initialValue}; 
  localVar[0] = newValue; check the following
     possible in the static context: static A a  = new A(), check the following
anon class creation:
     interface, class reference var, argument
     interface: an instance of the class that implements that interface
     class reference variable
     if class, anon class is an object of the subclass

anon class with parameter:
      no direct way
      possible with intializer

inheritance: extends implements multiple interaces.

overriding (complements hiding):
     just for non-static/instance methods
     can't override (can't hide)  a private method, but can use the same method in the derived class

    static/class methods
    class variables
    instance variables

overriding vs hiding:

    calling superclass method: super.method()
    subclass constructors: super() or super(arg1), etc

super vs this:
     this (anon class)
     outerclass.this (instance of outerclass)
    ClassName.this is the n-th lexically enclosing instance of this

instantiation of inner class:
      new innerClass() or
      Outer outer = new Outer();
      Outer.Inner = Inner();

instatiation of  nested static class:
      within the outer class:
       from the outside: Outer.Nested instance = new Outer.Nested()

calling outerclass from innerclass:
    static inner class like your fragment cannot access the outer class instance OuterClass.this

local class instatiation:
     only from the insider the method
     Class Outer {
               public void printText() {
                    class Local {

              Local local = new Local();

inner class from outside: java puzzle 9
        public class Outer {
              class Inner1 extends Outer {
                     Inner1() { super (); }
              class Inner2 extendes Inner1 {
                      Inner2() { Outer.this.super();}

extending outer class with a nested inner class:

What are the differences between method overloading and method overriding?
Overloaded MethodOverridden Method
ArgumentsMust changeMust not change
Return typeCan changeCan’t change except for covariant returns
ExceptionsCan changeCan reduce or eliminate. Must not throw new or broader checked exceptions
AccessCan changeMust not make more restrictive (can be less restrictive)
InvocationReference type determines which overloaded version is selected. Happens at compile time.Object type determines which method is selected. Happens at runtime.

Liskov substitution principle:
      Square is a rectangle
      example on restriction: Mauricio Linhares' answer

calling static methods from instance:
     allowed, but warnings
     you can make the instance null, yet static calls produce results

    static final
    static import (newer): java.lang.Math.PI

    you can hide
    you can access super class fields: super.x
    *you can access enclosing class fields: innerclass.super.x
abstract class:
    at least one method is abstract
    abstract a extends abstract b

    marker/tag interface = no fields, no methods: ex: Clonable
    only consonants + signature
    interface X extends interface1, interface2, interface3
    (illegal) interface X implements  interface1, interface2, interface3

interface vs abstract combo:
    interface can't extend  pure (100%) abstract
    interface can extend other interfaces (note: extends, not implements)
    abstract can extend interface
    abstract can implement interface
    static final constants
    final method: neither overriden nor hidden
    final variable can be hidden
    static final variable can be hidden (used is a better word) too (puzzle 72, java puzzlers)
static (belong to class, not an instance)
    no static methods in abstract class
    an instance method can call static method (but compiler warns)
    static methods can be hidden, but can't be overriden
    static methods can't be overriden by instance methods (compile error)
    static methods can't hide instance methods (compile error)
    Superclass d = new subclass(); //d.field prints field from Superclass
    static fields can be hidden with non-static and static fields with the same name

hiding fields:
     'public static String' type field can be hidden with 'protect int' field in the subclass
      narrowing visibility: public > protected > default > private
      static > non-static  
      types don't matter

    private members( methods or fields) can't be overriden or hidden
    same method/member with the same signature can be defined in the subclass.


       predefined vs custom
       type annotations (use, new in 1.8)
       format annon type
       predefined: @Deprecated, @Override, @SuppressWarnings
       @Override, not required, helps to prevent errors
       @SuppressWarnings({"unchecked", "deprecation")
       @SafeVarargs: does not perform unsafe operations on its varrags param
       meta-annotations: @Inherited (annotation), @Repeatable, @Target, @Documented, @Retention

common practices:

No comments: