Thursday, January 22, 2015

Java

http://learnxinyminutes.com/docs/java/

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

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

overloading:
     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
     http://stackoverflow.com/questions/20313052/java-static-and-dynamic-binding-upcast-overloading-mixed-together

constructors:
     no return type
     not inherited (thats why super())
     void Class() {}, different from the constr.
     http://stackoverflow.com/questions/6801500/why-do-constructors-in-java-not-have-a-return-type
     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
     http://www.fredosaurus.com/notes-java/oop/constructors/constructor-super.html
     http://stackoverflow.com/questions/1197634/java-error-implicit-super-constructor-is-undefined-for-default-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:
     super()
     instance intialization block
     constructor code
     http://www.programcreek.com/2011/10/java-class-instance-initializers/
     order: compiler inserts super(); then inserts instance intialization block; before the constructor code

anon constructors = intialization block
       https://javafeel.wordpress.com/2012/05/03/anonymous-constructors/
       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

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


upcasting:
     allowed
     Class1 extends Class2;
     Class 2 x  = new Class1

downcasting:
     allowed if there's a possibilty to succeed at runtime
     http://stackoverflow.com/questions/380813/downcasting-in-java


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:
      member methods
      member variable
      member class

variable:
      class v
      instance v
      local v (inside a method)
      shadowing/hiding (no overriding)
      http://www.xyzws.com/Javafaq/what-is-variable-hiding-and-shadowing/15

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
     http://www.cis.upenn.edu/~matuszek/General/JavaSyntax/inner-classes.html

anonymous class:
     an object, but not a class
     non-static  
     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
     https://www.clear.rice.edu/comp310/JavaResources/anon_inner.html
     http://stackoverflow.com/questions/3251018/java-anonymous-inner-class-using-a-local-variable
     possible in the static context: static A a  = new A(), check the following
     http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class
   
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
      http://stackoverflow.com/questions/5107158/how-to-pass-parameters-to-anonymous-class

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
     http://stackoverflow.com/questions/2000137/overriding-private-methods-in-java


hiding:
    static/class methods
    class variables
    instance variables
 

overriding vs hiding:
     http://docs.oracle.com/javase/tutorial/java/IandI/override.html


super:
    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
     http://tomlee.co/2007/06/java-this-and-inneranonymous-classes/
     http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html


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

      http://www.programmerinterview.com/index.php/java-questions/java-inner-class-example/
      http://stackoverflow.com/questions/12690128/how-to-instantiate-non-static-inner-class-within-a-static-method

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


calling outerclass from innerclass:
     outer.this.method()
     http://stackoverflow.com/questions/2808501/calling-outer-class-function-from-inner-class
    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();
             }
    }

   http://tutorials.jenkov.com/java/nested-classes.html

inner class from outside: java puzzle 9
        http://www.javapuzzlers.com/java-puzzlers-sampler.pdf
        http://stackoverflow.com/questions/3383460/odd-situation-for-cannot-reference-this-before-supertype-constructor-has-been-c
        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:
     http://stackoverflow.com/questions/2000137/overriding-private-methods-in-java

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.
     
     http://beginnersbook.com/2013/04/runtime-compile-time-polymorphism/
   

Liskov substitution principle:
      Square is a rectangle
      http://stackoverflow.com/questions/56860/what-is-the-liskov-substitution-principle
      example on restriction: Mauricio Linhares' answer
      http://stackoverflow.com/questions/6851612/java-access-modifiers-and-overriding-methods


calling static methods from instance:
     allowed, but warnings
     you can make the instance null, yet static calls produce results
     http://stackoverflow.com/questions/610458/why-isnt-calling-a-static-method-by-way-of-an-instance-an-error-for-the-java-co

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

fields:
    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

interface
    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
    http://codeinventions.blogspot.com/2014/07/can-interface-extend-multiple.html

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
    http://stackoverflow.com/questions/197893/why-an-abstract-class-implementing-an-interface-can-miss-the-declaration-impleme
 
final:
    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
      http://geekexplains.blogspot.com/2008/06/field-hiding-in-java-fields-are-only.html


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


 

@annotations  
       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:

      http://www.javapractices.com/home/HomeAction.do

No comments: