Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar + 1; //ok now Eg. void mymethod() { int foo; try { foo = getres(); catch (Exception e) { int bar = foo; //compile error since foo //may never have been initialized Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs. Eg. int i; someobj obj = new someobj(); mymethod(i, obj); void mymethod (int j, someobj o) {
Method Overloading There can be multiple methods defined in a class with the same name. The compiler picks the correct one based on the number and type of arguments passed to the method (also called parametric polymorphism). There is no programmer specified operator overloading in Java. (Except the + operator is overloaded for string concatenation). Constructors Used to initialize class instances (i.e. create objects). Constructors can be overloaded as shown in the following example. E.g. class car { String model; int doors; car (String m, int d) { //constructor 1 model = m; doors = d; E.g. class car { car (String m) { //constructor 2 this(m, 4); //calls the first //constructor final int def_doors = 4; car (String m) { //constructor this(m, def_doors); //compile error
Object Destruction Garbage collection removes objects, which no longer have references to them. It then reclaims the memory space. Subclassing and Inheritance A class inherits variables and methods from its superclass and uses them as if they're declared within the subclass itself. E.g. class animal { float wt; void eat() { class mammal extends animal { //subclass of animal int heartrate; //inherits wt from animal void breathe() { //inherits eat() class cat extends mammal { //subclass of mammal boolean longhair; //inherits heartrate and wt void purr() { //inherits eat() and breathe() A subclass inherits all members of its superclass NOT designated as private. Instances (objects) of a subclass can be used anywhere instances (objects) of the superclass are allowed. E.g. cat simon = new cat(); //simon refs an object of subclass cat animal creature = simon; //object ref simon can be assigned to //object ref creature because cat is a subtype of animal
Shadowed Variables An instance var in a subclass can shadow (hide) an instance var of the same name in the parent class. e.g. class IntCalc { int sum; class DecimalCalc extends IntCalc { //methods defined in this class see the //integer var sum double sum; //methods defined in this subclass see // the float var sum However, both vars exist for a given instance (object) of DecimalCalc, and they can have independent values. Methods that DecimalCalc inherits from IntCalc see the integer var sum. To be able to reference the int var inherited from IntCalc from within a method in DecimalCalc, we need to use the super keyword as in: int s = super.sum; Overriding Methods vs. Overloaded Methods Overloaded methods are methods with the same name but a different number or different type of arguments within a class. A subclass can also define a method that has exactly the same signature (i.e. same arguments and return type) as a method in its superclass. The method in the subclass is said to override the method in the superclass (this is another form of polymorphism). Thus methods in the superclass are shadowed by methods in the subclass by using method overriding.
e.g. class animal eat() sleep() reproduce() cat simon class mammal extends animal eat() reproduce() reproduce() sleep() class cat extends mammal purr() sleep() purr() Overloaded methods are selected by compiler at compile-time. Overriden methods are selected by Java dynamically at runtime. You can explicitly declare methods that can't be overridden by using the final modifier; these methods are not subject to dynamic binding and so there is no performance penalty. A static method in a superclass can be overridden by another static method (only) in a subclass, as long as the original method was not declared final. Overridden methods must adhere to the throws clause of the parent method's signature. this refers to members of the current object and super refers to the members of the superclass. e.g. class animal { void eat (fooditem f){ class carnivore extends animal { void eat( fooditem f) { if (f instanceof plant) //complain else super.eat(f);
Casting Casts checked both at compile and run-time. Incompatible casts result in class CastException at run-time. Only casts between objects in the same inheritance hierarchy are legal. e.g. cat simon = new cat(); animal creature = simon; //Implicit cast simon = creature; //compile error, incompatible type simon = (cat) creature; //ok Object Construction e.g. class Person { String name1; Person(String name) { name1 = name; class Doctor extends Person { String spec; Doctor(String name, String specialty) { super(name); //calls constructor of superclass spec = specialty;