简体   繁体   中英

Interfaces and methods in Java?

So, I'm having a bit of an issue trying to decipher an assignment, it is worded as follows:

"Create a interface file Animalize.java under c:\\myjava folder. Declare a method myLanguage() that takes no argument but returns a String type. Declare one more method of your choice. (ii) Create three different classes Dog.java Cat.java and Lion.java and let those implement the Animailize interface. This means each of this class need to have the methods declared in the Animalize interface. (iii) Create a class AnimalTest.java and inside that class's main method instantiate the three other classes created and call the methods for each.

Then save and compile by giving the javac Animalize.java Cat.java Dog.java Lion.java AnimalTest.java command on command prompt. Run using command java AnimalTest Make sure no errors and fix if any. Write the answer to (A) in the same file AnimalTest.java at top inside java comments.

Hint: The Animalize interface would have method declaration like,

public String myLanguage();

A Cat class would implement Animalize by first having a line in class definition

public class Cat implements Animalize

and then having a method

public String myLanguage()
{
            System.out.println(“Meow”);
}

Add additional method corresponding to your other method in Animalize interface."

I created all of the files specified above, with the following code:

:

public interface Animalize {
  public static void main (String args[]){
     public static String myLanguage();
     }
     }

:

      public class Cat implements Animalize
{
}

:

        public class Dog
{
}

:

        public class Lion
{
}

:

      class AnimalTest{

public class Dog implements Animalize{
public String myLanguage(){
return
System.out.println("Woof");
}
}

}

When I compile all of the classes as specified above, I receive the following errors in command line:

  

I've messed around with these quite a bit, and can't seem to figure out the issue, or even if I'm doing the assignment quite right. Aside from the errors, is there anything in any of the files that looks just absolutely wrong? I don't even know where to begin. Thanks to anyone who can help me!!

Interface cant have a main method. Please read up

http://download.oracle.com/javase/tutorial/java/IandI/createinterface.html

You have a variable inside your main() method that you've declared "public static". Local variables (those inside methods) can't be declared public or static.

There is a bunch of problems in your code and I can't do your homework. First @Adithya Surampudi is right, an interface cannot have a main method. Second, the method myLanguage will not work:

public String myLanguage() {
    return System.out.println("Woof");
    // instead do something like this
    return "Woof";
}

Third, you did not follow requirement #3. Your main method shall be defined in AnimalizeTest and not in Animalize . Just some hints...

Ok so to save you some pain as you continue writing code. An Interface provides a contract through which implementors must abide. In Java the keyword Interface is a set of methods and their signatures with no implementation. This is different from an abstract class which may provide implementation. The method signature is known as the contract, return type plus arguments.

You cannot have main in an interface as others have stated. When you have a contract to abide by System.out is not how you get back the information that would be for a String. What it does is puts a block of characters out that happen to resemble a String.

  • static modifier is not allowed in interface. It can be abstract or public.

     public static void main (String a[]) // wrong public void main (String a[]) // correct 
  • Only method signatures are allowed in interface.

Correct implementation can be like this.

       public interface Animalize {
                                    public String myLanguage();
                                  }

         public class Cat implements Animalize
         {
             public String myLanguage()
             {

             }
         }

         public class Dog implements Animalize
         {
             public String myLanguage()
             {

             }
         } 

         public class Lion implements Animalize
         {
             public String myLanguage()
             {

             }
         } 


         public class AnimalTest
         {
             public static void main (String a[])
             {
               Cat cat=new Cat();
               cat.myLanguage();
               Dog dog=new Dog();
               dog.myLanguage();

             }
         } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM