interface vs abstract class

interface vs abstract class

 

  • 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



Difference b/w interface and Abstract class.

interface

abstract

If we don’t know anything about the

implementation and have requirement

specifications then we should go for the

interface.

If we are talking about implementation but not

completely (partial implementation )

then we should go for an abstract class.

Inside the interface, every method is always

public and abstract whether we are declaring

or not. hence interface considers a 100%

pure abstract class.

Every method present in the abstract

class need not be public and abstract and

we can take concrete methods also.

As every interface method is always

public and abstract and hence we can’t

declare with the following modifiers

private, protected, final, static,

synchronized, native, and strictfp.

There are no restrictions on abstract

class method modifiers.

Every variable present inside the interface is

always public static final whether we are declaring or not

Every variable present inside the abstract

class need not be public, static,  final.

As every interface variable is always public

static, we can’t declare private, protected, volatile, and transient with the following modifier.

There are no restrictions on abstract

class variable modifiers.

For the interface variable compulsory we

should perform an initialization at the time

of declaration only. otherwise, we will get

compile time error.

For abstract class variables, we are not

required to perform initialization at the time

of declaration.

Inside the interface, we can’t declare static and instance blocks.

Inside the abstract class, we can declare

static and instance blocks.

Inside the interface, we can’t declare

constructors.

Inside the abstract class, we can declare

constructors.


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;
}
}
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);
}
}



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;
     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;
}
}
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);
}
}


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.

interface

abstract

Interface X{

}

abstract class X{

}

class  Test implements X{

}

class Test extends X{

}



While implementing the interface we can extend some other classes and hence we won’t miss any inheritance benefit.

While extending the abstract class it is not possible to extend any other class and hence we are missing the inheritance benefit

In this case, object creation is not costly

Test t=new Test() //2 second

In this case, object creation is costly

Test t =new Test()// 2minute