Hi Abbas,
Try this program :
class Person {
}
class Student extends Person {
}
class Teacher extends Person {
}
interface PersonIntf{
}
class PersonImpl implements PersonIntf{
}
public class InstanceofClass {
public static void main(String[] args) {
Person studentobj = new Student();
Person teacherobj = new Teacher();
Person personobj = new Person();
PersonImpl personimplobj = new PersonImpl();
Teacher refandobj_teacher = new Teacher();
if (teacherobj instanceof Student) {
Student student = (Student) teacherobj;
System.out
.println("This will not print as
pteacher is not an instance of Student");
}
if (teacherobj instanceof Teacher) {
Teacher real_teacher_reference = (Teacher) teacherobj;
System.out.println("Object of :"
+ real_teacher_reference.getClass());
}
if (teacherobj instanceof Person) {
Person person_reference = (Person) refandobj_teacher;
System.out.println("Object of :" +
person_reference.getClass());
person_reference = studentobj;
System.out.println("Object of :" +
person_reference.getClass());
person_reference = personobj;
System.out.println("Object of :" +
person_reference.getClass());
}
if (personimplobj instanceof PersonIntf){
System.out.println("Oh yes !! You can also use
instanceof to find
if the objects implements the interface");
}
}
}
Thanks,
Ashok A V
On Wed, Aug 19, 2009 at 2:54 PM, mcalex<[email protected]> wrote:
>
> why don't u add a System.out.println() to your if statement eg:
>
> Student student2 = (Student) person2;
> System.out.println("person2 IS a student");
>
> hth
> mcalex
>
> On Aug 19, 6:22 am, Abbas Zaini <[email protected]> wrote:
>> Hello friends
>> given that:
>> class Person is the super class of both Student and Teacher classes,
>>
>> *Person person1 = new Student();
>> Person person2 = new Teacher();
>> // Do the casting only when the type is verified
>> if (person2 instanceof Student) {
>> Student student2 = (Student) person2;
>>
>> }*
>>
>> we used the instanceof operator to find out if the object "person2" is an
>> instance of "Student" class, but it is not, it is a "Teacher" class
>> instance, and "Student" and "Teacher" are of the same level ( both are
>> direct children of the class "Person" ).
>> so considering the information above will the casting be done?? this is what
>> i want to be sure of, i mean the lessons says that if we cast the person2
>> object directly it will cause a runtime mismatch exception, so we add the
>> condition to be sure that person2 is an instance of Student and the
>> exception won't occur, but that doesn't change that Student is not a super
>> class of Teacher.
>> will "person2" be casted to "student2" ?? i don't think so but i need a
>> confirmation.
>>
>> thank you all.
>>
>> --
>> Abbas Zaini
> >
>
--
Victory belongs to the most persevering.
- Napoleon
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---