QA's approach 2 Java - Abstract Class vs Interface

Abstract Class vs Interface series 2: Example

1. Create package with runTimePolymorphism
2. Create interface with name as "Reviews
3. Create abstract class with name as "CompanyReviews
4. Create implementation classes with names as "GlobalDoor" & "MySurvey" 

The Class hierarchy diagram will be as follows:

The Package Diagram is as follows:











Add the following logic to Business logic classes


Reviews.java:

package RunTimePolymorphism;

public interface Reviews {

public String getReviews(String... args);
}

Details.java:

package RunTimePolymorphism;

public class Details {

int ratings = 0;

Float salary = 0.0f;

String pros = null;

String cons = null;

public int getRatings(String companyName) {

switch (companyName) {

case "Bally":

ratings = 4;

return ratings;

default:

ratings = 2;

return ratings;

}

}

public Float getSalary(String companyName, String designation) {

switch (companyName) {

case "Bally":

if (designation.equalsIgnoreCase("GET"))

salary = 540000.0f;

else if (designation.equalsIgnoreCase("AQA"))

salary = 600000.0f;

else if (designation.equalsIgnoreCase("QA"))

salary = 740000.0f;

else if (designation.equalsIgnoreCase("SQA"))

salary = 850000.0f;

if (designation.equalsIgnoreCase("LEAD"))

salary = 1100000.0f;

else if (designation.equalsIgnoreCase("OTHERS"))

salary = 0.0f;

return salary;

default:

if (designation.equalsIgnoreCase("GET"))

salary = 360000.0f;

else if (designation.equalsIgnoreCase("AQA"))

salary = 400000.0f;

else if (designation.equalsIgnoreCase("QA"))

salary = 540000.0f;

else if (designation.equalsIgnoreCase("SQA"))

salary = 650000.0f;

else if (designation.equalsIgnoreCase("LEAD"))

salary = 800000.0f;

return salary;

}

}

public String getPros(String companyName) {

switch (companyName) {

case "Bally":

pros = "Good place to work with lots of learning & competitive salary structure";

return pros;

default:

pros = "Good for gaining experience & salary structure was ok";

return pros;

}

}

public String getCons(String companyName) {

switch (companyName) {

case "Bally":

cons = "nothing much";

return cons;

default:

cons = "Needs to provide more exposure & implement new technologies";

return cons;

}

}

}




CompanyReviews.java:

package RunTimePolymorphism;

public abstract class CompanyReviews implements Reviews {

public final String heading = "Reviews of the selected Company is as follows: ";

public static Details instantiate() {

return new Details();

}

}



GlobalDoor.java

package RunTimePolymorphism;

public class GlobalDoor extends CompanyReviews {

final String ORGCOMPANY = "GLOBALDOOR";

String companyName = null;

String designation = null;

Details details = CompanyReviews.instantiate();

@Override

public String getReviews(String... args) {

this.companyName = args[0];

this.designation = args[1];

return ORGCOMPANY+" " + heading + "\n" + "Salary of employee is: " + details.getSalary(companyName, designation)+ "\n" +

"Rating of the company is: " + details.getRatings(companyName) + "\n" + "Company Pros are as follows: "

+ details.getPros(companyName) + "\n" + "Company cons are as follows: " + details.getCons(companyName);

}

}


MySurvey.java

package RunTimePolymorphism;

public class MySurvey extends CompanyReviews {

final String ORGCOMPANY="MYSURVEY";
String companyName = null;
String designation = null;
Details details = CompanyReviews.instantiate();

@Override
public String getReviews(String... args) {
this.companyName = args[0];
this.designation = args[1];
return ORGCOMPANY+heading + "'\n" + "salary of employee" + details.getSalary(companyName, designation)
+ "Rating of the company is" + details.getRatings(companyName) + "company Pros "
+ details.getPros(companyName) + "Company cons" + details.getPros(companyName);
}

}

Execution logic class is as follows


TestCompanyReviews.java

package RunTimePolymorphism;

public class TestCompanyReviews {

public static void main(String... args) {
// TODO Auto-generated method stub
CompanyReviews cr=null;
try {
cr=(CompanyReviews)Class.forName(args[0]).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(cr.getReviews(args[1],args[2]).toString());

}
}


Execute the program:

set the run configuration with fully qualified name of the class, companyName, designation as arguments at runtiime 


click on apply & the Run, the output obtained is as follows:




Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Selenium 4 absolute beginners - How to create Batch execution file

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?