Here is the pet(parent) class
public class Pet {
private String owner;
private String name;
private String type;
private char gender;
public void setOwner(String owner){
this.owner=owner;
}
public String getOwner(){
return owner;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void setGender(char gender ){
gender=Character.toUpperCase(gender);
if (gender=='F' || gender=='M'){
this.gender=gender;
}
else{
this.gender='U';
}
}
public char getGender(){
return gender;
}
public String getPetInfo()
{
return getName() + ", " + getType() + ", " + getGender();
}
}
Here is the interface ISound
public interface ISound {
public String makeSound();
}
Here is the dog class that inherits from pet and implements the interface ISound
public class Dog extends Pet implements ISound{
private String breed;
private String color;
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getPetInfo()
{
String info = super.getPetInfo();
return info + ", " + getBreed();
}
@Override
public String makeSound() {
return "bark";
}
}
Finally here is the Program class with the main
public class Program {
/**
* @param args
*/
public static void main(String[] args) {
Dog d= new Dog();
d.setBreed("Beagle");
d.setGender('m');
d.setName("Spot");
d.setType("Dog");
System.out.println(d.getPetInfo());
System.out.println(d.makeSound());
}
}
No comments:
Post a Comment