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??
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!!
Joined: Mar 20, 2006
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
[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.
Joined: Mar 20, 2006
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.
Joined: May 27, 2007
No tight coupling between itself and it's implementing class
what does tight coupling means here?? Would you please suggest ? thanks