Q. Any way we can't create object for abstract class but abstract class can contain constructor. what is the need?
Ans. a. Abstract class constructor will be executed whenever we are creating child class object to perform initialization of child object.
Approach 1 : without having constructor in abstract class.
abstract class Person{
String name;
String gender;
//100 properties.
}
class Student extends Person{
int rollNo;
Stucent(100 properties){
this.name = name;
this.age=age;
.
.
.
this.rollNo = rollno;
}
}
Student student = new Student(101 properties);
as like above we created 1000 classes - by above code, code redundancy problem is there.
Approach 2 : With constructor inside abstract class.
abstract class Person{
String name;
String gender;
//100 properties.
Person(100 properties){
this.name = name;
this.gender = gender;
.
100 lines of code; // this code will work for every child object
.
}
}
class Student extends Person{
int rollNo;
Student(100 properties){
super(100 properties);
this.rollNo = rollno;
}
}
Student s1 = new Student(101 properties);
less code and code re-usability.
Note : 1. Either directly or indirectly we can't create object for abstract class.
Q. anyway we can't create objects for abstract class and interface but abstract class can contain constructor but interface doesn't contain constructor. What is the reason?
Ans : The main purpose of constructor is to perform initialization of instance variables.
abstract class can contain instance variables which are required for child object. To perform initialization initialization of child those instance variables constructor is required for abstract class.
But every variable present inside interface is always public static final whether we are declaring or not and there is no chance of existing instance variable inside interface. Hence constructor concept is not required for interface.
Whenever we are creating child class object parent object won't be created just parent class constructor will be executed for child object purpose only.
class P{
P(){
sopln(this.hashCode()); //output : 100
}
}
class C extends P{
C(){
sopln(this.hashCode()); //output : 100
}
}
class Test{
public static void main(String[] args){
C c = new C();
sopln(c.hashCode()); //output : 100
}
}
Q. inside interface every method is always abstract and we can take only abstract methods in abstract class also then what is difference between interface and abstract class i.e is it possible to replace interface with abstract class?
We can replace interface with abstract class but it is not a good programming practice
this is something like recruiting an IAS officer for swiping activity.
if every thing is abstract then it is highly recommended to go for interface but not for abstract class.
1. While extending abstract class it is not possible to extends any other class and hence we are missing inheritance benefit.
while implementing interface we can extend some other class and hence we won't miss any inheritance benefit.
e.g Test t = new Test(); //2 minutes execution time;
Test t = new Test(); //2 seconds execution time.