- If we don't know anything about the implementation and only have requirement specifications, then we should go for an interface.
e.g.: Servlet - If we are talking about an implementation, but not a complete one (partial implementation), then we should go for an abstract class.
e.g.: GenericServlet, HttpServlet - If we are talking about a complete implementation that is ready to provide service, then we should go for a concrete class.
e.g.: MyOwnServlet
Difference between Interface and Abstract Class
| Interface | Abstract Class |
|---|---|
| If we don't know anything about the implementation and only have requirement specifications, then we should go for an interface. | If we are talking about an implementation, but not a complete one (partial implementation), then we should go for an abstract class. |
| Inside an interface, every method is always public and abstract, whether we declare it or not. Hence, an interface is considered a 100% pure abstract class. | Methods in an abstract class need not be public and abstract, and we can also have concrete methods. |
| As every interface method is always public and abstract, we can't declare it with the following modifiers: private, protected, final, static, synchronized, native, and strictfp. | There are no restrictions on abstract class method modifiers. |
| Every variable inside an interface is always public static final, whether we declare it or not. | Variables inside an abstract class need not be public, static, or final. |
| As every interface variable is always public static final, we can't declare it with the following modifiers: private, protected, volatile, and transient. | There are no restrictions on abstract class variable modifiers. |
| For an interface variable, we must perform initialization at the time of declaration. Otherwise, we will get a compile-time error. | For abstract class variables, we are not required to perform initialization at the time of declaration. |
| Inside an interface, we can't declare static and instance blocks. | Inside an abstract class, we can declare static and instance blocks. |
| Inside an interface, we can't declare constructors. | Inside an abstract class, we can declare constructors. |
Why Does an Abstract Class Need a Constructor?
Ques: We can't create an object of an abstract class, but an abstract class can still contain a constructor. What is the need for it?
Ans: The abstract class constructor is executed whenever we create a child class object, to perform the initialization of the child class object.
Approach 1: Without a constructor in the abstract class
Ques: Why is this code not recommended?
Ans: It has more code and redundancy problems.
abstract class Person {
String name;
int age;
// ...
// 100 properties
}
class Student extends Person {
int rollNo;
Student(String name, int age, String color, /* ... 100 properties */ int rollNo) {
this.name = name;
this.age = age;
this.color = color;
// ...
// 100 properties
this.rollNo = rollNo;
}
}
class Teacher extends Person {
String subject;
Teacher(String name, int age, String color, /* ... 100 properties */ String subject) {
this.name = name;
this.age = age;
this.color = color;
// ...
// 100 properties
this.subject = subject;
}
}
class Main {
public static void main(String[] args) {
Student s = new Student(/* 101 properties */);
Teacher t = new Teacher(/* 101 properties */);
}
}
Approach 2: With a constructor inside the abstract class
This constructor works for the initialization of every child object.
Ques: Why is this code recommended?
Ans: It has less code and better code reusability.
abstract class Person {
String name;
int age;
// ...
// 100 properties
Person(String name, int age, String color, /* ... 100 properties */) {
this.name = name;
this.age = age;
this.color = color;
// ...
// 100 properties
}
}
class Student extends Person {
int rollNo;
Student(String name, int age, String color, /* ... 100 properties */ int rollNo) {
super(/* 100 properties */);
this.rollNo = rollNo;
}
}
class Teacher extends Person {
String subject;
Teacher(String name, int age, String color, /* ... 100 properties */ String subject) {
super(/* 100 properties */);
this.subject = subject;
}
}
class Main {
public static void main(String[] args) {
Student s = new Student(/* 101 properties */);
Teacher t = new Teacher(/* 101 properties */);
}
}
Note: We can't create objects of abstract classes, either directly or indirectly.
Why Does an Interface Not Have a Constructor?
Ques: We can't create objects of an abstract class or an interface. An abstract class can contain a constructor, but an interface cannot. What is the reason?
Ans: The main purpose of a constructor is to initialize instance variables.
An abstract class can contain instance variables. A constructor is required for the child object to initialize those instance variables, so an abstract class needs a constructor.
But every variable inside an interface is always public static final, whether we declare it or not, and there is no chance of instance variables existing inside an interface. Hence, the constructor concept is not required for an interface.
Whenever we create a child class object, the parent object is not created. Only the parent class constructor is executed, for the purpose of initializing the child object.
e.g.:
class Parent {
Parent() {
System.out.print(this.hashCode());
}
}
class Child extends Parent {
Child() {
System.out.print(this.hashCode());
}
}
class Test {
public static void main(String... args) {
Child c = new Child();
System.out.print(c.hashCode());
}
}
Can We Replace an Interface with an Abstract Class?
Ques: Inside an interface, every method is always abstract, and we can also take only abstract methods in an abstract class. Then what is the difference between an interface and an abstract class? Is it possible to replace an interface with an abstract class?
Ans: We can replace an interface with an abstract class, but it is not good programming practice. It is like recruiting an officer for a sweeping job.
If everything is abstract, it is highly recommended to go for an interface.
| Interface | Abstract Class |
|---|---|
| interface X { } |
abstract class X { } |
| class Test implements X { } |
class Test extends X { } |
| While implementing an interface, we can extend some other class, and hence we don't miss any inheritance benefit. | While extending an abstract class, it is not possible to extend any other class, and hence we miss the inheritance benefit. |
| In this case, object creation is not costly. Test t = new Test(); // 2 seconds |
In this case, object creation is costly. Test t = new Test(); // 2 minutes |

0 Comments