Solved

Regarding interfaces and abstract class in java

Posted on 2006-06-10
27
283 Views
Last Modified: 2010-03-31

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
Comment
Question by:shaan432
  • 8
  • 5
  • 5
  • +5
27 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 16877073
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
 
LVL 24

Accepted Solution

by:
sciuriware earned 32 total points
ID: 16877083
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
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 31 total points
ID: 16877124
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16877137
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16877142
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 31 total points
ID: 16878605
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 16879743
ksivananth, must you interfere everywhere?
Find a question to be answered.

;JOOP!
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16879914
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 16879927
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 16880003
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 16880017
Most confusing to others.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16880047
tell me what my posting confused u!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 24

Expert Comment

by:sciuriware
ID: 16880140
this kind of unnecessary discussions in the greed for points, #55.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16880161
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
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 31 total points
ID: 16880189
0
 
LVL 8

Expert Comment

by:colr__
ID: 16883726
>> 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
 
LVL 24

Expert Comment

by:sciuriware
ID: 16883876
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
 
LVL 8

Expert Comment

by:colr__
ID: 16883917
Good will to all (wo)men!!! ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 16883979
> You may be right colr__ but I am not the only one suffering from comments being repeated.

get used to it :)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16883990
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
 

Author Comment

by:shaan432
ID: 16930327
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
 
LVL 13

Expert Comment

by:Webstorm
ID: 17120656
I suggest a split : sciuriware {http:#16877083}, Webstorm {http:#16877124}, ksivananth {http:#16878605}
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 17120663
... and mayankeagle {http:#16880189}
0
 
LVL 35

Expert Comment

by:girionis
ID: 17120665
thanks, this is very helpful :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now