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
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to