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
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. ------------------
Joined: Nov 14, 2000
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?
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
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
Joined: Sep 29, 2000
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.