|
Clarification with interfaces.
|
|
06-25-2008, 08:27 PM
Post: #1
|
|||
|
|||
|
Hi,
Suppose i have a prog like this: interface Content { int i=9; void publish(); } class Article implements Content { void publish(){ /*Some code*/ } } class Blogs implements Content { void publish() { /* Some code*/ } . . . . . . . now if there are n number of classes which implement the interface Content and among these classes i want the implementation of publish() method only in n/2 number of classes then it is not possible to declare all the rest of the n/2 classes as abstract as the number of classes is huge but at the same time i want to make use of 'i' also...what is the solution for this? |
|||
|
06-25-2008, 09:02 PM
Post: #2
|
|||
|
|||
|
RE: Clarification with interfaces.
I think the solution to your problem is to create a class which implements Content and give an empty implementation for publish method. Then extend all your classes from this base class. Now in n/2 number of classes implement the publish method as required.
|
|||
|
06-25-2008, 09:22 PM
Post: #3
|
|||
|
|||
|
RE: Clarification with interfaces.
Could u please elaborate a little further if possible with an example, Albin?
|
|||
|
06-25-2008, 09:27 PM
Post: #4
|
|||
|
|||
|
RE: Clarification with interfaces.
What I mean is something like this.
[java] interface Content { int i=9; public void publish(); } class ContentImpl implements Content { public void publish() { // Do nothing here. } } class Article extends ContentImpl { public void publish(){ /* Some code */ } } class Blogs extends ContentImpl { public void publish() { /* Some code */ } } class SomeOtherContent extends ContentImpl { } [/java] |
|||
|
06-25-2008, 09:32 PM
Post: #5
|
|||
|
|||
|
RE: Clarification with interfaces.
Ok now I got it..Thanks a lot.
|
|||
|
« Next Oldest | Next Newest »
|

Search
Member List
Calendar
Help




