Beruflich Dokumente
Kultur Dokumente
(Shamelessly ripped from Head First Java, Daves Slides, and some kid who I
mugged for his Java notessorry Vikas, that was me.)
I.
II.
Constructors
a. Code that runs when you say new to a class type an object of that type is
created using the class as a blueprint.
b. Constructors initialize the state of an object
c. In you dont specify a constructor, the compiler will put one in for you
d. If you put a constructor in your class, a default constructor will not be
provided by the compiler.
This could be a problem, because Java calls the constructors of the
superclasses, going up the inheritance hierarchy. If there is no default
constructor, the constructor chain is broken:
class Person {
String name;
Person (String name) { this.name = name; }
}
class Student extends Person {
private boolean suckUpToTA;
Student( ) {
// here Java tries to call new Person() but cannot find it;
suckUpToTA = true;
}
}
How do we fix it?
class Student extends Person {
private boolean suckUpToTA;
Student( ) {
super(name); // this has to be the first thing in constructor!
suckUpToTA = true;
}
}
iii. When overriding methods from Object, follow the rules. You
HAVE to pass in a parameter of type Object, you HAVE to keep it
public if it was before. You HAVE to return a compatible object.
iv. An object contains everything it inherits from each of its
superclasses:
Interfaces
i. A 100% abstract class.
ii. Allows for multiple inheritance by allowing a class to extend one
superclass but implement multiple interfaces.
iii. A class that implements an interface must implement all the
methods of the interface, since all interface methods are implicitly
public and abstract. You can explicitly write that a method is
public and abstract but you dont have to.
j. IS-A vs. HAS-A: IS-A relationships are examples where inheritance
should be used, HAS-A relationships are examples where composition
should be used.
i. A triangle IS-A shape, a cat IS-A feline, and a surgeon IS-A doctor
Catch is where you try to recover from something bad that happened.
A method throws exceptions when something fails at runtime.
An exception is an Object!
This is how you throw an exception:
throw new Exception();
h. The finally block is where you do something that must happen whether
you threw an exception or not. An example of this is closing a file stream.
i. Methods can throw more than one exception, and they can catch more
than one type of exception, and exceptions are polymorphic:
try{
//risky code involving reptiles
}catch{SnakeBiteException){
//code for antidote
}catch(ReptileException){
//code for bandaid
}
Now if SnakeBiteException is a child class of ReptileException, we must try to
catch it first some compilers/IDEs check this for you. Otherwise, catching a
ReptileException first means the specific SnakeBiteException catch is
unreachable.AND WELL NEVER GET THE ANTIDOTE! AHHHHH!!!!!
j. If you want to call a method that throws an exception, but you dont want
to handle it in the calling method, you duck the exception:
Heres my method that throws an exception:
void riskyMethod throws SajException(){
if(noSaj){
//notice that I can pass a string parameter!
throw new SajException(Its not a party without Saj!);
}//if
}//risky method
Heres my method that uses riskyMethod without a try catch:
public void haveParty() throws SajException{
//notice lack of try catch
riskyMethod();
}
YOU MUST EITHER HANDLE AN EXCEPTION, OR DUCK IT