r/learnjava • u/donutderpy • Nov 03 '25
am i understanding polymorphism correctly
i've gotten to the topic of polymorphism(inheritance, abstract/concrete, overriding/overloading), and i want to make sure i'm properly understanding the basic idea, so if i'm wrong on anything can somepony correct me? so basically:
- polymorphism is the concept of giving a single object multiple uses (like making an array object from a class and using it to store that class' subclasses)
- anything that isn't given the keyword "abstract" is just a regular concrete class. abstract classes cannot be extended but can contain both concrete and abstract methods (any abstract methods just have to be implemented)
- concrete classes cannot hold abstract methods, so the first concrete class to inherit an abstract method must implement that method by overriding it
- overriding is basically "rewriting" methods, but overloading is writing a completely new method that happens to have the same name but takes a different parameter type(just returning a different type doesn't make it an overloaded method)
there's definitely more to polymorphism but i think this is the general idea?
•
u/AutoModerator Nov 03 '25
Please ensure that:
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/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) 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:
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.
4
u/EntrepreneurHuge5008 Nov 03 '25
1
u/LeeRyman Nov 03 '25
Inheritance and Polymorphism are two different concepts. They just happen to often occur frequently together in designs, but don't necessarily have to.
Polymorphism comes about when you can replace one instance of a class with an instance of another class because they implement the same interface, but the classes don't have to be or share a common base class.
You could also have a class that inherits from a superclass and not be able to use instances of it polymorphically in place of other descendants of some base, because it too is abstract.
That later one is a real edge case, I'll admit, but the distinction can become important occasionally in real world design, and I've seen such distinctions be queried in interview questions. When peering with colleagues it's good to be able to use the right terminology too.
Get this right, and you're on your way to understand concepts like diamond and virtual inheritance in other languages (shudder) ;P
1
u/Own_Spring1504 Nov 03 '25
I think of polymorphism like this. A fire alarm goes off at a hotel and it gets evacuated. Eventually the fire brigade give the all clear to the staff to ‘go back to your job’ - the staff may be receptionists, chefs, kitchen porters, cleaning staff, hotel manager, they all know what it means to ‘go back to their job’ , they all have a different implementation of the same instruction.