• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class and Interface

 
gurpreetsingh hora
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please continue the discussion on the thread you started earlier.
 
Aby Ouseph
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanjeev

No tight coupling between itself and it's implementing class

what does tight coupling means here??
Would you please suggest ?
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic