The document discusses abstract classes and interfaces in object-oriented programming. It defines an abstract Animal class with subclasses Fish, Spider, and Cat. The Fish class extends Animal and implements the Pet interface. The Cat and Spider classes also extend Animal. The code tests the classes by creating Fish, Cat, and Spider objects and calling their methods.
The document discusses abstract classes and interfaces in object-oriented programming. It defines an abstract Animal class with subclasses Fish, Spider, and Cat. The Fish class extends Animal and implements the Pet interface. The Cat and Spider classes also extend Animal. The code tests the classes by creating Fish, Cat, and Spider objects and calling their methods.
private String catName;public Cat(){ this(""); } public Cat(String name){ super(4); catName=name; } //@Overridepublic void eat() { System.out.println("kuceng mangan iwak"); } public String getName() { return catName; } public void setName(String name) { catName=name; } public void play() { throw new UnsupportedOperationException("Not supported yet."); } }
Pet.java
public interface Pet {
public String getName(); public void setName(String name); public void play(); } TestAnimals.java
public class TestAnimals {
public static void main(String[] args) { Fish d = new Fish(); Cat c = new Cat("puki"); Animal a = new Fish(); Animal e = new Spider(); d.setName("bobi"); d.eat(); System.out.println("iki jenenge iwakku "+d.getName()); d.walk(); c.eat(); System.out.println("jenenge iwakku "+c.getName()); c.walk();((Fish)a).setName("jamal"); ((Fish)a).eat(); System.out.println("jenenge iwakku "+((Fish)a).getName()); ((Fish)a).walk(); ((Spider)e).eat(); ((Spider)e).walk(); } }