• 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

Java Interfaces

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I hope that every knows,In java interfaces by Default,
1)All methods are public and abstract.
2)Variables are public,static and final.

Why is it like that ? any specific reason ?

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Absctraction. An interface is a model to a certain behavior; furthermore, they are an alternative for multiple inheritance in Java.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me elaborate on this:

methods are public because interafaces are ment to be the face of your application to outside world. And abstract because we dont need any implementation here.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason all interface methods are empty is to avoid collisions where two interfaces contain the same method, but with different implementations.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add,

Interfaces are meant for standardization of a concept followed by classes in a particular scenario. Hence an implementing class itself must define all the methods of the interface calling for interface methods to be abstract. They are public to be acessed by outside world. The variables are static and final again for standardization of a concept

Regards
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sai Prakash wrote:Hi,

I hope that every knows,In java interfaces by Default,
1)All methods are public and abstract.
2)Variables are public,static and final.

Why is it like that ? any specific reason ?



if you dont want all methods to be abstract, use an abstract class instead of an interface,

By the way, using an interface marks a class of having the same characteristic as that of the interface,
Hence its essential for the methods to be public,
Eg:


Can you create an Object of an interface ???
If not, then what is the sense of Not keeping variables final ?

 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, is the code block not workin ?
sorry , my bad, the

Disable BB code in this message

was checked.
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic