Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Java - Summary
- ============================================
- Coding Style
- ============================================
- -All variables should have meaningful names.
- -Constants must be declared as public static final.
- -Use collections instead of regular arrays.
- ============================================
- Polymorphism
- ============================================
- public class Animal{
- public int myInt = 1;
- public String speak(){ return "ha?"; }
- public void eat(int calories){System.out.print("Yammy");}
- }
- public class Dog extends Animal {
- public int myInt = 2;
- public String speak(){return "woof";}
- }
- public class Cow extends Animal {
- public String speak(){return "moo";}
- public void getMilk(){....}
- public void eat(int calories){System.out.print("Yammy Yammy");}
- }
- Cow myCow = new Cow();
- Dog myDog = new Dog();
- Animal myAnimal = myCow;
- myAnimal.speak(); //moo
- myAnimal.getMilk(); //error. Animals don't give milk.
- myAnimal.eat(1212); //Yammy Yammy
- -Shadowing (NOT to use): Animal a = new Dog();
- System.out.print(a.myInt); // 1
- -"Program to interface, not to implementation" - try to define variables so that their type is highest in hierarchy.
- i.e. Animal a = new Dog(); and NOT: Dog a = new Dog();
- Casting:
- Animal a = new Cow(); //implicit up casting
- Animal animal = new Cow;
- Cow c = (Cow) animal; //legal down casting
- AVOID DOWN CASTING AND USE OF instanceof
- ============================================
- Abstract classes and Interfaces
- ============================================
- -Abstract classes:
- public Abstract class Animal{
- protected abstract void speak();
- public void eat(){...}
- public static abstract ... //compilation error
- }
- public class Dog extends Animal {
- public void speak(){...}
- }
- public class Cat extends Animal {
- } //compilation error. speak() not implemented.
- -----------------------------------------------
- -Interfaces:
- public interface Printable{
- public void print();
- }
- public class Doc implements Printable{
- public void print(){...}
- }
- public static void main(String args[]){
- Document d = new Document();
- d.print();
- Printable p = new Printable(); //Compilation error
- }
- -Interfaces can extend interfaces.
- ============================================
- Design Patterns
- ============================================
- http://www.tutorialspoint.com/design_pattern/
- Useful:
- -Delegation
- -Factory
- -Singleton
- -Facade
- -Decorator
- Behavioral patterns
- -Iterator
- -Strategy
- ============================================
- Collections
- ============================================
- ArrayList - https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
- LinkedList - https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html
- useful commands:
- sort(), isEmpty(), indexOf(), get(int index), size()
- ============================================
- Exceptions
- ============================================
- Exception - a class that implements the Throwable interface.
- Syntax:
- try
- {
- Statement A
- }
- catch(Exception e) //catch exception thrown by Statement A
- {
- Statement B
- }
- ----
- throw NullPointerException; //manually throw exception
- ----
- Checked exceptions - extend the Exception class. Compiler knows about this type of exceptions and will give warnings if something is wrong.
- Unchecked exceptions - extend the RuntimeException class.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement