0

Is it possible to access Methods of another class using an object from another class?

Our Lecturer gave this code: lenz.getCourses().removeCourses("CS210"); Where "lenz" Is a reference of an object of class Student. 👇 Student lenz= New Student ("Lance","Merit","20201,courseList); The getCourses() method let's assume is defined in the Student class, that method we can access cause we have an instance of the Class referenced by lenz. But the removeCourses() method is a method defined in another Class called Course. I was thinking in order to access a method of a class u create an instance of that class. But up there he's using an instance of another class( in this case the Student class object referenced by lenz) to access a method(*removeCourses()) defined in another Class (Course). I'm new with this OOP Programming.Please someone explain how he did that? Cause he didn't....

14th Mar 2020, 2:29 AM
Nica STRAIT
Nica STRAIT - avatar
1 ответ
+ 1
Yes you can do this if getCourse() return the object of Course class.You can call the public methods and variables using there object reference variable.
14th Mar 2020, 3:16 AM
Prathvi
Prathvi - avatar