Suppose I have an abstract class Person . There is another class Student which extends Person . But the Student class has a member variable, say college of type String , which is not there in Person class.
We know that we can reference a subclass from a superclass, for example,
Person p = new Student();
Will the object p have the member college ?
You won't be able to do p.college
. However, you can cast it to Student
and in this case it will have:
((Student) p).college;
In your sample, Person
object IS a Student
and hence will have the college
member.
Since you cast the Student
to a Person
, any public routines or data not present in Person
will be hidden by the
cast
assignment though.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.