View Single Post
  #2 (permalink)  
Old 01-18-2008, 11:56 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Polymorphism
Hello java_fun2007

If you create an instance of DVD or of Cassete, it is also an instance of Video, since DVD and Cassete are extended from Video and contains all the inheritable attributes and methods of Video. That means that you can have a variable, media, that can contain a DVD or Cassete. So lets a media variable with a DVD in it. The new keyword use used to create instances of a class.
Code:
Video media = new DVD();
Now, we know that media is an Video but we might want to know if it is a DVD or a Cassete. For that we use the instanceof keyword for a boolean test.
Code:
if (media instanceof DVD) System.out.println("I'm a DVD"); if (media instanceof Cassete) System.out.println("I'm a Cassete");
I use this stuff regularly, so I hope this helped you.
__________________
If your ship has not come in yet then build a lighthouse.
Reply With Quote