• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

errors while using interface

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
Ranch Hand
Posts: 62
Netbeans IDE MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Matthew sir. The code is working fine now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic