<< Chapter < Page | Chapter >> Page > |
Listing 3 . Definition of the class named B. |
---|
class B extends A implements I2{
public void p(){System.out.println("p in B");
}//end p()//---------------------------------//public void q(){
System.out.println("q in B");}//end q();
//---------------------------------//}//end class B |
Actually implements two interfaces
Although it isn't obvious from an examination of Listing 3 alone, the class named B actually implements both I2 and I1 . This is because the interface named I2 extends I1 . Thus, the class named B implements I2 directly, and implements I1 through interface inheritance.
The cardinal rule
In case you have forgotten it, the cardinal rule for implementing interfaces is:
If a class implements an interface, it must provide a concrete definition for all the methods declared by that interface, and all the methods inherited by that interface. Otherwise, the class must be declared abstract and the definitions must be provided by a class that extends the abstract class.
Must define two methods
As a result, the class named B must provide concrete definitions for the methods p and q . (The method named p is declared in interface I1 and the method named q is declared in interface I2 .)
As you can see from Listing 3 , the behavior of each of these methods is to display a message indicating that it has been executed. This will be useful later to tell us exactly which method is executed when we exercise the objects in the main method of the driver class.
The class named C
Listing 4 shows the upgraded version of the class named C .
Listing 4 . Definition of the class named C. |
---|
class C extends Object implements I2{
public void p(){System.out.println("p in C");
}//end p()//---------------------------------//public void q(){
System.out.println("q in C");}//end q();
//---------------------------------//}//end class B |
In this upgraded version, the methods named p and q each display a message indicating that they have been executed. Again, this will be useful later to let us know exactly which version of the methods named p and q get executed when we exercise the objects.
The driver class
Listing 5 shows the beginning of the class named Poly06 . The main method in this class instantiates objects of the classes named B and C , and exercises them to illustrate what can, and what cannot be done with them.
Listing 5 . Beginning of the class named Poly06. |
---|
public class Poly06{
public static void main(String[] args){I1 var1 = new B();
var1.p();//OK |
A new data type
As explained in the previous module, when you define a new interface, you create a new data type.
You can store the reference to any object instantiated from any class that implements the interface in a reference variable of that type.
A new object of the class B
The code shown in Listing 5 instantiates a new object of the class B .
Important: stored as type I1
It is important to note that the code in Listing 5 stores the object's reference in a reference variable of the interface type I1 (not as the class type B ) .
Call an interface method
Following this, the code in Listing 5 successfully calls the method named p on the reference, producing the following output on the computer screen:
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?