Skip to content
Advertisement

What is the Java way to apply inheritance in this particular problem?

I have two classes A and B and I have one email generating script.

In python, the code would look something like

def generateEmail(message):
    #implementation

def getMessage(item_list):
    message = []
    for item in item_list:
        #do some processing 
    return message

def main():
    #other code
    generateEmail(getMessage(item_list_A))
    generateEmail(getMessage(item_list_B))

This doesn’t work for Java as it is strongly-typed. How can I implement this in java?

Edit: Here are the A (Student) and B (Teacher) classes:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Student{
   int studentId;
   String studentName;
   LocalDate dateOfAddmission;
   float percentage;
   int currentStd;
   String father;
   String mother;
   LocalDate dob;
   String address;
   Boolean disability;
}


@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Teacher{
   int teacherId;
   String teacherName;
   LocalDate dateOfJoining;
   LocalDate dateOfRetirement;
   Boolean classTeacher;
   int currentStd = null;
   LocalDate dob;
   String address;
   Boolean disability;
}

Here is the processing and generating email class for A.

public class generateEmail{
    public Boolean generateEmail(ArrayList<Student> listOfStudent){
        //code to fetch the list of relevant students ids
        ArrayList<int> relevantStudents = getRelevantStudents();
        ArrayList<String> messageList = new ArrayList<>();

        listOfStudent.forEach(student->{
           if(relevantStudents.contains(student.getStudentId()){
                //create a message string from the student information and add to the message list
           }
        }
        //create an excel sheet with the message strings 
       //send email 
    }
}

I need to create a similar report generating method for teachers but the attributes differ so I needed help with that.

Advertisement

Answer

public interface Dummy{
}

public class A implements Dummy{
   int studentId;
   String studentName;
   LocalDate dateOfAddmission;
   float percentage;
   int currentStd;
   String father;
   String mother;
   LocalDate dob;
   String address;
   Boolean disability;

}

public class B implements Dummy{
   int teacherId;
   String teacherName;
   LocalDate dateOfJoining;
   LocalDate dateOfRetirement;
   Boolean classTeacher;
   int currentStd = null;
   LocalDate dob;
   String address;
   Boolean disability;
}

public class MainClass{
    public static void main(String...array){
        //other code
        generateEmail(getMessage(itemListA));
        generateEmail(getMessage(itemListB));
    }
    
    private List<String> getMessage(List<Dummy> itemList){
         List<String> message = new ArrayList<>();
        for(Dummy item : itemList){
            //if required typecast the dummy object and do the processing                           
            //accordingly
            //do some processing 
        }
        return message
    }
    
    private void generateEmail(List<String> messages){
         //Do your processing
    }


}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement