• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in Accessing method of interface  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As we know the to access methods of an interface
1. First we need to implement the interface.
2. Override the method of interface
3. create the obj of implementing class in the ref variable of that interface
4. call the method of that ref var.




I have saved the program by name TestCalc.java and compiling the with the same name
But the compilation fails giving lots of errors may guide for the same
 
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that in the second method of the interface, the return type is not void, so you have to return a value compatible with int.

Also I think you have a simple misspelling in your main method. You are trying to create an instance of MyCalculator.
 
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some minor mistakes in your code

[MODERATOR ACTION: Deleted code.]

I would suggest you use some IDE that will help you along the coding. The IDE will help you resolve any such minor issues.
 
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you use some IDE that will help you along the coding. The IDE will help you resolve any such minor issues.


I would rather that he sticks to the command prompt for now to get a feel of how things work under the hood.
 
Jdinesh Tiwari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


D:\JAVAPROG>javac TestCalc.java
TestCalc.java:6: class, interface, or enum expected
MyCalc implements calc{ //Step 1
^
TestCalc.java:7: class, interface, or enum expected
public void add(int x, int y){ // Step 2
^
TestCalc.java:9: class, interface, or enum expected
System.out.println("x+y ="+ sum);
^
TestCalc.java:10: class, interface, or enum expected
}
^
TestCalc.java:13: class, interface, or enum expected
public int mul(int x, int y){//Step 2
^
TestCalc.java:15: class, interface, or enum expected
System.out.println("x*y ="+pro);
^
TestCalc.java:16: class, interface, or enum expected
return pro; //Given return statement
^
TestCalc.java:17: class, interface, or enum expected
}
^
8 errors

may help to resolve the same
 
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jdinesh Tiwari wrote:D:\JAVAPROG>javac TestCalc.java
TestCalc.java:6: class, interface, or enum expected
MyCalc implements calc{ //Step 1
^


The error is quite self explanatory, isn't it?
HINT: Read the line 6 of your code again, pay attention to the caret symbol in the error to understand what is wrong.
 
Jdinesh Tiwari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

D:\JAVAPROG>javac TestCalc.java
TestCalc.java:15: class, interface, or enum expected
public int mul(int x, int y){//Step 2
^
TestCalc.java:17: class, interface, or enum expected
System.out.println("x*y ="+pro);
^
TestCalc.java:18: class, interface, or enum expected
return pro; //Given return statement
^
TestCalc.java:19: class, interface, or enum expected
}
^
4 errors

Thanks all now what is the problem i am facing still 4 errors
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you would properly and consistently indent your code, the problem would be self-evident.
 
Jdinesh Tiwari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your hints. Now its working fine
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jdinesh Tiwari wrote:Thanks all now what is the problem i am facing still 4 errors

So, why not go through the same process you did to solve the first 4 errors?

Amit's quite right: the clue to your problems is in the errors themselves, so read the messages.

Honestly, you'll be much more satisfied if you work it out for yourself, unless you simply want somebody to do the work for you - in which case, you've come to the wrong site.

Winston
 
Jdinesh Tiwari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Wintson you are right with your honest suggestions . But i think i am here at the right site. To be very Honest some people are very good for suggestions only
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jdinesh Tiwari wrote:Dear Wintson you are right with your honest suggestions . But i think i am here at the right site. To be very Honest some people are very good for suggestions only

Not quite sure what you meant by that; but I hope it's good, and you'll follow my advice.

Best of luck.

Winston
 
Jdinesh Tiwari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Winston I will follow your suggestions i am not good at programming but i want to learn the same. All your hits and guidance help me lot. And its a good site where people like you guide us on the basic and tuff part of java.
 
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Jdignesh,

The best part of this site is that you will find very intelligent yet very down to earth folks who strongly believe in the philosophy of negating spoon feeding. Because it serves as a last nail hammered into the coffin of a budding greenhorn (read programmer's) like us.

I will give you a hint to resolve this because ultimately you have to figure it out as suggested by Winston, Amit and others too..
The hint is carefully look at the class MyCalc structure, the error messages indicate that there seem to be some problem with the curly braces in this class.
And for future programming tip, please write fully qualified names for the interface, methods,classes,variable etc. This will help you the most in realizing good program design practices
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!