Page 1 of 1

Error in Sample questions from Oracle

Posted: Thu Jun 06, 2013 12:14 pm
by deepa.patre
Here is the code

Code: Select all

interface Rideable {
	String getGait();
	}
	public class Camel implements Rideable {
	int weight = 2;
	
	public static void main(String[] args) {
	new Camel().go(8);
	}
    void go(int speed) {
	++speed;
	weight++;
	int walkrate = speed * weight;
	System.out.print(walkrate + getGait());
	}
	 String getGait() {
	return " mph, lope";
	}
	}
When i execute this in eclipse it shows the error as - "Cannot reduce the visibility of the inherited method from Rideable" So i made getGait() in Camel as public...
1. But my question is: I haven't reduced the visibility its the same as inherited method then why is the error?
After making the method public when i try to execute it shows the following message - " Exception in thread "main" java.lang.NoSuchMethodError: main"
What is the problem with the code?
How do i make it work?

Please advise,
Regards,
Deepa.

Re: Error in Sample questions from Oracle

Posted: Fri Jun 07, 2013 7:49 pm
by admin
1. all methods of an interface are public even if you don't write public keyword in front of the methods. So that is why if you don't make the method public in the class, it fails to compile.

2. Not sure what and how exactly are you running the code but there is no problem with the main method that you've shown here.

HTH,
Paul.

Re: Error in Sample questions from Oracle

Posted: Mon Jun 10, 2013 3:26 pm
by deepa.patre
Thanks! The program is executing...