- If we don't know anything about implementation just we have requirement specifications then we should go for the interface.
eg-: Servlet.
- If we are talking about implementation but not completely(partial implementation ) then we should go for an abstract class.
eg-:GenricServlet,HttpServlet
- If we are talking about implementation completely and ready to provide service then we should go for a concrete class
eg-: myOwnServlet
Ques-: Anyway, we can't create an object for an abstract class but the abstract class can contain a constructor what is the need?
Ans-:Abstract class constructor will be executed whenever we are creating a child class object to perform initialization of the child class object.
Approach 1-: without having a constructor in the abstract class.
Ques-: why this code is not recommended?
Ans-: more code and code redundancy problems.
abstract class person{
String name;
int age;
.
.
.
.
100 properties;
}
class Student extends person{
int roll_no;
Student(String name,int age,String color,..........100 properties){
this.name=name;
this.age=age;
this.color=color;
.
.
.
. 100 properties;
this.roll_no=roll_no;
String name;
int age;
.
.
.
.
100 properties;
}
class Student extends person{
int roll_no;
Student(String name,int age,String color,..........100 properties){
this.name=name;
this.age=age;
this.color=color;
.
.
.
. 100 properties;
this.roll_no=roll_no;
}
}
class Teacher extends person{
String subject;
Teacher(String name,int age,String color,..........100 properties){
this.name=name;
this.age=age;
this.color=color;
.
.
.
. 100 properties;
this.subject=subject;
}
}
class Teacher extends person{
String subject;
Teacher(String name,int age,String color,..........100 properties){
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 pass);
Teacher t=new Teacher(101 properties pass);
}
class Main{
public static void main(String[] args){
Student s=new Student(101 properties pass);
Teacher t=new Teacher(101 properties pass);
}
}
Approach 2-: with constructor inside an abstract class.
this constructor will work for every child object initialization.
Ques-: why this code is recommended?
Ans-: less code and code reusability.
abstract class person{
String name;
int age;
.
.
.
.
100 properties;
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 roll_no;
student(String name,int age,String color,..........100 properties){
super(100 properties pass);
this.roll_no=roll_no;
}
this.name=name;
this.age=age;
this.color=color;
.
.
.
. 100 properties
}
}
class Student extends person{
int roll_no;
student(String name,int age,String color,..........100 properties){
super(100 properties pass);
this.roll_no=roll_no;
}
}
class Teacher extends person{
String subject;
class Teacher extends person{
String subject;
Teacher(String name,int age,String color,..........100 properties){
super(100 properties pass);
this.subject=subject;
}
}
class Main{
public static void main(String[] args){
Student s=new Student(101 properties pass);
Teacher t=new Teacher(101 properties pass);
}
class Main{
public static void main(String[] args){
Student s=new Student(101 properties pass);
Teacher t=new Teacher(101 properties pass);
}
}
Note-:either directly or indirectly we can't create objects for abstract classes.
Ques-: Anyway, we can't create objects for the abstract class and interface but the abstract class can contain a constructor but the interface does not contain a constructor what is the reason?
Ans-: The main purpose of a constructor is to perform the initialization of instance variables.
An abstract class can contain an instance variable that is required for a child object to perform the initialization of those instance variables constructor is required for an abstract class.
But every variable present inside the interface is always public static final whether we are declaring or not and there is no chance of existing instance variables inside the interface hence constructor concept is not required for the interface.
whenever we are creating a child class object parent object won't be created just the parent class constructor will be executed for the child object purpose only.
eg-:
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());
}
}
Ques-: Inside the interface, every method is always abstract and we can take only abstract methods in the abstract class also then what is the difference b/w interface and an abstract class is it possible to replace the interface with the abstract class?
Ans-: we can replace the interface with an abstract class but it is not good programming practice. This is something like recruiting an officer for swiping activity.
if everything is abstract then it is highly recommended to go for the interface.