• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Interface...

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

interface ii
{
public void main(String[] a);
}

Is it a valid interface or not?How can we implement this method?
But the exam shows it is valid.Can anybody clarify me?Thanks.
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you mean by "how can the method be implemented" - it's a method like any other one. Here's an example class that implements the interface:
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the main method were declared as static, then it couldn't be in an interface.

If this main method were an entry-point, it would be declared as:

public static void main(String[] a) {
}

But the interface declaration that you've given is legal because it is not an entry-point and is not declared as being static.

Kaydell
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

An interface with the declaration of a method like

is perfectly fine from the technical point of view, thus the question has to be answered with yes.

I think this question tries to be misleading concerning the following issues:
If a class implements the given interface, then it must also implement the declared main()-method:
  • But according to the rules for overwriting it is not allowed to add the static modifier.
  • According to the rules for overloading it is not allowed to implement the same method but with a static modifier.


  • This means, a class that implements correctly the interface provides a main()-method with a behavior like any other
    method, too.
    But the important consequence of implementing this interface is, that the class cannot act as the entry point of an application, since it is not possible to implement a correct starting method, i.e.
    for an application.

    Kind regards,
    Robert

    [ July 21, 2007: Message edited by: Robert Horvath ]
    [ July 21, 2007: Message edited by: Robert Horvath ]
     
    kathir vel
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi ,
    Thanks for your replies.If I am using both static main() and implemented interface main method in my program, it is showing (main method is already in the pgm)compile error.
    So we cann't declare
    public void main(String[] args); in an interface.AM I Right?Please can you clatify me,if I am wrong.Thanks.
     
    Ulf Dittmer
    Rancher
    Posts: 43028
    76
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by kathir vel:
    So we cann't declare
    public void main(String[] args); in an interface.



    Not in a class that you intend to run using the "java" tool.

    "static" is not part of the method signature, thus the two methods signature's would be identical, which is not allowed.
     
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hello kathir,

    look at the signature of the method there is static in the method what the contract defines so it is perfectly legal even to implement it.
    thanks
     
    Ranch Hand
    Posts: 100
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Kathir,

    have a look into this code. this main can be implemented as follows.
    if you are finding any doubts/mistakes Please let me know.



    Thanks
     
    Robert Horvath
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Kathir!

    Within an interface
  • you CAN declare the method public void main(String[] args);

  • Note: void main(String[] args) or public abstract void main(String[] args) is equal to this.
  • you CANNOT declare public static void main(String[] args); Note: the static modifier would not make sense for a method in an interface and is therefore not allowed.


  • Within a class
  • you CAN implement the previously created interface and implement the main()-method (see madhu's example from above)


  • Concerning your compiler error:
    you CANNOT implemented the interface (and the main()-method) in a class AND overload the main()-method with a method that adds only the static-modifier, i.e. public static void main(String[] args); since this is AGAINST a rule of overloading!

    This means, that you can
  • either implement the interface and use the main()-method as any other method declared in an interface (but not as an entry-method for an application!)
  • or you can implement a valid entry-method for an application in a class but not implement the interface at the same time.


  • Robert
     
    look! it's a bird! it's a plane! It's .... a teeny tiny ad
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic