| | |
how is abstact class different from Interface??
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
An interface is a static context template that classes can implement. The methods within the interface have no body and must be of public access.
In addition, interfaces can also contain "constants" in which data is declared and defined.
An example--
--when a class implements an interface, the class also implements the methods the interface contains. However, the implementing class MUST define the methods implemented.
An example--
--notice that the class MyTestClass doesn't declare VALUE however it is defined in the interface and therefore MyTestClass implements the public-accessible VALUE also.
An Abstract class is much like both a class AND an interface, however moreso a class.
An Abstract class has the potential to have default methods and interface-like methods where the methods MUST be defined by concrete subclasses that extend from the abstract class.
Furthermore, the concept of Abstract is "not fully defined" so in that respect, abstract classes cannot be instantiated.
An example--
--do not be distracted by the protected and final modifiers. The key focus is the abstract void subCommand method. Notice that an abstract class is like an interface in which it can house methods without definitions (so long as they are declared abstract) and additionally you can't instantiate an abstract class much like you can't instantiate an interface.
However when you are using an abstract class in a subclass you must override the abstract methods you are implementing from the abstract class.
An example--
--notice that I'm storing MyOtherClass into a reference-variable called MyAbstractClass and then the templateMethod is called. Because MyOtherClass has a specialized implementation of subCommand, the call to templateMethod polymorphically calls the overridden subCommand within the template algorithm.
Hopefully with the above example, you can see why abstract classes and interfaces are extremely useful.
The major difference between an abstract class and an interface is the way Java handles each - you can extend only one class but you can implement an infinite amount of interfaces.
That being said, use interfaces whenever possible if the implementing class needs more implementations.
In addition, interfaces can also contain "constants" in which data is declared and defined.
An example--
java Syntax (Toggle Plain Text)
public interface MyInterface{ public final int VALUE = 100; // constant public void doSomething(); // method declaration within interface }
--when a class implements an interface, the class also implements the methods the interface contains. However, the implementing class MUST define the methods implemented.
An example--
java Syntax (Toggle Plain Text)
public class MyTestClass implements MyInterface{ public void doSomething(){ System.out.println(VALUE); } }
--notice that the class MyTestClass doesn't declare VALUE however it is defined in the interface and therefore MyTestClass implements the public-accessible VALUE also.
An Abstract class is much like both a class AND an interface, however moreso a class.
An Abstract class has the potential to have default methods and interface-like methods where the methods MUST be defined by concrete subclasses that extend from the abstract class.
Furthermore, the concept of Abstract is "not fully defined" so in that respect, abstract classes cannot be instantiated.
An example--
java Syntax (Toggle Plain Text)
public abstract class MyAbstractClass{ protected abstract void subCommand(); public final void templateMethod(){ System.out.println("Performing a defined command..."); subCommand(); System.out.println("SubCommand finished!"); } }
--do not be distracted by the protected and final modifiers. The key focus is the abstract void subCommand method. Notice that an abstract class is like an interface in which it can house methods without definitions (so long as they are declared abstract) and additionally you can't instantiate an abstract class much like you can't instantiate an interface.
However when you are using an abstract class in a subclass you must override the abstract methods you are implementing from the abstract class.
An example--
java Syntax (Toggle Plain Text)
public class MyOtherClass extends MyAbstractClass{ protected void subCommand(){ System.out.println("Whoo! This is my method! O_O"); } public static void main(String... args){ MyAbstractClass mac = new MyOtherClass(); mac.templateMethod(); } }
--notice that I'm storing MyOtherClass into a reference-variable called MyAbstractClass and then the templateMethod is called. Because MyOtherClass has a specialized implementation of subCommand, the call to templateMethod polymorphically calls the overridden subCommand within the template algorithm.
Hopefully with the above example, you can see why abstract classes and interfaces are extremely useful.
The major difference between an abstract class and an interface is the way Java handles each - you can extend only one class but you can implement an infinite amount of interfaces.
That being said, use interfaces whenever possible if the implementing class needs more implementations.
Last edited by Alex Edwards; Aug 27th, 2008 at 1:47 am.
•
•
•
•
suppose in abstact class "InputStream" we can implement method read(byte[]) with it's reference. how does this method is being refferred by it's abstract class as there will be no instantiation to this abstact class.
Do you mean how are you able to call read from an object of type InputStream if you cannot directly instantiate one? If so then please re-read my post, this is mentioned in there.
If you mean extending the abstract class then using read, you will have to override the read method with your own implementation of read. Your concrete class that extends from the abstract class should not be marked abstract, so that you will be able to instantiate your concrete class and still have the functionality of the extending class. Read my above post thoroughly - this is also mentioned.
Also, this might help-- CLICK!
You know what?
You should use interface when you want to take all the common features from different stuffs say you have one interface that contains method a() and another interface that contains method b(). Now in the situation when you want your program to have both a() and b() need to be present and you want a restriction that a() and b() shouldn't be present in one file you should break them in two interfaces because your program can implement two interfaces simultaneously. So here you can apply Interface. So when I need to provide the multiple inheritance functionality I should use Interface.
You should use abstract class when you want multilevel inheritance. Because abstract class can only be extended once.
Am I clear from this point? If not then let me know and I'll explain with real life scenarios and examples to make them clear as to where to use Interface and abstract class?
You should use interface when you want to take all the common features from different stuffs say you have one interface that contains method a() and another interface that contains method b(). Now in the situation when you want your program to have both a() and b() need to be present and you want a restriction that a() and b() shouldn't be present in one file you should break them in two interfaces because your program can implement two interfaces simultaneously. So here you can apply Interface. So when I need to provide the multiple inheritance functionality I should use Interface.
You should use abstract class when you want multilevel inheritance. Because abstract class can only be extended once.
Am I clear from this point? If not then let me know and I'll explain with real life scenarios and examples to make them clear as to where to use Interface and abstract class?
Stuck in the middle of Java/PHP/Ajax development?
For free help on any technology and free articles visit http://www.techcubetalk.com
For free help on any technology and free articles visit http://www.techcubetalk.com
![]() |
Other Threads in the Java Forum
- Previous Thread: System.out
- Next Thread: Using Formatter to print new lines
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arc arguments array arrays automation binary bluetooth c++ chat class classes client code codesnippet component csv database doctype draw ebook eclipse error event exception fractal freeze game givemetehcodez graphics gui html ide image input integer intellij iphone j2me java java.xls javaprojects jmf jni jpanel julia linux list loop loops mac map method methods mobile netbeans newbie number online oracle page parameter plazmic print problem program programming project recursion reporting rotatetext scanner screen sell server set size sms socket sort sourcelabs sql string superclass swing system template test testautomation threads time title tree tutorial-sample windows working





