This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

errors while using interface

 
Dushyant Shukla
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code doesn't compiles as expected.Keeps giving the errors like
display() in volksWagon cannot implement display() in fourWheeler
attempting to assign weaker access privileges;was public.............................four other similar errors!



please help rectifying the code.....
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't explicitly tell the compiler what privileges are required to access a method the compiler will assign default (more properly known as package-private). If your interface mandates that the method is public then you are restricting access because default allows more limited access than public see here for more info on access modifiers

Try to set you method void display() to public void display() and see if that helps.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dushyant. Welcome to the Ranch!

In addition to what Phil said, interface methods are implicitly public (because interfaces are intended to define a public interface). So all the methods you've defined in fourWheeler are public. And therefore they need to be public in the class that implements them.

By the way, I'd recommend getting used to following Java conventions if you're going to program in Java. And one of those conventions is that interfaces and classes should start with a capital. It's a good idea to get into good habits early on.
 
Dushyant Shukla
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Phil sir and Matthew sir for your support and Matthew sir I will keep your suggestion in my mind....
The code is working fine after the changes besides the line 72 (pa=a.nextBoolean()).whenever i enter a yes/no or 1/0 it says Exception in thread "main" java.util.InputMismatchException....
what can be the possible errors...
thanks once again!
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.

Dushyant Shukla wrote:The code is working fine after the changes besides the line 72 (pa=a.nextBoolean()).whenever i enter a yes/no or 1/0 it says Exception in thread "main" java.util.InputMismatchException....
what can be the possible errors...


Have you tried entering true/false?

When you're using a , if you want to be able to cope with unexpected input you sometimes need to use the "has..." methods. They tell you if the following input can be parsed as a particular type. So if you're about to use nextBoolean(), you can check hasNextBoolean() first. If that returns false, it means you're going to get an exception when you call nextBoolean().

And if you look at the Javadocs for java.util.Scanner#hasNextBoolean(), you'll see it says:

Returns true if the next token in this scanner's input can be interpreted as a boolean value using a case insensitive pattern created from the string "true|false".


If you want to be able to enter yes/no, you need to read the input as a String and compare it against String values.
 
Dushyant Shukla
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Matthew sir. The code is working fine now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic