(Polymorphism And Inheritance) How To Call Java Method Polymorphically
(Polymorphism And Inheritance) How To Call Java Method Polymorphically
In this case am calling toString() Method from different classes Polymorphically. I have a for loop that loops through all created objects while invoking toString() method from each class. (Employee Student Faculty Staff). I have multiple classes that inherits from one class (Person) .
SOURCE CODE
PersonEmployeeStudentFacultyStaffPolymorphismInDriver.java
/* * (Polymorphism And Inheritance) How To Call Java Method Polymorphically. * https://mauricemuteti.info/polymorphism-and-inheritance-how-to-call-java-method-polymorphically/ */ package personemployeestudentfacultystaffpolymorphisminjava; /** * * @author HP */ public class PersonEmployeeStudentFacultyStaffPolymorphismInDriver { /** * @param args the command line arguments */ public static void main(String[] args) { //Creating Person array of size 4 Person[] arrPerson = new Person[4]; //create objects of each class by passing argumnents //and add them in persons array. Person p1 = new Employee("Jane Doe", "14/02/2000", 25); arrPerson[0] = p1; Person p2 = new Faculty("John Hellen", "13/06/2001", 9); arrPerson[1] = p2; Person p3 = new Staff("Foo Bar", "12/12/2002", "IT Support"); arrPerson[2] = p3; Person p4 = new Student("Adam Aden", "1107/2003", "Active"); arrPerson[3] = p4; //Passing Persons array in identifyPersons() Static Method. identifyPersons(arrPerson); } /** * * @param personsArray */ public static void identifyPersons(Person[] personsArray) { // using for loop display each object toString Data Polymorphically. for (int i = 0; i < personsArray.length; i++) { System.out.println("\nPerson " + (i + 1)); //Print On The Console Using ToString Method. System.out.println(personsArray[i].toString()); } } }
Person.java
/* * (Polymorphism And Inheritance) How To Call Java Method Polymorphically. * https://mauricemuteti.info/polymorphism-and-inheritance-how-to-call-java-method-polymorphically/ */ package personemployeestudentfacultystaffpolymorphisminjava; /** * * @author HP */ public class Person { private String name; private String birthDate; /** * * @param name * @param birthDate */ public Person(String name, String birthDate) { this.name = name; this.birthDate = birthDate; } /** * Get the value of birthDate * * @return the value of birthDate */ public String getBirthDate() { return birthDate; } /** * Set the value of birthDate * * @param birthDate new value of birthDate */ public void setBirthDate(String birthDate) { this.birthDate = birthDate; } /** * Get the value of name * * @return the value of name */ public String getName() { return name; } /** * Set the value of name * * @param name new value of name */ public void setName(String name) { this.name = name; } @Override public String toString() { return "Person{" + "name=" + name + ", birthDate=" + birthDate + '}'; } }
Employee.java
/* * (Polymorphism And Inheritance) How To Call Java Method Polymorphically. * https://mauricemuteti.info/polymorphism-and-inheritance-how-to-call-java-method-polymorphically/ */ package personemployeestudentfacultystaffpolymorphisminjava; /** * * @author HP */ public class Employee extends Person { private int workingDays; /** * * @param name * @param birthDate * @param workingDays */ public Employee(String name, String birthDate, int workingDays) { super(name, birthDate); this.workingDays = workingDays; } /** * Get the value of workingDays * * @return the value of workingDays */ public int getWorkingDays() { return workingDays; } /** * Set the value of workingDays * * @param workingDays new value of workingDays */ public void setWorkingDays(int workingDays) { this.workingDays = workingDays; } @Override public String toString() { return super.toString() + " Employee{" + "workingDays=" + workingDays + '}'; } }
Student.java
/* * (Polymorphism And Inheritance) How To Call Java Method Polymorphically. * https://mauricemuteti.info/polymorphism-and-inheritance-how-to-call-java-method-polymorphically/ */ package personemployeestudentfacultystaffpolymorphisminjava; /** * * @author HP */ public class Student extends Person { private String status; /** * * @param status * @param name * @param birthDate */ public Student(String status, String name, String birthDate) { super(name, birthDate); this.status = status; } /** * Get the value of status * * @return the value of status */ public String getStatus() { return status; } /** * Set the value of status * * @param status new value of status */ public void setStatus(String status) { this.status = status; } @Override public String toString() { return super.toString() + " Student{" + "status=" + status + '}'; } }
Faculty.java
/* * (Polymorphism And Inheritance) How To Call Java Method Polymorphically. * https://mauricemuteti.info/polymorphism-and-inheritance-how-to-call-java-method-polymorphically/ */ package personemployeestudentfacultystaffpolymorphisminjava; /** * * @author HP */ public class Faculty extends Person { private int officeHours; /** * * @param officeHours * @param name * @param birthDate */ public Faculty(String name, String birthDate, int officeHours) { super(name, birthDate); this.officeHours = officeHours; } /** * Get the value of officeHours * * @return the value of officeHours */ public int getOfficeHours() { return officeHours; } /** * Set the value of officeHours * * @param officeHours new value of officeHours */ public void setOfficeHours(int officeHours) { this.officeHours = officeHours; } @Override public String toString() { return super.toString() + " Faculty{" + "officeHours=" + officeHours + '}'; } }
Staff.java
/* * (Polymorphism And Inheritance) How To Call Java Method Polymorphically. * https://mauricemuteti.info/polymorphism-and-inheritance-how-to-call-java-method-polymorphically/ */ package personemployeestudentfacultystaffpolymorphisminjava; /** * * @author HP */ public class Staff extends Person { private String title; /** * * @param name * @param birthDate * @param title */ public Staff(String name, String birthDate, String title) { super(name, birthDate); this.title = title; } /** * Get the value of title * * @return the value of title */ public String getTitle() { return title; } /** * Set the value of title * * @param title new value of title */ public void setTitle(String title) { this.title = title; } @Override public String toString() { return super.toString() + " Staff{" + "title=" + title + '}'; } }
NETBEANS OUTPUT
run:
Person 1
Person{name=Jane Doe, birthDate=14/02/2000} Employee{workingDays=25}
Person 2
Person{name=John Hellen, birthDate=13/06/2001} Faculty{officeHours=9}
Person 3
Person{name=Foo Bar, birthDate=12/12/2002} Staff{title=IT Support}
Person 4
Person{name=1107/2003, birthDate=Active} Student{status=Adam Aden}
BUILD SUCCESSFUL (total time: 0 seconds)