943,657 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1663
  • Java RSS
Aug 27th, 2008
0

how is abstact class different from Interface??

Expand Post »
hey i am beginner in java. i am not able to resolve differences in interface and with abstract class. do abstract class have methods defined??does those methods have body?? if so for abstract classes do not create objects, how they r invoked??
Reputation Points: 3
Solved Threads: 0
Light Poster
ravikiran032 is offline Offline
39 posts
since Jun 2008
Aug 27th, 2008
2

Re: how is abstact class different from Interface??

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--

java Syntax (Toggle Plain Text)
  1.  
  2. public interface MyInterface{
  3.  
  4. public final int VALUE = 100; // constant
  5.  
  6. public void doSomething(); // method declaration within interface
  7.  
  8. }

--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)
  1.  
  2. public class MyTestClass implements MyInterface{
  3.  
  4. public void doSomething(){
  5. System.out.println(VALUE);
  6. }
  7.  
  8. }

--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)
  1.  
  2. public abstract class MyAbstractClass{
  3.  
  4. protected abstract void subCommand();
  5.  
  6. public final void templateMethod(){
  7. System.out.println("Performing a defined command...");
  8. subCommand();
  9. System.out.println("SubCommand finished!");
  10. }
  11. }

--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)
  1.  
  2. public class MyOtherClass extends MyAbstractClass{
  3.  
  4. protected void subCommand(){
  5. System.out.println("Whoo! This is my method! O_O");
  6. }
  7.  
  8. public static void main(String... args){
  9. MyAbstractClass mac = new MyOtherClass();
  10. mac.templateMethod();
  11. }
  12. }

--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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 27th, 2008
0

Re: how is abstact class different from Interface??

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.
Reputation Points: 3
Solved Threads: 0
Light Poster
ravikiran032 is offline Offline
39 posts
since Jun 2008
Aug 27th, 2008
0

Re: how is abstact class different from Interface??

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.
I'm not sure if I'm understanding the question.

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!
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 27th, 2008
0

Re: how is abstact class different from Interface??

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?
Reputation Points: 13
Solved Threads: 1
Newbie Poster
jack239 is offline Offline
14 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: System.out
Next Thread in Java Forum Timeline: Using Formatter to print new lines





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC