COM 3118

April 2, 2001

Constructors

Constructor initializes an object immediately upon creation.

Constructor-method names are same as name of the class.

Constructor method can never return values; accordingly, no return value is ever specified.

class MyClass {

private int var1;

pubic MyClass(){

var1 = 10;

}

pubic MyClass(int a){

var1 = a;

}

public static void main (String args[]){

MyClass m = new MyClass();

MyClass n = new MyClass(20);

}

}

The this Keyword

Allows methods to refer to the object that invoked it.

Can use this anywhere a reference to an object o the current class’ type is permitted.

Instance Variable Hiding

When a local variable has the same name as an instance variable, tha local variable hides the instance variable.

Box(double width, double height, double depth) {

this.width = width;

this.height = height;

this.depth = depth;

}

 

 

 

 

Garbage Collection

Objects are dynamically allocated using new operator.

What about destroying them when they are not required anymore.

Garbage collector runs periodically.

Checks for objects that are no longer referenced by any running state or indirectly through other referenced objects.

Garbage Collection Works when:

The Finalize( ) Method

You specify those actions that must be performed before an object is destroyed.

Called just prior to garbage collection.

The finalize( ) method has this general form:

Protected void finalize () {

// finalization code here

}

Assigning Object Reference Variable

Car car1 = new Car();

Car car2 = car1;

//….

car1 = null;

car1 has been set to null, but car2 still points to the original object.

Methods

Overloading Methods

Two or more methods in the same class that share the same name.

Parameter declarations are different.

One way java implements polymorphism.

Return types do not play a role in overload resolution.

Automatic type conversion can play a role in Overloading.

Class Overloaded {

int i = 88;

void method1() {

System.out.println("No Parameters");

}

void method1(double a) {

System.out.println("double type of parameters");

}

 

public static void main (String args[]) {

Overloaded ol = new Overloaded ();

ol.method1();

ol.method1(i);

}

}

Argument Passing

When a simple type is passed to a method, it is done by use of call-by-value. Objects are passed by use of call-by-reference.

Static

When a member is declared static, it can be accessed before any objects of it’s class are created, and without reference to any object.

main() method is declared as static because it must be called before any objects exist.

All instances of class share the same static variable.

Methods declared as static:

It is illegal to refer to any instance variable inside a static method.

Nested and Inner Classes

Scope of nested class is bounded by the scope of its enclosing class.

Types of Nested Classes:

A static nested class is one which has the static modifier applied. It must access members of its enclosing class through an object.

Cannot refer to them directly.

Inner class is non-static class.

Inner class is fully within scope of outer class.

Inner class has access to all the variables and methods of its enclosing class, can refer to them directly, but the reverse is not true. Member of the inner class are known only within the scope of the inner class and may not be used by the outer class.

The super Keyword

Using super to call superclass Constructors

Class A{

int var1;

A(int val1){

var1 = val1;

}

}

Class B extends A{

int var2;

B(int val1, int val2){

super(val1);

var2 = val2;

}

}

super() must always be the first statement in the subclass’ constructor.

When Constructors are called, they are called in order of derivation, from superclass to subclass.

Method overriding

When a method in a subclass has the same name and type signature as a method in its superclass.

Whenever such a method is called, it always refers to the subclass version.

 

 

 

 

 

To access superclass version, use super.

Class A{

void method1(){

System.out.println("Inside method1 of A");

}

}

 

 

 

 

Class B extends A {

void method1(){

super.method1();

System.out.println("Inside method1 of B");

}

}

 

Using final to prevent Overriding

Class A{

final void method1(){

System.out.println("This is a final method");

}

}

class B extends A{

void method1(){} // ERROR! Can’t override

}

 

 

Using final to prevent Inheritance

final class A{

//…

}

class B extends A { // ERROR! Can’t subclass A

//…

}

Note

When you create a new class, you create new data type.

When an instance variable is accessed by code that is not part of the class in which that instance variable is defined, it must be done through an object of that class, through a dot operator.

When an instance variable is accessed by code that is the part of the same class as the instance variable, that variable can be referred to directly.

Same applies to methods

Any class can have only one superclass but many subclasses.

A superclass variable can reference a subclass Object.