简体   繁体   中英

Inheritance for Bean class in Java

I am working on Jsf2 project with primefaces. I have used some thing like this: Bean class:

Student Its the main Bean class with id and name. I have created two more bean class. Technical student and non technical student both these are extending student bean class. My question is am I doing Right? Is it right design ? I want to keep id and name common which is reusable across technical student and nontechnical students page

class student{
        id, name
        getter and setter
}

class Technicalstudent extends student{
        technicaldetails, 
        get and set
}

class NonTechnicalstudent extends student{
        nontechnicaldetails
        get and set
}

Yes your design is correct from inheritance perspective. Subclass/child need to inherit the common properties from parents and they can have their own properties.

I found nothing wrong with the code based on what you want to achieve. What you did here is called is-a relationship . Meaning a TechnicalStudent and a NonTechnicalstudent is also a Student. Therefore it will inherit every fields from Student. Hope that helps.

Your design is correct but As on Ui you have to map your enity with backend object. Map object with Student object and take aboolean is student object like Boolean isTechnicalStudent and based on the option set it true and false .

On UI end toggle your form of technical and non-technical student details and now due to this boolean you always have a knowledge that wether it is technical or non technical so you can easily type cast.

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