polymorphism vs overriding? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

polymorphism vs overriding?

what is the difference between polymorphism and overriding in java? anyone could he explain to me this 🙏

27th Jan 2018, 9:25 AM
Hayyan Jarboue
Hayyan Jarboue - avatar
6 Answers
+ 6
In case of Inheritance, where Parent-child relationship exist,  Parent class define a method say connect() which does connection to some remote service.  Child class has better way to do connection than Parent class and don't want to use Parent class connect() method, So it overrides Parent's class connect() method by providing own implementation of connect() method, Now whenever connect() method is called using Child class object, then connect() method of Child class will be called and not the connect() method of Parent/Super class, this concept is called Method overriding. class Parent{ public void connect(){ System.out.println("Doing connection in Parent way"); }} class Child extends Parent { @Override public void connect() { System.out.println("Doing connection in Child way"); }}
27th Jan 2018, 10:05 AM
GAWEN STEASY
GAWEN STEASY - avatar
27th Jan 2018, 10:22 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 9
Polymorphism means "having many forms." In Java, it means that a single variable might be used with several objects of related classes at different times in a program. When the variable is used with "dot notation" variable.method()to invoke a method, exactly which method is run depends on the object that the variable currently refers to. Overloading is when two or more methods of a class have the same name but have different parameter lists. When a method is called, the correct method is picked by matching the actual parameters in the call to the formal parameter lists of the methods. Another use of the term "overloading" is when an operator calls for different operations depending on its operands. For example + can mean integer addition, floating point addition, or string concatenation depending on its operands.
27th Jan 2018, 9:37 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 3
@Gawen thank you so much for all these details but could you please explain "overriding" for me?
27th Jan 2018, 9:50 AM
Hayyan Jarboue
Hayyan Jarboue - avatar
+ 1
@gaurav thank you soo much, i really appreciate it 👌
27th Jan 2018, 10:37 AM
Hayyan Jarboue
Hayyan Jarboue - avatar
0
i feel myself be like: class me extends gawen { public void teachme(){ while(true) { System.out.println("teach me more please"); } } }
27th Jan 2018, 10:21 AM
Hayyan Jarboue
Hayyan Jarboue - avatar