+ 2
polymorphism vs overriding?
what is the difference between polymorphism and overriding in java? anyone could he explain to me this 🙏
6 Antworten
+ 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");
}}
+ 18
lessons exist here , teach yourself
https://www.sololearn.com/learn/Java/2164/?ref=app
https://www.sololearn.com/learn/Java/2165/?ref=app
+ 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.
+ 3
@Gawen thank you so much for all these details but could you please explain "overriding" for me?
+ 1
@gaurav thank you soo much, i really appreciate it 👌
0
i feel myself be like:
class me extends gawen {
public void teachme(){
while(true)
{
System.out.println("teach me more please");
}
}
}