• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

Regarding interfaces and abstract class in java


In Java,  interfaces provide empty methods which has to be overrided when implemented. So instead of implementing those interfaces, if we directly define those methods in our class, wont it work? Then why r interfaces used? And how is it different from abstract class. On what contexts are both used?
0
shaan432
Asked:
shaan432
  • 8
  • 5
  • 5
  • +5
4 Solutions
 
sciuriwareCommented:
No, an interface is just a promise to implement.
An interface becomes an identity that gives a class a characteristic.

An abstract class is ...................... a class!
It's a building block.

;JOOP!
0
 
sciuriwareCommented:
Very important: given an interface  Abortable  promising a method  void abort() to stop any actions.

You can have 2 completely different classes A & B implementing them.

Declare    Abortable q = .....

Later:      q.abort();  // What will stop? You don't care if it's an A or B

'q' "sees" only the ability to call 'abort()' without asking "what kind of class is this".


An abstract class has nothing to do with this.
It is a begin to build a full blown class but the extend depend on your needs.
Good example: List;   see all classes derived from it.

;JOOP!

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
ksivananthCommented:
basically it all for a concrete design! and provides a high level of abstraction.

for e.g.,

interface Connection{
  void connect(...);
  void sendData(...);
  Object recieveData();
}

class abstract AbstractConnection implements Connection{
  public Object encodeData(...){
    // encode impl
  }

  public Object decodeData(...){
    //decode impl
  }
}

class HTTPConnection implements Connection{
  // all connection impl
}

class SecureHTTPConnection extends AbstractConnection{
  // all impl
}

interface declares methods for set of classes
abstraact class provides some common functionalit for set of classes

Also here based on the user previlleage you can give permission for accessing functionalities. For e.g., if a user has previllege for only the Connection interface, he can access method declared only in the interface...
0
 
sciuriwareCommented:
ksivananth, must you interfere everywhere?
Find a question to be answered.

;JOOP!
0
 
ksivananthCommented:
I just wanted to explain it in simple terms, after all its a forum and anybody can post their ideas for the context! Ultimately the person has to understand and get the ideas, don't worry, nothing going to affect u :)
0
 
sciuriwareCommented:
Then wait for his/her response, don't bomb with info.
The person must have a chance to learn.
Apart from that, you are always welcome with new or different insights.

;JOOP!
0
 
ksivananthCommented:
I don't know what makes u think that and how he lost the chance for learning, its upto the person who reads the posting, He is going to take whatever info he needs.

BTW, it is not a place to shout at one. if u have info just post it and correct other posting if its really wrong. Dont bring unncessary rules!
0
 
sciuriwareCommented:
Most confusing to others.
0
 
ksivananthCommented:
tell me what my posting confused u!
0
 
sciuriwareCommented:
this kind of unnecessary discussions in the greed for points, #55.
0
 
ksivananthCommented:
I think, if u are not greedy, the junk wouldn't be...

Ok buddy, u take the points, I don't need a single point. If u told me that all at the first that your concern is points, I wouldn't have replied
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
 
colr__Commented:
>> must you interfere everywhere?

I know you didnt ask for it, but in my opinion, this is outrageous bahaviour. This is a FORUM, a public post/debate site. EE is being littered recently with bitchiness between experts who are supposed to have the same goal - helping others. EE also plainly state in their question-post form that "experts compete for points" so it isnt a question of being greedy - for most experts, myself included, this is the whole point.

I think EE is the best forum I've used on the 'net, but it does have a problem with bickering between experts.

I also believe that the more people respond to a question, even if they all say the same thing, the better it is - when Im trying to elarn something I appreciate having multiple sources to read, so I can cross reference them and get different viewpoints.

And sciuriware, I really hope your response doesnt start with "yes, we didint ask your opinion..." - it's too predictable.

colr__
0
 
sciuriwareCommented:
You may be right colr__ but I am not the only one suffering from comments being repeated.
You can see constructive comments above.

There is a fine line between adding info and jacking.
In this case I was probably a bit too much irritated.

Sorry ksivananth; and thanks colr__.

;joop!
0
 
colr__Commented:
Good will to all (wo)men!!! ;-)
0
 
objectsCommented:
> You may be right colr__ but I am not the only one suffering from comments being repeated.

get used to it :)
0
 
sciuriwareCommented:
Yes objects, you could know: you have been around FOR SIX YEARS!
You must be one of the oldest members.

And I'll take your advise.

;JOOP!
0
 
shaan432Author Commented:
pls dont fight eachother here. Let me think abt the info u have given. Im having a real busy time.I'll be back soon with the points for u. Thank u.
0
 
WebstormCommented:
I suggest a split : sciuriware {http:#16877083}, Webstorm {http:#16877124}, ksivananth {http:#16878605}
0
 
WebstormCommented:
... and mayankeagle {http:#16880189}
0
 
girionisCommented:
thanks, this is very helpful :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 8
  • 5
  • 5
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now