Programming Language

Difference Between An Interface And An Abstract Class In Java

Abstract class and interface both are utilized to accomplish abstraction in java. Still there are part of contrasts between interface class and abstract class.

Interface Class: Java Interfaces are identical to protocols. They fundamentally represent a behavior that has been agreed-upon to encourage interaction between random objects. For instance, the control buttons on a Remote Controller make up the interface for people to communicate with TV. The manner in which this interface is made by various companies isn’t specific and you are barely conscious of or troubled about how exactly these buttons happen to be implemented to function internally. Interface is simply an agreement involving the maker as well as the buyer. The way the maker implements the exposed actions is generally not cared by the customer.

In Java, an Interface is usually several grouped methods with null bodies. You could have constant declarations within a Java Interface also. A class that makes use of the interface concurs to the exposed actions by implementing every one of the methods of the interface.

Implementing an interface indicates the class will aid a minimum of the exposed behavior. It may undoubtedly include a variety of additional behaviors/properties because of its clients.

interface TVRemoteController{
void power();
void setChannel(int channelNumber);
void upChannel();
void downChannel();
void upVolume();
void downVolume();
……
}

A sample implementation of this interface by a vendor, say Sony:

public class SonyTVRemoteController implements TVRemoteController{
/*…this class can have other methods, properties as well …*/
……
void power(){
//implementation of power() method of the interface
}
void setChannel(int channelNumber){
//implementation of setChannel(int) method of the interface
}
//similarly, implementation of other methods of the interface
……
}

 

Abstract Class: In Java, abstract class is actually a class that has been declared ‘abstract’. By declaring ‘abstract’ we make certain that class cannot be instantiated. Why then do we need to have such type of classes? Mainly because, you wouldn’t be having implementation of all of the methods within that class and you might want to allow the subclass to determine how you can put them into action. In this instance, there isn’t any point instantiating a partial class.

An abstract method is a method which does not have any implementation. When a class has just one abstract method, then you definitely have to declare the class ‘abstract’. Nevertheless, its not necessary to possess one or more abstract method to declare a class abstract. It is possible to declare a full class as ‘abstract’ too. This exercise is rarely used. One feasible explanation might be that you by no means want your customers to instantiate your class straight despite the fact that you have already given default implementation of all of the methods.

public abstract class SampleAbstractClass{
//…fields
……
//…non-abstract methods, if any
……
//…abstract method, if any J
abstract void sampleAbstractMethod(); //… ends with ‘;’
}

public class SubClassOfSampleAbstractClass extends SampleAbstractClass{
//… fields, and non-abstract methods (if any)
……
//…implementation of the abstract method
void sampleAbstractMethod(){
……
}
}

Leave a Reply