Week 7 — What is meant by polymorphism?

Question of the Week #7
What is meant by polymorphism?
5 Replies
Mercy
Mercy2y ago
Polymorphism is one of the OOP principles in Java and allow the same action be executing different action depending on the object or class it is used in. If a parent class Animal eat(); method, and Dog, Cat,Mouse We achieve polymorphism by method overriding, where the method has the same keys, but have different code in the body of a method
Submission from svetlemk#3330
Mercy
Mercy2y ago
Polymorphism is a core concept of object-oriented programming and it basically means that one type of object/call may have different behaviours. For example, take the following classes:
public abstract class SomeClass{
public abstract void someMethod();
}
public class SomeChildClass extends SomeClass{
@Override
public void someMethod(){
System.out.println("behaviour of SomeChildClass");
}
}
public class OtherChildClass extends SomeClass{
@Override
public void someMethod(){
System.out.println("behaviour of OtherChildClass");
}
}
public abstract class SomeClass{
public abstract void someMethod();
}
public class SomeChildClass extends SomeClass{
@Override
public void someMethod(){
System.out.println("behaviour of SomeChildClass");
}
}
public class OtherChildClass extends SomeClass{
@Override
public void someMethod(){
System.out.println("behaviour of OtherChildClass");
}
}
It is possible to create store either of those objects in a variable of type SomeClass. Depending on the object the variable holds, something different happens when calling someMethod:
SomeClass theVariable;
theVariable = new SomeChildClass();
//theVariable = new OtherChildClass();
theVariable.someMethod();
SomeClass theVariable;
theVariable = new SomeChildClass();
//theVariable = new OtherChildClass();
theVariable.someMethod();
Java differentiates between compile-time polymorphism and runtime polymorphism. Compile-time polymorphism means the decision which method is to be called is made at compile-time. If a method is overloaded, the compiler decides which method is called: assuming there are two methods:
void a(Object o){
System.out.println("method with Object parameter called");
}
void a(String s){
System.out.println("method with String parameter called");
}
void a(Object o){
System.out.println("method with Object parameter called");
}
void a(String s){
System.out.println("method with String parameter called");
}
then it is possible to call them and depending on the parameter type, the compiler will decide on a method to be called:
a(new Object());//method with Object parameter called
a("Hello World");//method with String parameter called
a(new Object());//method with Object parameter called
a("Hello World");//method with String parameter called
This is only dependent on the type of the variable which is known at compile-time. It doesn't matter what the type of the actual object is at runtime:
Object o="Hello World";
a(o);//method with Object parameter called
Object o="Hello World";
a(o);//method with Object parameter called
Mercy
Mercy2y ago
On the other hand, runtime polymorphism means the the decision which method to call is made at runtime. In Java, this is the case with overriding (as in the example with SomeClass above).
SomeClass theVariable;
theVariable = new SomeChildClass();
//theVariable = new OtherChildClass();
theVariable.someMethod();//depending on the actual object assigned to theVariable, this will do something different
SomeClass theVariable;
theVariable = new SomeChildClass();
//theVariable = new OtherChildClass();
theVariable.someMethod();//depending on the actual object assigned to theVariable, this will do something different
⭐ Submission from dan1st#7327
Mercy
Mercy2y ago
Polymorphism means the ability for the same static type to have different implementations of the methods it has. To sum this concept up you could say: Same type, different behavior.
class Superclass {
public void foo () {
System.out.println("Hello, I'm the superclass!");
}
}

class Subclass1 {
@Overwrite
public void foo () {
System.out.println("Hello, I'm the subclass");
}
}

class Subclass2 {
@Overwrite
public void foo () {
// this one has no output :(
}
}
class Superclass {
public void foo () {
System.out.println("Hello, I'm the superclass!");
}
}

class Subclass1 {
@Overwrite
public void foo () {
System.out.println("Hello, I'm the subclass");
}
}

class Subclass2 {
@Overwrite
public void foo () {
// this one has no output :(
}
}
When we use this in the following way:
Superclass ex;

ex = new Superclass();
ex.foo(); // "Hello, I'm the superclass!"

ex = new Subclass1();
ex.foo(); // "Hello, I'm the subclass"

ex = new Subclass2();
ex.foo(); // no output
Superclass ex;

ex = new Superclass();
ex.foo(); // "Hello, I'm the superclass!"

ex = new Subclass1();
ex.foo(); // "Hello, I'm the subclass"

ex = new Subclass2();
ex.foo(); // no output
As we can see that even though we have the same contractual method on the same static type the behavior can vary depending on the very specific subclass implementation. We can lock this behavior down with the final modifier on a method.
Mercy
Mercy2y ago
Generally polymorphism means that even though we have the same type we can still achieve different behaviors in the subclass
⭐ Submission from jade#9418

Did you find this page helpful?