HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Introduction to OO: part 2 - Polymorphism"
Vol. 2, Issue 12, p. 16

	

Listing 1: Abstract class Mammal extends Vertebrae. 

{ 
   public static final int   MALE      =   0; 
   public static final   int   FEMALE   =   1; 
   public static final   int   HERBIVORE   =   0; 
   public static final   int   CARNIVORE   =   1; 
   public static final   int   OMNIVORE   =   2; 
  
  

   public final boolean   hasWarmBlood      =   true; 
   public final boolean   givesBirthToLiveYoung   =   true; 
   public final boolean   hasFur         =   true; 

   public int      gender;      // 0 for male, 1 for female 
   public int      numLegs;   // 
   public int      dietHabits;   // 0 for herbivore, 1 for carnivore, 2 for 
omnivore 
    
   abstract public void walk(); 
  

}   // end class 

Listing 2: Class Human extends Mammal. 

{    
   String      name; 

   public Human() 
   { 
      gender      =   MALE; 
      numLegs    =    2; 
      dietHabits   =   OMNIVORE; 
   } 
    
   public void walk() 
   { 
      // The code that describes how a human walks goes here. 
   } 

   public void goToWork() 
   { 
      // The code that describes how a human goes to work goes here. 
   } 

}   // end class 
  

Listing 3: Class Canine extends Mammal. 

{ 

   public Canine() 
   { 
      gender      =   MALE; 
      numLegs    =    4 
      dietHabits   =   CARNIVORE; 
   } 

   public void walk() 
   { 
      // the code that describes how a human walks goes here 
   } 

   public void bark() 
   { 
      // Woof! 
   } 
} 

Listing 4. 

... 
... 
Human john = new Human();      // create a human object 
Mammal m;            // declare a Mammal variable 
m = john;            // this is legal;




 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.