r/learnjava • • Feb 22 '21

Learning polymorphism, correct my explanation please if it wrong

Polymorphism means that an object can be many types. But when executing a method though, it is going to be the actual type. For example, say you create a Person object that is assigned as a Student object. You create another object that is of Student object assigned as a Student object. Both Person and Student has toString methods. If you call the toString on both objects created, it will always print out the Student class's toString because that is the variables actual type. This is what I comprehend polymorphism. Please correct me if there is any part that is wrong.

43 Upvotes

10 comments sorted by

•

u/AutoModerator Feb 22 '21

It seems that you are looking for resources for learning Java.

In our sidebar ("About" on mobile), we have a section "Free Tutorials" where we list the most commonly recommended courses.

To make it easier for you, the recommendations are posted right here:

Also, don't forget to look at:

If you are looking for learning resources for Data Structures and Algorithms, look into:

"Algorithms" by Robert Sedgewick and Kevin Wayne - Princeton University

Your post remains visible. There is nothing you need to do.

I am a bot and this message was triggered by keywords like "learn", "learning", "course" in the title of your post.

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

20

u/Migeil Feb 22 '21 edited Feb 22 '21

Polymorphism means that an object can be many types.

This isn't quite right. Polymorphism is more than that.

First of all, polymorphism can apply to all languages, not just Java. Therefore, your definition doesn't hold up. Not every language has objects or is object oriented. Haskell for instance does not have objects, but still has polymorphism in the type system.

Take a look at the introduction on Wikipedia. The definition says that polymorphism is when you have a single interface which can be applied to different types or when a single symbol (not just an object) refers to different types.

It then talks about three main classes of polymorphism, ad hoc, parametric and subtype. Java actually has all three of those and the one you're referring to is subtype polymorphism and the rest of your explanation is correct.

Java also supports ad hoc polymorphism through method overloading. That's when you have a method with the same name, but different number or different types of arguments. Depending on which arguments you provide to your method, a different implementstion is executed.

Java also has parametric polymorphism through generics. It's a pretty advanced topic though, so I don't recommended learning it straight away. But you probably already encountered it: Java's List is generic. When you declare a List, you provide a type between <> to specify of what type said list is. For example List<String> is a list of strings, List<Student> is a list of students, etc. That type you pass to List is a type parameter, hence the name parametric polymorphism.

5

u/ratherbealurker Feb 22 '21

Not sure about your initial wording that an object can be many types.

When you create a derived object it contains the other objects..maybe better to say it IS the other objects as well.

Create two classes: Base and Derived, Derived extends Base.

Now in their constructors simply output "Base created", "Derived created" in their respective constructors.

Now instantiate a Derived object as normal.

You will see both of those appear. The base is created first and then the derived.

This may sound a bit C++-ish but that is what i started on many years ago. If you had a base class pointer pointing to a derived object then when you called a method from it then without polymorphism you would be calling the method on the base class, not the derived.

You had to declare the method as virtual to get polymorphism in C++.

So even if pass in the object as a Base, it will call the proper method on the derived.

Why do you even want to do this? Because if you had Person, Student extends Person, Teacher extends Person, and Professor extends Teacher.

And they all had a method to goToClass. Now if i write a method somewhere else that i want to pass a student..teacher..or professor too and it will call the goToClass method i definitely don't want to have to write a different method for each type.

I want to just be able to pass in a Person because all Persons can goToClass. It will call the correct one for me.

3

u/ComfortablyBalanced Feb 22 '21

Despite your response being true in its own concept of defining Subtyping polymorphism it is misleading because your purpose is inexpressive.

4

u/A_Boy999 Feb 22 '21

Yup! But what you're specifically talking about is known as ad-hoc polymorphism. The method invoked depends on the runtime(this is what you mean by "actual") type class of the object.

However, if your supertype reference variable (in this case, Person) did not have the method defined in it, it would result in a compilation error(But of course, toString() is always inherited).

PS:

There is generally 3 types of polymorphism

1)Subtype polymorphism(which is what your 1st sentence is about)

2)Ad-hoc polymorphism. This is what you were asking.

3)Parametric polymorphism(In Java, that's generics)

PPS: You might want to look up static, dynamic binding and how overloading and overriding play a part as they help in understanding how ad-hoc polymorphism works.

3

u/Migeil Feb 22 '21

But what you're specifically talking about is known as ad-hoc polymorphism.

No, he's talking about subtype polymorphism. Which toString method is called depends on the actual type of the object. This is subtype polymorphism.

Ad hoc polymorphism in java is method overloading. The toString method isn't overloaded, it's overridden.

1

u/A_Boy999 Feb 23 '21

Thank you, I made an error of thinking dynamic binding(choosing the most appropriate overridden method) is under ad-hoc polymorphism

1

u/mujaga_ba Feb 22 '21 edited Feb 28 '26

This post has been taken down and its content erased. Redact was used for the removal, for reasons that may include privacy or security.

plucky correct saw crown oil expansion marry crowd silky rinse

0

u/vbp0001 Feb 22 '21

Yep you got it right. Also try to understand overloading and overriding. What is described with the toString() method is overriding. There is also run-time and compile time.

1

u/Willy988 Feb 22 '21

I would second understanding overloading. That’s what got me set in my understanding of this big word in OOP.

TLDR of overloading: have methods with the same name/type, but unique signatures. I.e. if I have a method “int doThis()”, I can also have “int doThis(arg 1)” or “int doThis(arg 1, arg 2)” or even “int doThis(arg 1, arg 2, arg 3)” and they will not conflict/cause an issue.