Thursday, June 10, 2010

Inheritance and Extended Classes

  • Extended classes are also known as subclasses.
  • Inheritance models the is-a relationship.
  • If class E is an extended class of class B, then any object of E can act-as an object of B.
  • Only single inheritance is allowed among classes.
  • All public and protected members of a super class are accessible in the extended classes.
  • All protected members are also accessible within the package.

When to Overload

When there is a general, non-discrimitive description of the functionality that can fit all the overloaded methodes.

public class StringBuffer {
StringBuffer append(String str) { ... }
StringBuffer append(boolean b) { ... }
StringBuffer append(char c) { ... }
StringBuffer append(int i) { ... }
StringBuffer append(long l) { ... }
StringBuffer append(float f) { ... }
StringBuffer append(double d) { ... }
// ...
}

When all the overloaded methods offer exactly the same functionality, and some of them provide default values for some of the parameters.

public class String {
public String substring(int i, int j) {
// base method: return substring [i .. j-1]
}
public String substring(int i) {
// provide default argument
return substring(i, length - 1);
}
// ...
}

Wednesday, June 9, 2010

Example on Overloading

  • Each method has a signature: its name together with the number and types of its parameters

    MethodsSignatures
    String toString()
    void move(int dx, int dy)
    void paint(Graphics g)
    ()
    (int, int)
    (Graphics)


  • Two methods can have the same name if they have different signatures. They are overloaded.
  • Overloading Example

    Public class Point {

    protected double x, y;

    public Point() {

    x=0.0; y=0.0;

    }

    public Point (double x, double y) {

    this.x=x; this.y=y;

    }

    /**calculate the distance between this point and the other point*/

    public double distance (Point other) {

    double dx=this.x-other.x;

    double dy=this.y-other.y;

    return Math.sqrt(dx*dx+dy*dy);

    }

    /**calculate the distance between this point and (x,y)*/
    public double distance(double x, double y) {
    double dx = this.x - x;
    double dy = this.y - y;
    return Math.sqrt(dx * dx + dy * dy);
    }

    /**calculate the distance between this point and the origin*/
    public double distance() {
    return Math.sqrt(x * x + y * y);
    }
    // other methods
    }

Tuesday, June 8, 2010

Example on Method overriding

VBScript - Dictionary Object

Add an Item
Remove All
Remove
Key exists

Add an Item to Dictionary
Set oDictionary = CreateObject("Scripting.Dictionary")
oDictionary.Add "a","Akhila" 'Adds a item and key pair
oDictionary.Add "b","Bhanu"
oDictionary.Add "d","Deepthi"
MsgBox "The number of key/item pairs "&oDictionary.Count
For Each obj in oDictionary
msgbox "The key for "&obj&" and the associated data is "&oDictionary(obj)
Next
Msgbox "The value of key a is "&oDictionary.Item("a")

Determine if a specified key exists in the Dictionary Object
Set oDictionary = CreateObject("Scripting.Dictionary")
oDictionary.Add "a","Akhila" 'Adds a item and key pair
oDictionary.Add "b","Bhanu"
if oDictionary.Exists("b") Then msgbox "Key exists in the Dictionary"

Remove a key/item pair from Dictionary Object
Set oDictionary = CreateObject("Scripting.Dictionary")
oDictionary.Add "a","Akhila" 'Adds a item and key pair
oDictionary.Add "b","Bhanu"
oDictionary.Add "d","Deepthi"
oDictionary.Remove("b")

Removes all key/item pairs from Dictionary Object
Set oDictionary = CreateObject("Scripting.Dictionary")
oDictionary.Add "a","Akhila" 'Adds a item and key pair
oDictionary.Add "b","Bhanu"
oDictionary.Add "d","Deepthi"
oDictionary.RemoveAll

Friday, June 4, 2010

What is Garbage Collections

The >Net Framework's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap. As long as address space is available in the managed heap, the runtime continues to allocate space for new objects. However, memory is not infinite. Eventually the garbage collector must perform a collection in order to free some memory. The garbage collector's optimizing engine determines the best time to perform a collection, based upon the allocations being made. When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.