Sunday, September 14, 2014

Inheritance, polymorphism, abstract class and interface (Notes of reading Head First Java)

Inheritance
  • subclass inherits from superclass by using keyword extends
    • Ex: public class subclass extends superclass {}
  • A subclass inherits all public instance variables and methods of superclass, but does not inherit the private instance variables and methods
  • A nonpublic class can only be inherited by classes from the same package
  • Inherited methods can be overridden. The method cannot be overridden if it’s marked with final modifier
  • To invoke the superclass version of a method from a subclass that’s overridden the method, use the super keyword
    • Ex: super.method()
Polymorphism
  • superclass obj = new subclass() //The left is called reference type. The right is called object type
  • With polymorphism, the reference type can be a superclass of the actual object type.
  • obj.method() // If method is overridden in subclass, then method of subclass is called though the reference type is superclass
  • Besides assignment, we can also have polymorphic arguments and return types.
  • Rules for overriding
    • Arguments must be the same types and return types must be compatible.
    • The method can’t be less accessible.
  • Overloading vs overriding
    • Overloading is having two methods with the same name but different argument lists
    • The return types can be different
    • You can’t change only the return type
    • You can vary the access levels in any direction
Abstract class
  • The main reason for abstract class is that some classes just should not be instantiated
    • Ex: abstract public class superClass{}
  • The abstract method means the method must be overridden.
    • Ex: public abstract void method(); // There is no body for abstract method
  • Abstract class can contain both abstract and non-abstract methods
  • The first concrete class in the inheritance tree must implement all abstract methods
  • You cannot make a new instance of an abstract type, but you can make an array object declared to hold that type
  • Ex:
    • superClass obj = new superClass() // illegal
    • superClass[] objs = new superClass[5] // legal, each element inside objs can be either superClass or subClass
  • You can call a method on an object reference only if the class of the reference type actually has the method
  • Ex: 
    • public class subClass extends superClass{}
    • There is a method METHOD which is only defined in subClass
    • superClass obj = new subClass()
    • obj.METHOD() // illegal, cannot pass compiler
    • The way to workaround is:
      • obj_cast = (subClass) obj
      • obj_cast.METHOD() //legal
    • The compiler decides whether you can call a method based on the reference type, not the actual object type. The method you are calling on a reference must be in the class of that reference type. Doesn’t matter what the actual object is.
  • If a class contains abstract methods, the class must be declared as abstract
  • Every class in Java extends class Object
Interface
  • A Java interface is like a 100% pure abstract class
  • To define an interface
    • public interface intClass{}
  • To implement an interface
    • public class subClass extends superClass implements intClass{}
  • Interface methods are implicitly public and abstract, so typing ‘public’ and ‘abstract’ is optional
  • Benefits of interface
    • If you use interfaces instead of concrete subclasses as arguments and return types, you can pass anything that implements that interface
    • A class can implement multiple interfaces
  • A class that implements an interface must implement all the methods of the interface, since all interface methods are implicitly public and abstract

Saturday, September 13, 2014

Organize your code with package and jar (Notes of reading Head First Java)

I'm reading 'Head First Java' recently and here are some notes about package and jar.

Jar usage
  • Use jar file to compact your class files
    • /projects/class contains all classes files
    • /projects/source contains all java files
    • create manifest.txt under class
    • content of manifest.txt: 
    • Main-Class: <main_class> // don’t put .class on the end
    • Press the return key after typing the Main-Class line
  • Create jar file
    • cd /projects/class
    • jar -cvmf manifest.txt <file_name>.jar *.class
  • Execute jar file
    • java -jar <file_name>.jar
Organize your code in packages
  • Organize source code
    • /projects/source/com/shunrang/ contains all java files
    • Add this as the first line of all java files: package com.shunrang
  • Compile source code
    • mkdir /projects/class
    • cd /projects/souce
    • javac -d ../class com/shunrang/*.java
  • Run your code
    • cd /projects/class
    • java com.shunrang.<main_class>
    • The -d flag in javac will create the same structure under class folder as what is inside source folder
Combine Jar and package
  • Create manifest.txt file
    • Put manifest.txt under /projects/class
    • Content of manifest.txt: Main-Class: com.shunrang.<main_class>
  • Create Jar file
    • cd /projects/class
    • jar -cvmf manifest.txt <file_name>.jar com //All you specify is the com directory
  • Run Jar file
    • java -jar <file_name>.jar
  • Useful jar command
    • jar -tf <file_name>.jar //List the contents of a JAR, -tf stands for ‘Table File’
    • jar -xf <file_name>.jar //Extract the contents of a JAR, -xf stands for ‘Extract File'