Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 354
populationzero Avatar asked Apr 19 '18 17:04

populationzero


People also ask

What is inheritance and its types in Java?

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.

What is object and its types in Java?

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.


4 Answers

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());
like image 89
k_ssb Avatar answered Oct 22 '22 07:10

k_ssb


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.

like image 35
Amir Afghani Avatar answered Oct 22 '22 07:10

Amir Afghani


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()
like image 36
Uzair Avatar answered Oct 22 '22 07:10

Uzair


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()

like image 39
Glim Avatar answered Oct 22 '22 08:10

Glim