简体   繁体   中英

How to access the superclass object from the subclases?

I have been asked to write a program in java which has a class named 1.College(it has 'collegeName' and 'address' as its data members) 2.Department which inherits college(and has 'departmentName' and 'hodName' as its data members) 3. FaultyMember which inherits Department(it has 'facultyMember', 'facultyMemberQualification' and 'yearsOfExperience' as its data members).

I want user to enter the name of the college and its address. And then ask the user to input the no of departments in the college then the name of the each department and the corresponding HOD of the departments. Now, as the user inputs the name of the first department the program also asks the no of faculty members in each departments and takes input of the faculty member name, qualification,and years of experience.

The problem which I am facing is that in my qestion the super class 'Department' needs to access the subclass 'FacultyMember'.

How to solve the problem.

I have written the code so far as...

Hi without going into the design semantic of your senario. Answer to the question is.

A parent class has know obligation to know about its child. Hence you can't access child class fields inside a parent.

About the design of the senario. Kindly read about "Has a" and "is a" relationship.

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