What is an Abstract Class?

A class must be declared abstract if any of the following condition is true

  • The class has one or more abstract methods
  • The class inherits one or more abstract methods for which it does not provide implementation
  • The class declares that it implements an interface but does not provide implementation for every method of that interface

An abstract class cannot be instantiated. Abstract classes defer the implementation to subclasses. The subclass must provide the implementation of the abstract method or declare itself to be abstract in which case the implementation is deferred once again. You can declare a variable of an abstract class.

Example:

public abstract class LivingThing {
	public void breath(){
		System.out.println("Living Thing breathing...");	}
	public void eat(){
		System.out.println("Living Thing eating...");	}
	public abstract void walk();
}
public class Human extends LivingThing {
public void walk(){
	System.out.println("Human walks..."); }
}

 What is an Interface?

It defines a standard and public way of specifying the behavior of classes. All methods of an interface are abstract methods. It defines the signatures of a set of methods, without the body (implementation of the methods). A concrete class must implement the interface (all the abstract methods of the Interface). It allows classes, regardless of their locations in the class hierarchy, to implement common behaviors.
Example:

public interface Relation {
    public boolean isGreater( Object a, Object b);
    public boolean isLess( Object a, Object b);
    public boolean isEqual( Object a, Object b);
}

Implementing Interfaces Example:

public class Line implements Relation {
    private double x1;
    private double x2;
    private double y1;
    private double y2;
public Line(double x1, double x2, double y1, double y2){
    this.x1 = x1;
    this.x2 = x2;
    this.y1 = y1;
    this.y2 = y2; }
public double getLength(){
    double length = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)* (y2-y1));
    return length; }
public boolean isGreater( Object a, Object b){
    double aLen = ((Line)a).getLength();
    double bLen = ((Line)b).getLength();
    return (aLen > bLen); }
public boolean isLess( Object a, Object b){
    double aLen = ((Line)a).getLength();
    double bLen = ((Line)b).getLength();
    return (aLen < bLen);}
public boolean isEqual( Object a, Object b){
    double aLen = ((Line)a).getLength();
    double bLen = ((Line)b).getLength();
    return (aLen == bLen); }
}

All data fields are public final static and all methods are public abstract in an interface. For this reason, these modifiers can be omitted, as shown below:

The final Modifier

The final class cannot be extended:

final class Math {
     ...
}

The final variable is a constant:

final static double PI = 3.14159;

The final method cannot be overridden by its subclasses. The modifiers are used on classes and class members (data and methods), except that the final modifier can also be used on local variables in a method. A final local variable is a constant inside a method.

Why do we use Interfaces?

Reason #1: Encapsulation

To reveal an object’s programming interface (functionality of the object) without revealing its implementation. This is the concept of encapsulation.

  • The implementation can change without affecting the caller of the interface
  • The caller does not need the implementation at the compile time
  • It needs only the interface at the compile time
  • During runtime, actual object instance is associated with the interface type

Reason #2: Combining similar behaviors

To have unrelated classes implement similar methods (behaviors). One class is not a sub-class of another. For example:

Class Line and class MyInteger are not related through inheritance. You want both to implement comparison methods as given below

  • checkIsGreater(Object x, Object y)
  • checkIsLess(Object x, Object y)
  • checkIsEqual(Object x, Object y)

Define Comparison interface which has the three abstract methods above.

Reason #3: Multiple Inheritance

To model multiple inheritance a class can implement multiple interfaces while it can extend only one class.

Interface vs. Abstract Class

  • All methods of an Interface are abstract methods while some methods of an Abstract class are abstract methods. Abstract methods of abstract class have abstract modifier
  • An interface can only define constants while abstract class can have member fields
  • Interfaces have no direct inherited relationship with any particular class, they are defined independently while Abstract classes become part of the class hierarchy. (Interfaces themselves have inheritance relationship among themselves)

Interface as a Type

If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface. Let’s say Person class implements PersonInterface interface

Person p1 = new Person();
PersonInterface pi1 = p1;
PersonInterface pi2 = new Person();

Example: Implementing Multiple Interfaces

public class CSStudent extends Student 
implements PersonInterface, oneInterface, ThirdInterface
{
    // All abstract methods of all interfaces
    // need to be implemented.
}

An interface can only be implemented by classes or extended by other interfaces

Inheritance Among Interfaces

Interfaces are not part of the class hierarchy However, interfaces can have inheritance relationship among themselves

public interface PersonInterface {
    void doSomething();
}
public interface StudentInterface extends PersonInterface {
    void doExtraSomething();
}
Tagged with: ClassesJAVAObject Oriented
 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.