Java Abstract Classes and Methods Outcome: Show that you are able to: demonstrat
ID: 3905917 • Letter: J
Question
Java Abstract Classes and Methods
Outcome:
Show that you are able to:
demonstrate the ability to create abstract classes
demonstrate the ability to create abstract methods
demonstrate the ability to understand that an abstract class can have both concrete and abstract methods
understand that abstract classes cannot be instantiated
understand that you cannot have an object of type abstract class
understand that the purpose of an abstract class is to impose rules on all child classes.
You are to make up your own theme for this assignment, make it simple to be used as an example to teach others.
You are to make up your own classes for this assignment, again, make it simple to be used as an example to teach others.
You are to create an Abstract Class with Abstract Methods, a class that uses the abstract methods and a test class
You are to create all necessary setters, getters, constructors and methods.
You are to create a reasonable test case
YOU CANNOT:
Use global variables, word goto, the break command outside a case statement
Explanation / Answer
Person.java
**********************
package sample;
//abstract class
public abstract class Person {
private String name;
private String gender;
public Person(String nm, String gen){
this.name=nm;
this.gender=gen;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the gender
*/
public String getGender() {
return gender;
}
/**
* @param gender the gender to set
*/
public void setGender(String gender) {
this.gender = gender;
}
//abstract method
public abstract void work();
@Override
public String toString(){
return "Name="+this.name+"::Gender="+this.gender;
}
public void changeName(String newName) {
this.name = newName;
}
}
Employee.java
**************************
package sample;
public class Employee extends Person {
private int empId;
public Employee(String nm, String gen, int id) {
super(nm, gen);
this.empId=id;
}
/**
* @return the empId
*/
public int getEmpId() {
return empId;
}
/**
* @param empId the empId to set
*/
public void setEmpId(int empId) {
this.empId = empId;
}
@Override
public void work() {
if(empId == 0){
System.out.println("Not working");
}else{
System.out.println("Working as employee!!");
}
}
}
AbstractTest.java
**************************
package sample;
public class AbstractTest {
public static void main(String args[]){
//coding in terms of abstract classes
Person student = new Employee("Dove","Female",0);
Person employee = new Employee("Suvam","Male",123);
student.work();
employee.work();
//using method implemented in abstract class - inheritance
employee.changeName("John Doe");
System.out.println(employee.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.