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.