The fifth question and basic knowledge of java object-oriented interface after class

Function of interface

Represents a kind of ability, mainly to solve the limitation of single inheritance in java

Declaration of interface

Interface is used to be inherited and implemented, so it is generally recommended to use public or default modifier;
You cannot use private and protected to decorate interfaces. The interface keyword is used to declare an interface.
//Declaration of interface
interface Animal {  
  //Static constant member    
  public static final int AGE = 5;   
    //Abstract method member    
    public void eat();    
    public void travel(); 
    }

Implementation of interface

When a class implements an interface, it implements all the methods in the interface. Otherwise, the class must be declared as an abstract class. Class implements the interface using the implements keyword. In the class declaration, the implements keyword is placed after the class declaration. To implement the syntax of an interface, you can use this formula:
… implements interface name [, other interfaces, other interfaces

Features of the interface title

A: Objects cannot be instantiated but can be transformed up / down
B: Interface, so some properties are static constants, which must be assigned when defining
C: All methods in the interface are abstract methods, which can be omitted
D: There is only one public permission for properties and methods in the interface
E: After the class implements the interface, all abstract methods in the interface must be forced to be rewritten unless the class is abstract
F: A class can only inherit from one parent but can implement multiple interfaces at the same time
G: Multiple inheritance can be inherited between interfaces
H: No constructors can be defined in an interface

Exercises after class

 //Define Eatable interface 
public interface Eatable { 
//having dinner 
public void eat();
 } 
//Define the Swimmable interface 
public interface Swimmable { 
//Swimming 
public void Swim(); }
 //Define FlyingDiscCatchable interface 
 public interface FlyingDiscCatchable {
  //Connect to Frisbee 
  public void catchingFlyDisc(); }
   //Define the Pet class 
   public class Pet { public String name;
    public int health;
     public int love;
      public Pet(){}
       public Pet(String name, int health, int love) { 
       super();
        this.name = name; 
        this.health = health; 
        this.love = love; 
        } 
        public String getName() { 
        return name; 
        } 
        public void setName(String name) {
         this.name = name;
          } 
          public int getHealth() {
           return health;
            }
             public void setHealth(int health) {
              this.health = health; 
              } 
              public int getLove() { return love;
               } 
               public void setLove(int love) { 
               this.love = love; 
               } 
               public void print(){  
               System.out.println("Pet name"+this.getName()+"Health value"+this.getHealth()+"Intimacy"+this.getLove());
                } 
                } 
                //Define penguins‘ 
                public class Penguin extends Pet implements Eatable,Swimmable{
                 public String name; 
                 public int health;
                  public int love; 
                  public Penguin(){};
                   public String getName() { 
                   return name;
                    } 
                    public void setName(String name) {
                     this.name = name;
                      } 
                      public int getHealth() { 
                      return health; 
                      } 
                      public void setHealth(int health) {
                       this.health = health; 
                       } 
                       public int getLove() { 
                       return love;
                        } 
                        public void setLove(int love) { 
                        this.love = love; 
                        } 
                        public Penguin(String name, int health, int love) {
                         super(); 
                         this.name = name; 
                         this.health = health;
                          this.love = love; 
                          } 
                          @Override public void Swim() {
                             System.out.println("Penguin swimming");
                           } 
                           @Override public void eat() {
                             System.out.println("Penguins are eating"); } } 
                             //Defining dogs
public class Dog1 extends Pet implements Eatable,FlyingDiscCatchable{                          
                    public int love;
                    public int health;
                    public String getName() {
                    return name;
 }
 public void setName (String name){
 this.name=name;
 }
 public int getLove(){
 return love;
 }
 public void setLove(int love){
 this.love=love;
 }
 public int getHealth(){
 return health;
 }
 public void setHealth(int health){
 this.health=health;
 }
 public Dog1(){};
 public Dog1(String name,int love,int health){
 super();
 this.name=name;
 this.love=love;
 this.health=health;
 }
 @Override
 public void catchingFlyDisc(){
 System.out.println("The dog is picking up the frisbee")
 }
 @Override
 public void eat(){
 System.out.println("The dog is eating");
 }
}

//Define main function
public static void main(String[] args) {
 Pet go=new Dog1("Dog",100,100);
  go.print();
  Pet qq=new Penguin("penguin",100,100);
  qq.print();
  Dog1 go1=new Dog1();
  go1.eat();
  go1.catchingFlyDisc();
  Penguin qq1=new Penguin();
  qq1.Swim();
  qq1.eat();

Tags: Java

Posted on Sun, 14 Jun 2020 01:30:01 -0400 by daxxy