Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

extending Interface

 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can we prevent a Interface for being extended by a other interface?
Surely final will not work,is there any way of doing so?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


How can we prevent a Interface for being extended by a other interface?
Surely final will not work,is there any way of doing so?



First of all, interfaces are made accessible from any class hierarchy, no bondage in general. But

Yeah you can do that,
place the interface inside a class and make it private so that outside the class nobody could extend it! It would be for personal use in the class (by the innerclasses and so) to implement it or extended by another interface in the same class but looks little clumsy.

Thanks and Regards,
cmbhatt
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:

First of all, interfaces are made accessible from any class hierarchy, no bondage in general.



What do you mean by that?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andy,

Interfaces are created to to be implemented by classes, whereas class can be made final to be not extended.

I only meant, the purpose of having an interface is availed when a class implements it and avails the benefit of polymorphism too. The question was "How can be prohibit an interface to be extended by another interface".
To extend an interface means to give more burden to the concrete (implementing)class. Sometimes we may want to not allow other interface to extend it, we can make it private but inside a class. We call it nested interface.

But what we generally see, standard interfaces are made public to be implemented by any classes to avail their benefits.

Do I miss something?

Thanks and Regards,
cmbhatt
 
Andy Morris
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well what I was thinking that you were saying was that interfaces are visible by all classes (except when nested like you said), but you can obviously give an interface default access, in which case only classes inside that package can implement it, or even see it. That's useful for developing low level components in a de-coupled fashion.

That might achieve what Sanjeev was really tring to do (not quite the same), though one of the main purposes of interfaces is not to restrict implementation to a single class - I think that's worth understanding!

In anycase, this is not really anything to do with SCJP...
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic