Solved

Regarding interfaces and abstract class in java

Posted on 2006-06-10
27
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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:Mayank S
Mayank S 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

696 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