简体   繁体   中英

Inheritance in Java and Object Types

I'm currently studying a book for the AP CS A exam, specifically the Barron's book for test preparation.

One section of the book refers to two classes, Student and GradStudent , where GradStudent extends Student .

GradStudent has the method getId() while Student does not.

If I were to run the following code:

Student s = new GradStudent()
s.getId()

The book informs me that I would get an error. Why is this? Since I am initializing it as a GradStudent , wouldn't s have access to the method getId() ?

Essentially, if I declare a variable as the superclass, and initialize it to the subclass, what happens?

In other words, how do s and g in the following example differ:

Student s = new GradStudent()
GradStudent g = new GradStudent()

EDIT:

I've now understood that s only has access to the methods in the Student class. So what happens if I do the following:

Student s = (new GradStudent().setId(1) )

What happens to the id field? (Assuming it is only present in the GradStudent class) If I casted s to GradStudent again, would it be able to access the same id ?

You get an error because s is declared to be type Student . It doesn't matter that it was instantiated as a new GradStudent() , the compiler only knows what type s was declared as. So basically you can only use s as if it were a Student (you can only use methods defined by the Student class).

If you really need to use .getId() , you have two options. You can declare s as a GradStudent :

GradStudent s = new GradStudent();
System.out.println(s.getId());

Or, you can cast s to GradStudent :

Student s = new GradStudent();
System.out.println(((GradStudent) s).getId());

The variable

Student s

is declared as a Student , not a GradStudent . You assigned a GradStudent instance to the Student variable - this does not change the type of said variable.

You could do the following to address the compile time error :

Student s = new GradStudent()
((GradStudent)s).getId()

but this is considered a code smell.

Exactly because the GradStudent has the method getId() while Student does not.

Your GradStudent inherits the Student methods but Student class only has their OWN methods.

It will work if GradStudent implements a Student getId()

I hope you have got your answer given by above folks. Just wanted to add some points.

Calling method s.getId() would end up giving you an error because s is a type of Student and at the compile time compiler checks if getId() method exists in Student and if it doesn't find it, will give you an error.

But s is pointing to an object of subtype which is GradStudent and it would be resolved at run time and compiler is unaware of it. So to satisfy compiler you have to downcast s like this

((GradStudent)s).getId()

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM