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?
Java For Testers Single Level inheritance - A class inherits properties from a single class. For example, Class B inherits Class A. Multilevel inheritance - A class inherits properties from a class which again has inherits properties. Hierarchical inheritance - Multiple classes inherits properties from a single class.
Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class. Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.
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.
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()
                        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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With