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

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

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

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

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

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.

Comments
great explanations

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.

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!

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?

This article has been dead for over six months. Start a new discussion instead.