Biblioteca Java - Blame information for rev 39
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
39 | mihai | 1 | package introducere.java.operator; |
4 | mihai | 2 | |
3 | /** | ||
4 | * The instanceof operator compares an object to a specified type. | ||
5 | * You can use it to test if an object is an instance of a class, | ||
6 | * an instance of a subclass, or an instance of a class that implements a | ||
7 | * particular interface.The following program, InstanceofDemo, defines a parent | ||
8 | * class (named Parent), a simple interface (named MyInterface), and a child class | ||
9 | * (named Child) that inherits from the parent and implements the interface. | ||
10 | */ | ||
11 | public class InstanceofDemo { | ||
12 | public static void main(String[] args) { | ||
13 | |||
14 | Parent obj1 = new Parent(); | ||
15 | Parent obj2 = new Child(); | ||
16 | |||
17 | System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent)); | ||
18 | System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child)); | ||
19 | System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface)); | ||
20 | System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent)); | ||
21 | System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child)); | ||
22 | System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof MyInterface)); | ||
23 | } | ||
24 | } | ||
25 | |||
26 | class Parent{} | ||
27 | |||
28 | class Child extends Parent implements MyInterface{} | ||
29 | |||
30 | interface MyInterface{} |