A school called ‘Danville’ has a provision of keeping 2 kinds of Presidents: School President and Class president. (President class can be either a concrete class or an abstract class or an interface. You need to determine what it will be from the following clues.) There can be only 1 school president but there can be multiple class presidents. School and class presidents do not have any common attribute but they have a common school name which is fixed as ‘Danville’. Class presidents have a class id which school presidents do not have. Both school president and class president have a common method named exercisePower but their implementations are different. It prints “Exercising power on school” for the former and “Exercising power on class __” for the latter.
import java.util.Scanner;
abstract class President{
//School and class presidents do not have any common attribute but
//they have a common school name which is fixed as ‘Danville’.
public static final String SCHOOL_NAME="Danville";
/**
* Constructor
*/
public President() {}
public void exercisePower() {}
}
class SchoolPresident extends President{
/**
* Constructor
*/
public SchoolPresident() {}
@Override
public void exercisePower() {
System.out.println("Exercising power on school");
}
}
class ClassPresident extends President{
//Class presidents have a class id which school presidents do not have.
private int id;
/**
* Constructor
* @param id
*/
public ClassPresident(int id) {
this.id=id;
}
@Override
public void exercisePower() {
System.out.println("Exercising power on class "+id);
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
}
public class Q195466 {
public static void main(String[] args) {
President schoolPresident =new SchoolPresident();
President classPresident =new ClassPresident(1112354);
System.out.println("School name: "+President.SCHOOL_NAME);
schoolPresident.exercisePower();
classPresident.exercisePower();
}
}
Comments
Leave a comment