File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes abstract class & interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "abstract class & interface" Watch "abstract class & interface" New topic
Author

abstract class & interface

swati j
Greenhorn

Joined: Nov 14, 2000
Posts: 10
Please elaborate on differences between an abstract class & an interface.
Surya Bahadur
Ranch Hand

Joined: Sep 28, 2000
Posts: 88
Hi
Rather than me trying to explain the things..take a look at these two URL's which point to Java Tutorial
For Interface
http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
For AbstractClass
http://java.sun.com/docs/books/tutorial/java/javaOO/abstract.html
Surya
Sandeep Jain
Ranch Hand

Joined: Oct 25, 2000
Posts: 124
hai,
Well to expain you in simple words

When we declare an abstract class. It has to be extended by some other class where are the method are overrriden. ie an immediate hierarchy has to be maintained but when we declare an interface we can implement the same anywhere in any of the package . I is not neccessary maintain the same hierarchy.
------------------
Sandeep Jain


Try and Try Till u succeed<br /> <br />Sandeep Jain
Srivatsan Raman
Greenhorn

Joined: Nov 26, 2000
Posts: 3
Hi Swati,
Before going into abstract class and interface we will see what is an abstract method which is the basis of both. An abstract method is the one which is common to both. An abstract method is a method that is just declared but not implemented.
An abstract class is the one which contains atleast one abstract method and one implemented method ( normal method ) whereas an interface has all of functions abstract.
Some other minor differences :
1. All the member functions and variables are explicitly public in an Interface. Because there is no point having a private function because you cannot override it.
2. In an abstract class the methods has to be declared abstract it is not the case in an interface.
3. A subclass has to use implements to inherit an interface.
whereas an abstract class should use extend.
4. You can implement as many interfaces. you cannot do with classes.
Whether it is an interface or abstract class, all the subclasses which inherits from the class should implement those abstract method or else the class becomes abstract or interface based on atleast one of the method is implemented.
To create an object, the class has to be non-abstract, so if a
implements an interface or extends a abstract class to create an object of that type the subclass have to implement all the abstract functions.
e-mail me if u have further doubts on this.
------------------
swati j
Greenhorn

Joined: Nov 14, 2000
Posts: 10
Hi Srivatsan,
You said that an abstract class has atleast one abstract meth. & one normal meth.
But an abstract class is not REQUIRED to have an abstract meth.(,whereas a class which HAS abstract meth. should be declared abstract.)
--If you could explain this?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
You make a class abstract if you want to "force" others to sub-class it before using it. If there are no asbstract methods in it - it would be sort of trivial to make the class abstract, but it would prevent anyone from instantiating the class.
You don't actually need any "normal" methods in an abstract class either.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Weigang Gu
Ranch Hand

Joined: Jan 16, 2001
Posts: 44
Hi, Cindy:
I would appreciate if you can elaborate a little bit more about
substantiate abstract class. I know an abstract class can not be instantiated and I guess an interface can not either. But both can be declared. Am I right?
thanks
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
You are right. An abstract class can not be instantiated. You would need to make a subclass of it, and instantiate the subclass.
Interfaces are a bit trickier. By definition all of the methods in an interface are abstract (they have no method body - no functionality). Actually some interfaces have NO methods at all. These exist only as "markers" to say "I am this sort of a thing".
Now when you say that an interface can not be instatiated, in the strictest sense that is true. However, there is the case of "upcasting" to an interface which you have to deal with.
Runnable is an interface. It is used to create objects that have threads.
You CAN NOT say:
Runnable r = new Runnable();
However if you have a class MyClass implements Runnable you CAN say:
Runnable r = new MyClass();
In this way you can end up with objects that appear to be instances of the interface, but actually hold objects of MyClass.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract class & interface