Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
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
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
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.
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.
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss