The this Keyword The super Keyword

J.E.D.I. The following example demonstrates how to declare and use an interface. interface MyInterface { void iMethod; } class MyClass1 implements MyInterface { public void iMethod { System.out.printlnInterface method.; } void myMethod { System.out.printlnAnother method.; } } class MyClass2 implements MyInterface { public void iMethod { System.out.printlnAnother implementation.; } } class InterfaceDemo { public static void mainString args[] { MyClass1 mc1 = new MyClass1; MyClass2 mc2 = new MyClass2; mc1.iMethod; mc1.myMethod; mc2.iMethod; } } Here is the expected output: Interface method. Another method. Another implementation.

1.3.14 The this Keyword

The this keyword can be used for the following reasons: 1. Disambiguate local attribute from a local variable 2. Refer to the object that invoked the non-static method 3. Refer to other constructors As an example for the first purpose, consider the following code wherein the variable data serves as an attribute and a local parameter at the same time. class ThisDemo1 { int data; void methodint data { this.data = data; this.data refers to the attribute while data refers to the local variable } } The following example demonstrates how this object is implicitly referred to when its non-static members are invoked. Introduction to Programming II Page 19 J.E.D.I. class ThisDemo2 { int data; void method { System.out.printlndata; this.data } void method2 { method; this.method; } } Before looking at another example, lets first review the meaning of method overloading. A constructor like a method can also be overloaded. Different methods within a class can share the same name provided their parameter lists are different. Overloaded methods must differ in the number andor type of their parameters. The next example has overloaded constructors and the this reference can be used to refer to other versions of the constructor. class ThisDemo3 { int data; ThisDemo3 { this100; } ThisDemo3int data { this.data = data; } } Coding Guidelines: Call to this should be the first statement in the constructor.

1.3.15 The super Keyword

The use of the super keyword is related to inheritance. It used to invoke superclass constructors. It can also be used like the this keyword to refer to members of the superclass. The following program demonstrates how the super reference is used to call superclass constructors. class Person { String firstName; String lastName; PersonString fname, String lname { firstName = fname; lastName = lname; } } class Student extends Person { String studNum; StudentString fname, String lname, String sNum { superfname, lname; studNum = sNum; } } Coding Guidelines: Introduction to Programming II Page 20 J.E.D.I. super refers to the immediate superclass. It should be the first statement in the subclasss constructor. The keyword can also be used to refer to superclass members as shown in the following example. class Superclass{ int a; void display_a{ System.out.printlna = + a; } } class Subclass extends Superclass { int a; void display_a{ System.out.printlna = + a; } void set_super_aint n{ super.a = n; } void display_super_a{ super.display_a; } } class SuperDemo { public static void mainString args[]{ Superclass SuperObj = new Superclass; Subclass SubObj = new Subclass; SuperObj.a = 1; SubObj.a = 2; SubObj.set_super_a3; SuperObj.display_a; SubObj.display_a; SubObj.display_super_a; System.out.printlnSubObj.a; } } The program displays the following result. a = 1 a = 2 a = 3 2

1.3.16 The static Keyword