<< Chapter < Page | Chapter >> Page > |
Furthermore, the overuse of static members can lead to problems similar to those experienced in languages like C and C++ that support global variables and global functions.
The use of static members will be discussed in the next module.
This section contains a variety of miscellaneous information.
Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.
I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.
In the past, unknown individuals have misappropriated copies of my modules from cnx.org, converted them to Kindle books, andplaced them for sale on Amazon.com showing me as the author. I receive no compensation for those sales and don't know who doesreceive compensation. If you purchase such a book, please be aware that it is a bootleg copy of a module that is freelyavailable on cnx.org.
Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.
A complete listing of the sample program is shown in Listing 15 below.
Listing 15 . Complete program listing. |
---|
/*File Poly06.java
Copyright 2002, R.G.BaldwinThis program illustrates polymorphic
behavior using interfaces in additionto class inheritance.
The program output is:p in B
q in Bp in B
q in Bx in A
toString in Ap in B
p in Cq in C
**************************************/interface I1{
public void p();}//end interface I1
//===================================//interface I2 extends I1{
public void q();}//end interface I2
//===================================//class A extends Object{public String toString(){
return "toString in A";}//end toString()
//---------------------------------//public String x(){return "x in A";
}//end x()//---------------------------------//
}//end class A//===================================//
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//===================================//
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//===================================//
public class Poly06{public static void main(
String[]args){
I1 var1 = new B();var1.p();//OK
//var1.q();//won't compile((I2)var1).q();//OK
System.out.println("");//blank lineI2 var2 = new B();var2.p();//OK
var2.q();//OK//Following won't compile
//String var3 = var2.x();String var3 = ((A)var2).x();//OK
System.out.println(var3);var3 = var2.toString();//OK
System.out.println(var3);
System.out.println("");//blank lineObject var4 = new B();//var4.p();//won't compile
((I1)var4).p();//OKSystem.out.println("");//blank linevar2 = new C();
var2.p();//OKvar2.q();//OK
System.out.println("");//blank line}//end main
}//end class Poly06 |
-end-
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?