The Artima Developer Community
Sponsored Link

Java Answers Forum
INTERFACE vs ABSTRACT

4 replies on 1 page. Most recent reply: Oct 22, 2003 5:01 AM by perry arora

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Constant

Posts: 2
Nickname: cons
Registered: Oct, 2003

INTERFACE vs ABSTRACT Posted: Oct 20, 2003 1:25 AM
Reply to this message Reply
Advertisement
kind of new to this language... can any one kindly tell me the difference between the 2 classes???

when do we use which....

what are the advantages or disadvantages of between them???


Jonathon Brozny

Posts: 24
Nickname: jonathon
Registered: Oct, 2003

Re: INTERFACE vs ABSTRACT Posted: Oct 20, 2003 1:13 PM
Reply to this message Reply
An Interface is a class that has methods but no method definitions

An Abstract class has some methods without definitions and some methods with definitions;
interface Something {
void someMethod(string someValue);
string somethingElse();
}
 
abstract class Something {
void someMethod(string someValue);
string somethingElse(){
    return someValue;
  }
}


If possible I use an interface unless I have to use an abstract class.

zenykx

Posts: 69
Nickname: zenykx
Registered: May, 2003

Re: INTERFACE vs ABSTRACT Posted: Oct 20, 2003 7:19 PM
Reply to this message Reply
I think that you can find the kind of response for your question in the basics of OOP.
It's not something particular to java. I suggest you make a search on net on "abstract vs interface" and you'll get plenty answers.

Lars Marius Garshol

Posts: 6
Nickname: larsga
Registered: Oct, 2003

Re: INTERFACE vs ABSTRACT Posted: Oct 21, 2003 6:33 AM
Reply to this message Reply
The main difference is that when implementing an interface you have much more freedom than when implementing an abstract class. You don't inherit any member variables or pre-existing methods, and you can inherit from any other class you want and implement as many other interfaces as you want. With an abstract class you're pretty constrained: you can't inherit from any other class.

Generally, if you want to define a set of methods that can be implemented in many different ways: use an interface. If you want to create a utility superclass that has useful functionality that can be inherited: use an abstract class (but think twice first; it's usually the wrong thing).

Morale: if in doubt, use an interface.

perry arora

Posts: 22
Nickname: perry
Registered: Sep, 2003

Re: INTERFACE vs ABSTRACT Posted: Oct 22, 2003 5:01 AM
Reply to this message Reply
hi
Right said friend

The very basic differnce or the point of distinction is that what do you want to use !!

If u want to inherit any Basic structure for your object you shoud use the abstract class reason being when you inherit the class you are forced to follow a structure for using it.

ok... check the following DATA .. its need to scan it!





INTERFACE VS ABSTRACT CLASSES


(1) THE IMPLEMENTOR/SUB CLASS SHARE A COMMON STATE&BEHAVIOUR OR THEY SHARE METHODS SIGNATURE

(3) INTERFACE CAN NOT PROVIDE ANY DEFAULT CODE

(4**) CLASSES CAN DEFIN THE CORE IDENTITY OF THEIR DECENDANTS , BUT THE INTERFACE DESCRIBE THE PERIPHERAL IDENTITY E.D AUTOMOBILE CLASS CAN USE THE RECYLE INTERFACE WHICH COULD BE APPLIED TO MANY UNRELATED CLASSES

**(5) INTERFACE ARE USED FOR THE CONCEPT OF DELEGATION WHEREAS THE ABSTRACT CLASSES DEFINE WHAT WE CALL INHERITENCE

(A)for e.g the best e.g is when we use the Runnable interface not extend the thread class.. when we use the runnable interface we do not create a separated thread infact . we want some code fratgement that can run independently.. or in other words we coud delegate the execution line to it...

but when we use the thread class we create a complete new threas that coud also be given some inherit behaviour and state.

(B) the second best e.g is when we use the javax.sql.DataSource :: in this case we obtain the connection using this interface or in other words our code delegates this job to the data source implementation of the vendor .. whose data base we are using...





(6) THE ABSTRACT CLASS FORCESS AN STRUCTURE


when we extend that class with that we inherit the behaviour and t some state of the class also that can be good or bad ,, that can allow a limited or a free access ... in a way it imposes a structure..

but when ever we impmenet the interface it provides nothing more than a set of constants.. we have to take tools from other classes.. and we are free at the internal design..........

(7) INTERFACES ARE SLOW AND REQUIRE AN EXTRA INDIRECTION


(2) THIRD PARTY CLASS CAN ONLY EXTEND ONE SINGLE CLASS BUT CAN IMPLEMENT MULTIPLE INTERFACES



(8) INTERFACE ONLY PROVIDE THE public static constants CLASSES PROVIDE BOTH THE INSTANCE AND THE STATIC CONSTANTS - and also one can write an intialization code for intializing the constants .. which is not possible in the interfaces

(9) CHANGES ARE DIFFICULT TO REFLECT IN INTERFACES... WE CAN ATLEAST PROVIDE AN INTIAL CODE FOR THE ADDED METHOD IN THE CLASS

if we add a new method to the interface we will have to track all the implementation of the interface to include that change

but if the same is the case with the classes in that case we can atleast provide an basic default implementation that all the descendants can use/....


Hope it will help U

bye

Flat View: This topic has 4 replies on 1 page
Topic: Dos Prompt Previous Topic   Next Topic Topic: JList problem HELP!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use