Skip to content
Advertisement

How to create objects for parameterized constructors in Java, when we have two classes with the same attributes?

I have class Student, that has first name, last name and age, and a method to print the name and the age of the student.

JavaScript

And I have a second class Professor, that has first name, last name, and university. And I have a method to print the info about the professor.

JavaScript

And in the main class I create two objects.

JavaScript

And it’s showing me an error: String cannot be converted to Student, can someone explain why is that, and how should I fix this?

Advertisement

Answer

You’ve created the objects properly but are not calling their functions properly.

JavaScript

You need to name the instance first then specify the method inside to call after the dot.

Advertisement