80 likes | 244 Views
Abstract Class, Interface, Package. Membuat sebuah project yang mengimplementasikan interface dan abstract class, dengan rincian sbb Abstract Class Animal Interface Flyer Interface Speaker Class Dog : extends Animal and implements Speaker Class Cat : extends Animal and implements Speaker
E N D
Membuat sebuah project yang mengimplementasikan interface dan abstract class, dengan rincian sbb • Abstract Class Animal • Interface Flyer • Interface Speaker • Class Dog : extends Animal and implements Speaker • Class Cat : extends Animal and implements Speaker • Class Bird : extends Animal and implements Speaker, Flyer • Class Bat : extends Animal and implements Flyer • Main.java (main class)
Animal <<abstract>> Dog Cat Bird Bat Interface : Flyer Interface : Speaker
Class Animal public abstract class Animal { private int age; public void setAge(intnewAge) { this.age=newAge; } public intgetAge() { return age; } public abstract void eat() ; public abstract void walk(); }
Interface Flyer public interface Flyer { public void flying(); }
Class Cat public class Cat extends Animal implements Speaker { String name; public Cat(String newName) { this.name=newName; } @Override public void eat() { System.out.println(name+" eat fish"); } @Override public void walk() { System.out.println(name+" with four leg"); } public void speaking() { System.out.println(name+" speaks MIAUW MIAUW"); } }
Class Main public class Main{ public static void main(String[] args) { Cat caty=new Cat("Caty"); caty.setAge(2); System.out.println("Caty's age is "+caty.getAge()+" years "); caty.eat(); caty.speaking(); } }
Lengkapipada project diatas • Interface Speaker • Class Dog • Class Bird • Class Bat