public interface ExampleInterface {public void doAction();public String doThis(int number);}
public class sub implements ExampleInterface {public void doAction() {//specify what must happen}
public String doThis(int number) {//specfiy what must happen}}
现在扩展一个类
public class SuperClass {public int getNb() {//specify what must happenreturn 1;}
public int getNb2() {//specify what must happenreturn 2;}}
public class SubClass extends SuperClass {//you can override the implementation@Overridepublic int getNb2() {return 3;}}
在这种情况下
Subclass s = new SubClass();s.getNb(); //returns 1s.getNb2(); //returns 3
SuperClass sup = new SuperClass();sup.getNb(); //returns 1sup.getNb2(); //returns 2
public class ExtendsAndImplementsDemo{public static void main(String args[]){
Dog dog = new Dog("Tiger",16);Cat cat = new Cat("July",20);
System.out.println("Dog:"+dog);System.out.println("Cat:"+cat);
dog.remember();dog.protectOwner();Learn dl = dog;dl.learn();
cat.remember();cat.protectOwner();
Climb c = cat;c.climb();
Man man = new Man("Ravindra",40);System.out.println(man);
Climb cm = man;cm.climb();Think t = man;t.think();Learn l = man;l.learn();Apply a = man;a.apply();
}}
abstract class Animal{String name;int lifeExpentency;public Animal(String name,int lifeExpentency ){this.name = name;this.lifeExpentency=lifeExpentency;}public void remember(){System.out.println("Define your own remember");}public void protectOwner(){System.out.println("Define your own protectOwner");}
public String toString(){return this.getClass().getSimpleName()+":"+name+":"+lifeExpentency;}}class Dog extends Animal implements Learn{
public Dog(String name,int age){super(name,age);}public void remember(){System.out.println(this.getClass().getSimpleName()+" can remember for 5 minutes");}public void protectOwner(){System.out.println(this.getClass().getSimpleName()+ " will protect owner");}public void learn(){System.out.println(this.getClass().getSimpleName()+ " can learn:");}}class Cat extends Animal implements Climb {public Cat(String name,int age){super(name,age);}public void remember(){System.out.println(this.getClass().getSimpleName() + " can remember for 16 hours");}public void protectOwner(){System.out.println(this.getClass().getSimpleName()+ " won't protect owner");}public void climb(){System.out.println(this.getClass().getSimpleName()+ " can climb");}}interface Climb{public void climb();}interface Think {public void think();}
interface Learn {public void learn();}interface Apply{public void apply();}
class Man implements Think,Learn,Apply,Climb{String name;int age;
public Man(String name,int age){this.name = name;this.age = age;}public void think(){System.out.println("I can think:"+this.getClass().getSimpleName());}public void learn(){System.out.println("I can learn:"+this.getClass().getSimpleName());}public void apply(){System.out.println("I can apply:"+this.getClass().getSimpleName());}public void climb(){System.out.println("I can climb:"+this.getClass().getSimpleName());}public String toString(){return "Man :"+name+":Age:"+age;}}
输出:
Dog:Dog:Tiger:16Cat:Cat:July:20Dog can remember for 5 minutesDog will protect ownerDog can learn:Cat can remember for 16 hoursCat won't protect ownerCat can climbMan :Ravindra:Age:40I can climb:ManI can think:ManI can learn:ManI can apply:Man
class AnimalClass {
public void doEat() {
System.out.println("Animal Eating...");}
public void sleep() {
System.out.println("Animal Sleeping...");}
}
public class Dog extends AnimalClass implements AnimalInterface, Herbi{
public static void main(String[] args) {
AnimalInterface a = new Dog();Dog obj = new Dog();obj.doEat();a.eating();
obj.eating();obj.herbiEating();}
public void doEat() {System.out.println("Dog eating...");}
@Overridepublic void eating() {
System.out.println("Eating through an interface...");// TODO Auto-generated method stub
}
@Overridepublic void herbiEating() {
System.out.println("Herbi eating through an interface...");// TODO Auto-generated method stub
}
}
定义的接口:
public interface AnimalInterface {
public void eating();
}
interface Herbi {
public void herbiEating();
}