• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

difference between abstract class and interface?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as far as i can tell, both define methods with no bodies.
only interfaces can declare variables.
only interfaces can be instansiated.
what else? i don't understand the point.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Abstract class CAN contain methods with method bodies. Interfaces CANNOT.
2. you CANNOT extend from more than one class...
but you can implement more than one interface..(kinda multiple inheritance).

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!
What about variables?
This is true: Variables in interfaces are implicitly public final static.
I THINK this is true: Variables in abstract classes have no such restrictions and can have any access modifier etc...
Correct me!
/Kaspar

/Kaspar
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An abstract class can declare variables, too. It can also provide implementations of some methods; it is abstract only because at least one method is declared but not implemented.
Interfaces do not provide implementations of any methods. Therefore it is necessary for an implementing class to define all methods in an interface (even if such implementations are merely stubs). A class extending an abstract class, on the other hand, need only implement the abstract methods of its superclass.
Implementing an interface allows a class to extend any other class. Conversely, a class which extends an abstract class cannot extend any other class.
An interface is more "general" in a sense because all implementing classes must separately define the functionality of all methods, which means that 2 different classes implementing the same interface may be very different in many ways. Classes that extend abstract classes, on the other hand, are usually quite similar and usually differ only in the specific ways in which they implement a very limited number of methods.
 
Rex Rock
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More.....
When you implement an interface..the method should have 'public' access in the class you implement even if the method signature in the interface is not mentioned as public, the reason being all methods in an interface are considered to be public by default and when u implement a interface you can't lower/reduce its access modifier
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Donny,
I have noted some points on interfaces when I referred
Java2 The compete reference and the certification study guide.
hope this is useful .
INTERFACES:
1. Interfaces are similar to classes ,but they lack instance variables , and their methods are declared without
Body.
2. The access modifier is public or not used
3. Variables can be declared inside interface declarations.They are implicitly final and static ( ie they can not be changed by the implementing class)
IMPLEMENTING INTERFACES
1. One or more classes can implement that interface by including implements clause in its class definition
2. Access modifier is either public or not used
3. If a class implements more than one interface ,then interfaces are separated by comma
4. If a class implements more than one interface that declare the same method then the same method will be Used by clients of either interface
5. The method that implements that interface must be declared public.Also the type signature of the implementing method must match exactly the type signature specified in the interface definition.

ACCESSING IMPLEMENTATIONS THROUGH INTERFACE REFERENCE
1 Variables can be declared as object references that use an interface rather than a class type
For eg interface name variablename = new class name which implements that interface ()
2 Any instance of any class that implements that declared interface can be stored in such a variable
3 When the method is called thru one of these referencess , the correct version will be called based on the actual instance of the interface being referred to.
4 An interface variable only has the knowledge of the methods declared by its inter face declaration

PARTIAL IMPLEMENTATION
If a class includes an interface but does not fully implement the methods defined by that interface then that class must be declared abstract.
VARIABLES IN INTERFACES
Variables can be declared in interface and they can be initialised with the desired value. By implementing the interface that variables can be used as constants

INTERFACE CAN BE EXTENDED
One interface can extend another interface by using the keyword extends
When a class implements an interface which inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain.


 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Scott Appleton:
An abstract class can declare variables, too. It can also provide implementations of some methods; it is abstract only because at least one method is declared but not implemented.

This isn't quite true. An abstract class does not have to have any abstract methods.

------------------
Co-Moderator of the Programmer Certification Forums
 
INDU, BALA
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Thomas,
Can you please explain this. I am not very clear.
Thankyou.
"An abstract class does not have to have any abstract methods."
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More,
variable declared in interface must be initialized because they are static final.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indu,
It means that the following is a legal class declaration:

Notice that while Foo is declared as abstract, it does not contain any abstract methods. The only thing that abstract does in this case is to prevent instantiating a new Foo. The JLS 8.1.1.1 specifically discourages use of abstract for this purpose alone and suggests using a private constructor instead. See http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#34944

[This message has been edited by JUNILU LACAR (edited July 13, 2001).]
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You can declare a class as absract class, it does not have to have an abstract method. It can't be instantiated.
public abstract class A{
void aMethod(){
}
}
But if the class has any abstract method it has to be declared abstract.
Vanitha.
 
INDU, BALA
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou all. I got it.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the JLS:

A class type should be declared abstract only if the intent is that subclasses can be created to complete the implementation. If the intent is simply to prevent instantiation of a class, the proper way to express this is to declare a constructor (�8.8.8) of no arguments, make it private, never invoke it, and declare no other constructors.

There is another reason. It may be that each method has a default implementation but that no class that extends the abstract class would leave all the methods as their default. A class like MouseAdapter could have been made abstract because no one would ever instantiate a MouseAdapter because it doesn't do anything (all the methods are empty). In fact, it is abstract although it has no abstract methods!!!
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi indu,
1. if a class is declared as abstract ,it is not required that methods of that class are to be declared abstract,
2. but if a class contains an abstract method ,the class must be declared abstract.
thomas is trying to explain my point 1 above.
 
INDU, BALA
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou Thomas & Srinivas for your further clarification on the subject discussed above.
 
reply
    Bookmark Topic Watch Topic
  • New Topic