• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

interface declaration..

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is this a valid interface declaration...??
 
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The methods implemented should be public in the class.
 
Swapnil Sonawane
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and interface name is spelled incorrectly too.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interface declaration is valid, but when you implement an Interface in a class, this class should either provide an implementation for the methods in interface Or should be declared abstract.

Example if you impelement methods:



Example if you declare the class as abstract:
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just one minor addition to what jaspreet wrote, if your implementing class is an Abstract class you do NOT have to implement any of the contract methods from the interface (you may implement them and you may define the as abstract, but you do not HAVE to implement them).

The first concrete class MUST implement any unimplemented method from the interface AND any abstract method from the abstract class.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

The interface methods are by default (and must only be )'public' and 'abstract'. The member variables are constants.

For example:

// This is what you code
interface Archnid{
String SOME_CONSTANT= "CONSTVALUE";

void bite();
void spinWeb(String s);
}

// This is what compiler make changes or adds to the above code.
// Note that the compiler added 'public' and 'abstract' access and non-access modifiers for the methods.
// Note also that the 'public', 'static' and 'final' access and non-access modifiers for the member variables

interface Archnid{
public static final String SOME_CONSTANT = "CONSTVALUE";
public abstract void bite();
public abstract void spinWeb(String s);

}

// This is when you implement the above interface
// Here you must specify the method signature as public, if not compiler error occurs!

class ArchnidImpl implements Archnid{
public void bite(){
// your implementation code goes here...
}

public void spinWeb(){
// your implementation code goes here...
}

}

Bottom line is, the interface methods are by default(and must only be) 'public' and 'abstract'. Even if you specify or not, compiler adds it for you('public' and 'abstract' modifiers, as the example above)!
Interface can have only constants are member variables! Otherwise, compiler error occurs!

But when you implement an interface, you must specify the signature exactly. For example, void bite(){} is not the same as public void bite(){}. While implementing an interface, compiler will not add the necessary modifiers for you!

Hope it helps.

Kind Regards
Kris
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not correct .

1. You must provide body part of method unless class is abstract .
2. Access modifier of method must be public .

 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic