Monday, February 11, 2013

SCJP - Overriding vs overloading

Overriding Rules:
Access - cannot have a more restrictive access.
Argument list - must match exactly
Return type - the same or a subtype (this is the called covariant return, new in Java 5)
Exceptions - Can throw any unchecked (runtime) exceptions; cannot throw checked exceptions that are new or broader (must by the same, a subtype or none); can throw less or narrower.

private, final or static method cannot be overriden:
private methods are not visible in the derived class, so if the method has the same signature in the derived class, then it is a new method - no compilation failure.
final methods mean that they cannot be redefined, or a compiler error is generated.
static method cannot be overriden because they do not belong to the instance.
  • When a child class contains the same instance method as a parent class instance method (assuming all the rules of method overriding are followed), the child class method overrides the parent class method.
  • When a child class contains a static method that is the same as a static method in the parent, this child method hides the parent class method.
  • A child class cannot contain a nonstatic version of a static method in its parent class. Neither can a child class contain a static method with the same version of a nonstatic method in the parent. Either of these situations generates a compiler error.

In simpler terms, instance methods are overridden and static methods are hidden.
The overriden call is executed in the object type (runtime).

Overloading Rules:
Access - can change
Argument list - Must change
Return type - can change
Exception - can change

The overloaded call is executed in the reference type (compile time).




Information collected from:
SCJP: Sun Certified Programmer for Java Platform Study Guide: SE6 (Exam CX-310-065), Richard F. Raposa
SCJP Sun Certified Programmer for Java 6 Exam 310-065, Katherine Sierra and Bert Bates
A Programmer's Guide to Java SCJP Certification: A Comprehensive Primer (3rd Edition)

Static imports

A static variable can be imported into a source file, since Java 5.0, which allows the static variable to be accessible without being prefixed with its corresponding class or interface name.

syntax:
import static packagenames.classname.variablename;

E.g. of usage:

import static my.blueprints.House.counter;
import static java.lang.System.*;

class Test {
  public static void main(String [] args) {
    out.println(“counter = “ + counter);
  }
}

SCJP finalize question

Which statement is true?
A. A class's finalize() method CANNOT be invoked explicitly.
B. super.finalize() is called implicitly by any overriding finalize() method.
C. The finalize() method for a given object is called no more than once by the garbage collector.
D. The order in which finalize() is called on two objects is based on the order in which the two objects became finalizable.


TreeSet subset detail

When manipulating a subSet of a TreeSet, you need to know that the subSet is linked to the original set, which means that if changes are performed in the original set, they will also get reflected in the subSet, and vice-versa.

public static void main ( String args [ ] ) {
  TreeSet t = new TreeSet();
  t.add(1);
  t.add(2);
  t.add(4);
  TreeSet t2 = (TreeSet)t.subSet(2, true, 4, true);
  t.add(3);
  System.out.print(t + " " + t2);
}


This will print:
[1, 2, 3, 4] [2, 3, 4]

But if you do this:
public static void main ( String args [ ] ) {
  TreeSet t = new TreeSet();
  t.add(1);
  t.add(2);
  t.add(4);
  TreeSet t2 = (TreeSet)t.subSet(2, true, 4, true);
  t.add(5);
  System.out.print(t + " " + t2);
}


It will print:
[1, 2, 4, 5] [2, 4]

because the number 5 is out of range of the subset.

Sunday, February 10, 2013

SCJP question

What is the result of this code?


class Person{
  String name="No name";
  public Person(String nm){ name=nm;}
}

class Employee extends Person {
  String empID="0000";
  public Employee(String id) { empID=id;}
}

public class EmployeeTest{
  public static void main(String [] args){
    Employee e=new Employee("4321");
    System.out.println(e.empID);
  }
}


A. 4321
B. 0000
C. An exception is thrown at Runtime
D. Compilation fails

Thread States


FAQs concerning Threads:
Q: What happens if we create a thread, and then call twice the start() method?
A: An IllegalThreadStateException is thrown. This is a RuntimeException.

java.io Class Hierarchy Diagram









Diagram sources:
Gailer-net.de

java.io cheat sheet for SCJP:
(from book SCJP Sun Certified Programmer for Java 6 Exam 310-065)

Example for using these constructors:
How to read lines from a text file, display them and close all resources?

try{
  BufferedReader reader= new BufferedReader (new FileReader(new File("file.txt")));
  String line=null;
  while( (line=reader.readLine())!=null){
    System.out.println(line);
  }
  reader.close();
}catch(Exception e){}


For other package diagrams, check falkhausen.de