Thread: InstanceOF
View Single Post
  #2 (permalink)  
Old 11-28-2007, 06:33 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,122
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
The simplest way to say is that, this is the way you cast an Object in java:

Code:
class Point { int x, y; }
class Element { int atomicNumber; }
class Test {
    public static void main(String[] args) {
        Point p = new Point();
        Element e = new Element();
        if (e instanceof Point) { // compile-time error
            System.out.println(“I get your point!”);
            p = (Point)e;     // compile-time error
        }
    }
}
It's some type of typecasting, this case wouldn't work because Element is not A Point.. so in order to fulfill that requirement, you need to create:

Code:
class Point extends Element { int x, y; }
As you see in the inheritance, Element is having Point as a parent, and Element is now A Point.

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by HelloWorld : 11-28-2007 at 06:39 PM.
Reply With Quote