My doubt or argument is-
An Abstract class can be replaced by extending a Concrete Class and implementing an Interface.
How or What?
What does an Abstract class contain? Methods that implement some default behaviour. Different access modifiers. Can extend another abstract class. Can be Instantiated. and so on...
All of them can be achieved using a Concrete class. When there is a need to maintain a structure and no implementation, we can use an interface.
I can see Abstract Class as a combination of a Concrete Class & Interface.
To show some code-
public abstract class MobilePhone {
abstract public String getPhoneModel();
public String getIMEI(){
// Consider execute() is some function that returns us IMEI for now
return execute("*#06#");
}
}
public class SamsungPhone extends MobilePhone {
public String getPhoneModel(){
String imei = displayIMEI();
return getSamsungModelFromDB(imei);
}
}
public class iPhone extends MobilePhone {
public String getPhoneModel(){
String imei = displayIMEI();
return getiphoneModelFromDB(imei);
}
}
The same thing can be achieved if we do something like this-
public class MobilePhone {
public String getIMEI(){
// Consider execute() is some function that returns us IMEI for now
return execute("*#06#");
}
}
public interface Imobile {
String getPhoneModel();
}
public class SamsungPhone extends MobilePhone implements Imobile {
public String getPhoneModel(){
String imei = displayIMEI();
return getSamsungModelFromDB(imei);
}
}
public class iPhone extends MobilePhone implements Imobile {
public String getPhoneModel(){
String imei = displayIMEI();
return getSamsungModelFromDB(imei);
}
}
What is that special use-case or need for an abstract class?
References-
When to use an interface instead of an abstract class and vice versa?
When do I have to use interfaces instead of abstract classes?
How should I have explained the difference between an Interface and an Abstract class?
and few more(but they are not so helpful)
PS : I am not referring to difference between Abstract Class and Interface