- 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.
Thursday, June 10, 2010
Inheritance and Extended Classes
When to Overload
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
Methods Signatures 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
VBScript - Dictionary Object
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