How to?

View Answers

September 16, 2008 at 1:18 PM

Hi ,



Methods and constructors both use super to refer to a superclass, but in different ways. Methods use super to execute an overridden method in the superclass


class MethodTest {
void getBirthInfo() {
System.out.println("Rose.");
}
}
class Welcome extends MethodTest {
void getBirthInfo() {
System.out.println("This is simple.");
System.out.print("a mammal normally is ");
super.getBirthInfo();
}
}



Constructors and methods use the keyword this quite differently. A method uses this to refer to the instance of the class that is executing the method. Static methods do not use this; they do not belong to a class instance, so this would have nothing to reference. Static methods belong to the class as a whole, rather than to an instance. Constructors use this to refer to another constructor in the same class with a different parameter list. Study the following code:


public class Welcome {
String name;
Welcome(String input) {
name = input;
}
Welcome() {
this("Welcome to Rose");
}
public static void main(String args[]) {
Welcome p1 = new Welcome("digger");
Welcome p2 = new Welcome();
System.out.println("Welcome to India");

}
}

-----------------------------------------


Visit for more information.

http://www.roseindia.net/java

Thanks

September 18, 2008 at 10:50 PM

thanks for the help









Related Tutorials/Questions & Answers:
Advertisements