When someone asked me what polymorphism is... Can overloading or overriding be the answer to that?
I think polymorphism needs to be added more than that. The one defined without the implementation of the method is called an abstract class And if you implement those methods in the lower class, is this also an override?
I don't think overloading is the right answer.
oop java
The clearest way to express polymorphism is through abstraction classes.
public abstract class Human{
...
public abstract void goPee();
}
This class is called an abstract class. Because in Human class, the method goPee() is not implemented.
It's only defined in subclasses like Male and Female. Human is an abstract class.
So unlike Male and Female, human cannot create objects.
public class Male extends Human{
...
@Override
public void goPee(){
System.out.println("Stand Up");
}
}
public class Female extends Human{
...
@Override
public void goPee(){
System.out.println("Sit Down");
}
}
So we can implement Male and Female in this way. Polymorphism here is
public static void main(String[] args){
ArrayList<Human> group = new ArrayList<Human>();
group.add(new Male());
group.add(new Female());
// ... add more...
// // tell the class to take a pee break
for (Human person : group) person.goPee();
}
In this way, the data type of the group is Human, but the form of goPee() changes depending on which data type you create the object It's polymorphism.
617 Uncaught (inpromise) Error on Electron: An object could not be cloned
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
581 PHP ssh2_scp_send fails to send files as intended
578 Understanding How to Configure Google API Key
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
© 2024 OneMinuteCode. All rights reserved.