• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

abstract class & interface

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please elaborate on differences between an abstract class & an interface.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic