wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Abstract & Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract & Interface" Watch "Abstract & Interface" New topic
Author

Abstract & Interface

Santoo
Greenhorn

Joined: Aug 04, 2000
Posts: 6
Could someone plz. make the concepts clear.
Both Interface & Abstract class has got empty declaration of methods.Then why can't we use Interface instead of Abstract Class ? When do we create Abstract Class ?
Thanks In Advance.
Santoo
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
An abstract class can have 100 methods in it only one of which is abstract. So all you have to do is implement that 100th method and run.
An interface with 100 methods has... well... 100 abstract methods. You have to implement ALL of them in the class implementing that interface.


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
naren
Greenhorn

Joined: Aug 06, 2000
Posts: 9
santoo,both abstract class and interface defines null body in its methods,which need to be implemented in other classes.
Abstract classes are used,when ever u need variable declaration,methods.
Interface doesn't allow to declare the variables.

bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Just to see if I have this straight:
Abstract classes can have thier own methods and instance variables like any other class but one or more of the methods are abstract, meaning they have no body and the body needs to be defined in the class that extends the abstract class, or that class must also be declared as abstract. You can not instantiate an abstract class, but you can declare a variable of an abstract class type for polymorphism (just as you can with interfaces).
Interfaces can have methods and variables also, but by default the methods are public and abstract and the variables are public, static, and final. Since the methods are abstract, none of them can have a body and all of the methods must be declared in the class implementing the interface, or else that class will also be an interface.
And the third variation to this is too not have an interface or abstract class, but to just have a superclass and subclass and have the subclass override the method in the superclass. This seems useful when the superclass can act on its own and making an instance of it makes since. This can be a way of having a default action on the method that can then be overridden by sub-classes.
Now from what I can see, you would use an abstract class when you have common methods that any subclass needs to have, but you also have methods for the abstract class that need to do something, or have a body. You use interfaces when you do not want the class to do anything, but tell all other classes the methods that they must have in order to be instatiated. And you use superclasses and overriding when the superclass can act as its own class and making an instace of that class is an appropriate action.
I think I am putting that in pretty simple terms but that is what I have gathered from the main differences of when to use an interface, abstract class, or just a superclass.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract & Interface
 
Similar Threads
why can't we instantiate abstract class
regarding Abstract class and Interface
Core Java
Abstract Class & Interface
Abstract and Interface