Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Clarification with interfaces.
06-25-2008, 08:27 PM
Post: #1
Question Clarification with interfaces.
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?
Find all posts by this user
Quote this message in a reply
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.
Visit this user's website Find all posts by this user
Quote this message in a reply
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?
Find all posts by this user
Quote this message in a reply
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]
Visit this user's website Find all posts by this user
Quote this message in a reply
06-25-2008, 09:32 PM
Post: #5
RE: Clarification with interfaces.
Ok now I got it..Thanks a lot.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: