Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Simple interface example giving erors

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple interface example giving erors

    interface Test
    {
     
           final static int a,b;
     
     
           int compute(int a,int b);
     
     
     
    }
     
     
    class Sum implements Test
    {
     
    public int compute()
     
          {
      System.out.println("Inside subclass");
     
     
     
     
     public int compute(int x, int y)
     
          {
      System.out.println("Inside subclass");
    	return(x*y);
          }
     
     
     }
     
     
     
     
    class Interfacedemo 
    {
     
     public static void main(String args[])
      {
     
          System.out.println("Interface Example . . .");
     
     
     
          Test t;
     
          Sum s;
     
          t = s;
     
     
      System.out.println("Interface Example . . ." + t.compute(10,20));
     
     
       }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Simple interface example giving erors

    You forgot to post the full text of the error message.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple interface example giving erors

    C:\Users\Pushpendra\Desktop\Project\Interface>java c Interfacedemo.java
    Interfacedemo.java:4: = expected
    final static int a,b;
    ^
    Interfacedemo.java:4: = expected
    final static int a,b;
    ^
    Interfacedemo.java:25: illegal start of expression
    public int compute(int x, int y)
    ^
    3 errors

    C:\Users\Pushpendra\Desktop\Project\Interface>

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Simple interface example giving erors

    Where did you get this example from?
    final static int a,b;
    Why do you have this statement in the interface?

    Interfacedemo.java:25: illegal start of expression
    The compiler is confused by what looks like the start of a new method within another method. You can not nest one method within another. Finish one with its ending } before starting another

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple interface example giving erors

    This is how i wrote again the code . . . but it got failed to execute correctly.

    interface Test
    {
     
          final  int a;
          final   int b;
     
           int compute(int a,int b);
     
     
     
    }
     
     
    class Sum implements Test
    {
     
     
     
       int compute(int x, int y)
     
          {
              System.out.println("Inside subclass");
     
    	return(x*y);
          }
     
     
     }
     
     
     
     
    class Interfacedemo 
    {
     
     public static void main(String args[])
      {
     
          System.out.println("Interface Example . . .");
     
     
     
          Test t;
     
          Sum s;
     
          t = s;
     
     
     
      System.out.println("Interface Example . . ." + t.compute(10,20));
     
     
       }
     
     
    }
    __________________________________________________ __________________-

    ERRORS
    __________________________________________________ ______________________

    Interfacedemo.java:4: = expected
    final int a;
    ^
    Interfacedemo.java:5: = expected
    final int b;
    ^
    2 errors

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Simple interface example giving erors

    Interfacedemo.java:4: = expected
    The compiler wants the variables to be assigned a value. That is done with the = followed by a value.
    Have you read the Tutorial on how to write an interface:
    Start here: // Trail: Learning the Java Language: Table of Contents (The Java™ Tutorials)
    and find interface

Similar Threads

  1. Class hierarchy... giving me a headache, so I could use some help
    By Kerr in forum Object Oriented Programming
    Replies: 6
    Last Post: May 30th, 2011, 05:03 PM
  2. xml parsing getting all subtags without giving it's name
    By HungryCoder in forum Java SE APIs
    Replies: 1
    Last Post: May 27th, 2011, 03:27 AM
  3. [SOLVED] Giving Thread.sleep( ); a decimal value
    By Knox in forum Threads
    Replies: 2
    Last Post: April 9th, 2011, 08:55 PM
  4. JavaCompilerobject giving an error WITHOUT ide
    By divs1210 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2011, 08:49 PM
  5. SAAJ giving exception with large attachments
    By Veronica in forum Web Frameworks
    Replies: 2
    Last Post: January 4th, 2011, 04:13 AM

Tags for this Thread