wood burning stoves 2.0*
The moose likes Java in General and the fly likes Abstract class and Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Abstract class and Interface" Watch "Abstract class and Interface" New topic
Author

Abstract class and Interface

gurpreetsingh hora
Greenhorn

Joined: May 27, 2007
Posts: 7
Hi can somebody suggest me why do we need to define a class abstract if it's method is/are abstract ,i know that if i am not defining it as abstarct it will give error.
so why is it so?
and why do we need abstract class??

and also talking about the interface why do we actually need the interface??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
Please continue the discussion on the thread you started earlier.
Aby Ouseph
Greenhorn

Joined: Dec 01, 2006
Posts: 10
Interface is mainly used to show the behavior of derieved class.if we declare function in an interface, we have to define that functions in the class that implement the interface.
Sanjeev Kulkarni
Greenhorn

Joined: Mar 20, 2006
Posts: 26
It's a design contract between an abstract method and it's class. In Java, every relationship has a contract(set of obligations to be followed)

Since the abstract method(member of the class)itself needs to be implemented to define it's behavior in the subclass extending the class in which this method is declared abstract, the whole class is abstract!

And, an abstract class needn't have any abstract method declared inside of it while the class has to be abstract, if it's to include at least one abstract method!!
Sanjeev Kulkarni
Greenhorn

Joined: Mar 20, 2006
Posts: 26
Advantages with interfaces:
a) Class can implement any no of interfaces- thus lot of behaviors
b) No tight coupling between itself and it's implementing class
c) Change in design doesn't pose code-change headache or lot of code changes in the code base, unlike in case of abstract class that provides uni-directional inheritance property to the client code
d) No concrete methods and only public static final variables and abstract methods (by default), no constructors, no instance variables
Arnav Velimala
Ranch Hand

Joined: Jun 04, 2007
Posts: 37
If you don't want to instantiate a class, you declare it as "abstract". For ex, the javax.servlet.http.HttpServlet class is an abstract class even though none of its method are abstract.
Arnav Velimala
Ranch Hand

Joined: Jun 04, 2007
Posts: 37
Interfaces can also be used to relate two un-related classes.
The two classes can be related by implemeting the same interface.
(as any class can implement any interface)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Hi, Ced@r715
Welcome to the ranch. Yu may not be aware of the Naming Policy here. Please read the following document, and change your name accordingly Thank you.
http://www.javaranch.com/name.jsp


[My Blog]
All roads lead to JavaRanch
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
[Sanjeev Kulkarni]c) Change in design doesn't pose code-change headache or lot of code changes in the code base, unlike in case of abstract class that provides uni-directional inheritance property to the client code

Isn't that the opposite case. All methods in an Interface are public hence a part of the public API. Once you add a method all those classes that implement that interface or its subinterface would have to implement those methods.
Sanjeev Kulkarni
Greenhorn

Joined: Mar 20, 2006
Posts: 26
Yes, you're right, we need to obviously make code changes; But, not as much as in case of abstract class scenario- where all subclasses will have to be changed if they're dependent on each through a chain of abstract classes until the first "real" subclass of Super Class implements the abstract method of the Super Class.
gurpreetsingh hora
Greenhorn

Joined: May 27, 2007
Posts: 7
Sanjeev

No tight coupling between itself and it's implementing class

what does tight coupling means here??
Would you please suggest ?
thanks
 
wood burning stoves
 
subject: Abstract class and Interface
 
Similar Threads
Regarding abstract class
abstract class and interface
abstract class and interface
Interface vs abstract class
why to go for interface