Interface
Abstract
- Abstract cannot be used to instantiate objects
- An abstract class contains one or more abstract methods, abstract methods do not provide implementations
- Constructors and static methods cannot be declared abstract, constructors are not inherited, static methods are not overrided
Interface
- Interface the blueprint of the class, contains only constants, method signatures, default methods, static methods, and nested types
- Interface specifies a set of methods that the class has to implement
- All interface members must be public
- All methods in an interface are implicitly public abstract
- All fields in an interface are implicityly public, static and final
- All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier
- If a class implements an interface and does not provide method bodies for all functions specified in the interface, then class must be declared abstract
Difference
- Type of methods: Interface can have only abstract methods. Abstract class can have abstract and non-abstract methods. From Java 8, it can have default and static methods also
- Final Variables: Variables declared in a Java interface are by default final. An abstract class may contain non-final variables
- Type of variables: Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables
- Implementation: Abstract class can provide the implementation of interface. Interface can’t provide the implementation of abstract class
- Inheritance vs Abstraction: A Java interface can be implemented using keyword “implements” and abstract class can be extended using keyword “extends”
- Multiple implementation: An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces
- Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.
When to use what?
- Abstract
- In java application, there are some related classes that need to share some lines of code then you can put these lines of code within abstract class and this abstract class should be extended by all these related classes
- You can define non-static or non-final field(s) in abstract class, so that via a method you can access and modify the state of Object to which they belong
- You can expect that the classes that extend an abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private)
- Interface
- It is total abstraction, All methods declared within an interface must be implemented by the class(es) that implements this interface
- A class can implement more than one interface. It is called multiple inheritance
- You want to specify the behavior of a particular data type, but not concerned about who implements its behavior
Abstract and Interface
public interface Drivable
{
// Constant
int wheel = 4;
// Abstract methods
String getMaker();
void setMaker(String m);
// Static methods
static String getType()
{
return "Wheel: "+4;
}
// Default methods
default void display()
{
System.out.println("Wheel: "+wheel);
}
}
public abstract class Car implements Drivable
{
private String maker;
public Car(String m)
{
maker = m;
}
public String getMaker()
{
return maker;
}
public void setMaker(String m)
{
maker = m;
}
@Override
public String toString()
{
return "Maker: "+maker;
}
public abstract void display();
}
public class Buick extends Car
{
private String color;
public Buick(String m, String c)
{
super(m);
color = c;
}
public String getColor()
{
return color;
}
public void setColor(String c)
{
color = c;
}
public String toString()
{
return super.toString()+" "+"Color: "+color;
}
@Override
public void display()
{
System.out.println("Buick display ...");
}
}
public class Test
{
public static void main(String args[])
{
Drivable d = new Buick("Buick", "White");
System.out.println(d.wheel);//static field
System.out.println(Drivable.getType());//static method
d.display();//default method
System.out.println(d);//toString
Buick b = new Buick("Honda", "Buick");
System.out.println(b.getMaker()+" "+b.getColor());//abstract methods
}
}
- Static fields are able to be inherited
- Static methods are not able to be inherited
- Default methods
- Not mention the default method at all, which lets your extended interface inherit the default method
- Redeclare the default method, which makes it abstract
- Redefine the default method, which overrides it
- Abstract methods must be implemented or by a class, if not, the class has to be declared as abstract class
Multiple Inheritance
public interface Drivable
{
// Constant
int wheel = 4;
// Abstract methods
String getMaker();
void setMaker(String m);
// Default methods
default void display()
{
System.out.println("Wheel: "+wheel);
}
}
public interface Paintable
{
String getColor();
void setColor(String c);
}
public class Car implements Drivable, Paintable
{
private String maker;
private String color;
public Car(String m, String c)
{
maker = m;
color = c;
}
public String getColor()
{
return color;
}
public String getMaker()
{
return maker;
}
public void setColor(String c)
{
color = c;
}
public void setMaker(String m)
{
maker = m;
}
@Override
public String toString()
{
return "Maker: "+maker+" Color: "+color;
}
}
public class Test
{
public static void main(String args[])
{
Drivable c = new Car("Buick", "White");
System.out.println(c.getMaker());
Paintable c2 = new Car("Honda", "Blue");
System.out.println(c2.getColor());
}
}
Implements Interfaces Containing Same Functions
public interface Drivable
{
// Constant
int wheel = 4;
// Abstract methods
String getMaker();
void setMaker(String m);
// Default methods
default void display()
{
System.out.println("Driable ...");
}
}
public interface Paintable
{
String getColor();
void setColor(String c);
default void display()
{
System.out.println("Paintable ...");
}
}
public class Car implements Drivable, Paintable
{
private String maker;
private String color;
public Car(String m, String c)
{
maker = m;
color = c;
}
public String getColor()
{
return color;
}
public String getMaker()
{
return maker;
}
public void setColor(String c)
{
color = c;
}
public void setMaker(String m)
{
maker = m;
}
@Override
public void display()
{
Drivable.super.display();
Paintable.super.display();
}
@Override
public String toString()
{
return "Maker: "+maker+" Color: "+color;
}
}
Reference