Given the code below..... abstract class Animal{ private String name; //define a
ID: 3682598 • Letter: G
Question
Given the code below.....
abstract class Animal{
private String name;
//define a constructor
public Animal(String n){
name=n;
}
public abstract void makeNoise();
public void sayMyName(){
System.out.println("Animal");
}
}
// create a child class or "subclass"
class Dog extends Animal{
//to satisfy the parent constructor which requires a String
public Dog(String s){
super(s); //pass the String back to the parent constructor
}
public void makeNoise(){
System.out.println("Bark, Bark, Bark");
}
public void sayMyName(){
//overriding method from parent
System.out.println("Dog");
}
}
class Cat extends Animal{
public Cat(String s){
super(s); //pass the String back to the parent constructor
}
public void makeNoise(){
System.out.println("Meow, Meow, Meow");
}
public void sayMyName(){ //overriding method from parent
System.out.println("Cat");
}
}
final class Bird extends Animal{ public Bird(String s){
super(s); //pass the String back to the parent constructor
}
public void makeNoise(){
System.out.println("Cheep,Cheep,Cheep");
}
public void sayMyName(){ //overriding method from parent
System.out.println("Bird"); }
}
Create an abstract parent class called student that has a String field Jnumber and has a constructor to supply it.
Create two child classes, one called "Commuter" and one called "Resident"
The child classes will have additional fields, License Number and Dorm, respectively.
The child classes will need set and get methods for their added fields.
Explanation / Answer
Hi, Please find the below classes implemented as per requirement.
Student.java
abstract class Student{
private String jNumber;
//define a constructor
public Student(String number){
jNumber=number;
}
}
class Commuter extends Student{
private int licenseNo;
public int getLicenseNo() {
return licenseNo;
}
public void setLicenseNo(int licenseNo) {
this.licenseNo = licenseNo;
}
public String getDorm() {
return dorm;
}
public void setDorm(String dorm) {
this.dorm = dorm;
}
private String dorm;
public Commuter(String s){
super(s); //pass the String back to the parent constructor
}
}
class Resident extends Student{
private int licenseNo;
private String dorm;
public Resident(String s){
super(s); //pass the String back to the parent constructor
}
public int getLicenseNo() {
return licenseNo;
}
public void setLicenseNo(int licenseNo) {
this.licenseNo = licenseNo;
}
public String getDorm() {
return dorm;
}
public void setDorm(String dorm) {
this.dorm = dorm;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.