How to Use Extends and Implements
Java example code, how to use extends and implements in class
package myjavaexample; public class MyJavaExample { private interface DoSomething { public void sound(); } private class Animal { public String voice="Animal"; public Animal() { } public Animal(String voice) { this.voice=voice; } } private class Pet extends Animal implements DoSomething { public Pet() { } public Pet(String voice) { super(voice); } public void sound() { System.out.println(this.voice); } } public void experiment() { Pet dog=new Pet(); dog.sound(); Pet cat=new Pet("Miauww"); cat.sound(); Pet bird=new Pet("Cici"); bird.sound(); } public static void main(String[] args) { MyJavaExample main=new MyJavaExample(); main.experiment(); } }



Comments
Post a Comment