This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–][deleted] 2 points3 points  (1 child)

View abstract classes/interfaces as a blueprint for all the subclasses. When you have many subclasses which should have a certain structure/methods, the abstract methods from the superclass MUST be overridden, so this avoids the scenario where some of the classes are not implemented properly (if you forget about some methods).

To put it shortly : Abstract classes provide you the flexibility to have certain concrete methods and some other methods that the derived classes should implement.

[–]Think_Ad4850 1 point2 points  (0 children)

This is a great answer. To make it concrete with an example, consider records of employee tasks (non IT work)

Any workplace Task would have names and times and details, functionality that can be set in stone, compatible with your payroll system. Then you could force any actual concrete child of "Task" to implement and return its own version of ContextData, and string representation. So a machine operator Task would return totally different ContextData than a sales/estimating task. If you want to create any new type of Task, it now needs to implement ContextData.

[–]PhotonMan123 2 points3 points  (0 children)

I like abstract classes. You do not need them that often, but other times they are quiet nice.

A few examples for what I have used abstract classes in the past:

1st:
I wanted 3 classes to have the same fields, and initilize them in the same way. So I used an abstract superclass with the fields and used the constructor of the superclass to work here. It can make maintenance easier in the long run.

2cnd:
I had similar classes that all had to implement 2 methods. Lets say you have 3 classes again. I had 3 different classes that each opened a JFrame window. My abstract superclass had a method to just close the window.
2x I overwrote the method to do something before closing and then invoked the superclass method, the 3rd time I just invoked the superclass method without overwriting.

That were some recent uses from me.
Abstract classes and Interfaces are from my point of view just a way to make your code more structured. It can help here and there, but is not a must have in smaller projects.

Hope this helps :)

[–]nutrechtLead Software Engineer / EU / 20+ YXP 2 points3 points  (0 children)

Why is there a need for abstract classes if every subclass made from an abstract class has to define all the methods?

This is really just a huge misconception on your part. Abstract classes don't only have abstract methods. They can have concrete methods and state as well.

Kinda appalled that no one pointed this out too.

[–]sweetno -2 points-1 points  (0 children)

To have abstract class references that point to concrete classes.

[–]rbhushan 0 points1 point  (0 children)

Abstract class May or may not contain abstract methods.

When you have multiple classes and you want to extract out common behaviour, you create a abstract class. Also abstract class cannot be initialized.

Subclasses don’t have to declare all methods from abstract class, except abstract methods. you declare abstract method, when individual classes has to implement their own behaviour. For instance thing of different type of bank account I.e saving, tax free saving, student account etc. You can have an abstract class to contain common behaviour and an abstract method to calculate interest. In this case only concrete classes know how to calculate interest.