-4

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?

Oracle Docs - Abstract Class

and few more(but they are not so helpful)

PS : I am not referring to difference between Abstract Class and Interface

Community
  • 1
  • 1
bozzmob
  • 12,364
  • 16
  • 50
  • 73
  • 1
    So given that you're ok with base classes, the question is "why make a base class abstract?". Well sometimes you want to put abstract methods in it. Sometimes you don't want your base class directly instantiated. Trying to achieve the same thing with a combination of an interface and a concrete base class doesn't discount the usefulness of the abstract class. – khelwood Nov 01 '16 at 09:16
  • Sorry @RealSkeptic I have no attitude. I saw the down-votes even though it was a genuine question, even though I have told that its not the same old question of Abstract class vs Interfaces or whatever. I'm sorry. I never wanted to say so. But, people react too quickly than understanding what's exactly happening. – bozzmob Nov 01 '16 at 09:18
  • 1
    @bozzmob It's not that it's a dupe, though it might be, it's that is off topic on this site... – George Nov 01 '16 at 09:21
  • @RealSkeptic I did my research. I've read few 10's of posts on SO. I even gave an example to show what exactly is my doubt. Please suggest how can I improve the question. Should I add few links? But, they don't help in anyway :( – bozzmob Nov 01 '16 at 09:21
  • @RealSkeptic Agree with the reading help pages part of it. I'll close this myself. Thanks for the suggestions. – bozzmob Nov 01 '16 at 09:22
  • 1
    One crucial point is: when you start your question with "please downvote me" ... then probably something is wrong. If you had avoided that part; and just asked "what is the real benefit of having abstract classes" ... things might have gone were different. I still decided to answer, as I see some merit to the real question that you have hidden in your text; but you might still decide to edit your input dramatically. Maybe you regain some upvotes then. – GhostCat Nov 01 '16 at 09:24
  • 1
    @GhostCat he he. I got that point. – bozzmob Nov 01 '16 at 09:25
  • 1
    @RealSkeptic Is it better now? – bozzmob Nov 01 '16 at 09:32
  • @RealSkeptic Took care of it. – bozzmob Nov 01 '16 at 09:36
  • @bozzmob Please note: I guess you wont see much updates any more. So consider accepting that answer that was most helpful to you. Or the one answer you actually got ;-) – GhostCat Nov 02 '16 at 10:39
  • @GhostCat Yes :) Did it. – bozzmob Nov 03 '16 at 11:04
  • Would upvote ... again, but already did. Nice working with you ;-) – GhostCat Nov 03 '16 at 11:31

1 Answers1

3

An abstract class allows you to fix behavior, like in:

public abstract class Foo {

  protected abstract int getValue();

  public final void doTheFoo() {
    int value = getValue();
    ... do something ...
  }

By using such an approach you can guarantee that the behavior of doTheFoo() can't be changed by subclasses of Foo. But still you can extend Foo and allow for those subclasses to have a certain impact on what doTheFoo() will be doing.

That is something that you can't do with concrete classes + interfaces.

The other core aspect: keep in mind that such concepts are also a mean of communication. By having the abstract keyword, you can tell your "audience" that you do not want that class to be instantiated. If that is of value to you right now isn't the real point; as the concept was simply important enough for the fathers of Java to make it part of the language.

GhostCat
  • 137,827
  • 25
  • 176
  • 248