Advertisement
Parasect

Untitled

Feb 23rd, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. Java - Summary
  2.  
  3. ============================================
  4.                 Coding Style
  5. ============================================
  6. -All variables should have meaningful names.
  7. -Constants must be declared as public static final.
  8. -Use collections instead of regular arrays.
  9.  
  10. ============================================
  11.                 Polymorphism
  12. ============================================
  13. public class Animal{
  14.     public int myInt = 1;
  15.     public String speak(){ return "ha?"; }
  16.     public void eat(int calories){System.out.print("Yammy");}
  17. }
  18. public class Dog extends Animal {
  19.     public int myInt = 2;
  20.     public String speak(){return "woof";}
  21. }
  22. public class Cow extends Animal {
  23.     public String speak(){return "moo";}
  24.     public void getMilk(){....}
  25.     public void eat(int calories){System.out.print("Yammy Yammy");}
  26. }
  27. Cow myCow = new Cow();
  28. Dog myDog = new Dog();
  29. Animal myAnimal = myCow;
  30. myAnimal.speak(); //moo
  31. myAnimal.getMilk(); //error. Animals don't give milk.
  32. myAnimal.eat(1212); //Yammy Yammy
  33.  
  34. -Shadowing (NOT to use): Animal a = new Dog();
  35.                         System.out.print(a.myInt); // 1
  36.  
  37. -"Program to interface, not to implementation" - try to define variables so that their type is highest in hierarchy.
  38. i.e. Animal a = new Dog(); and NOT: Dog a = new Dog();
  39.  
  40. Casting:
  41. Animal a = new Cow(); //implicit up casting
  42. Animal animal = new Cow;
  43. Cow c = (Cow) animal; //legal down casting
  44.  
  45. AVOID DOWN CASTING AND USE OF instanceof
  46.  
  47. ============================================
  48.       Abstract classes and Interfaces
  49. ============================================
  50. -Abstract classes:
  51. public Abstract class Animal{
  52.     protected abstract void speak();
  53.     public void eat(){...}
  54.     public static abstract ... //compilation error
  55. }
  56.  
  57. public class Dog extends Animal {
  58.     public void speak(){...}
  59. }
  60. public class Cat extends Animal {
  61. } //compilation error. speak() not implemented.
  62. -----------------------------------------------
  63. -Interfaces:
  64. public interface Printable{
  65.     public void print();
  66. }
  67. public class Doc implements Printable{
  68.     public void print(){...}
  69. }
  70.  
  71. public static void main(String args[]){
  72.     Document d = new Document();
  73.     d.print();
  74.     Printable p = new Printable(); //Compilation error
  75. }
  76.  
  77. -Interfaces can extend interfaces.
  78.  
  79. ============================================
  80.               Design Patterns
  81. ============================================
  82. http://www.tutorialspoint.com/design_pattern/
  83. Useful:
  84. -Delegation
  85. -Factory
  86. -Singleton
  87. -Facade
  88. -Decorator
  89. Behavioral patterns
  90. -Iterator
  91. -Strategy
  92.  
  93. ============================================
  94.                  Collections
  95. ============================================
  96. ArrayList - https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
  97. LinkedList - https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html
  98. useful commands:
  99. sort(), isEmpty(), indexOf(), get(int index), size()
  100.  
  101. ============================================
  102.                  Exceptions
  103. ============================================
  104. Exception - a class that implements the Throwable interface.
  105.  
  106. Syntax:
  107. try
  108. {
  109. Statement A
  110. }
  111. catch(Exception e) //catch exception thrown by Statement A
  112. {
  113. Statement B
  114. }
  115. ----
  116. throw NullPointerException; //manually throw exception
  117. ----
  118. Checked exceptions - extend the Exception class. Compiler knows about this type of exceptions and will give warnings if something is wrong.
  119. Unchecked exceptions - extend the RuntimeException class.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement