Very helpful tut!
I would like to ask, do I have the correct understanding of polymorphism? Here's what I understand:
first, I create a class called Human, which has a generic talk() method:
I can then create EXTENSIONS of the Human class, which override the talk() method in Human.java... This Child class does this:Code:public class Human { public void talk() { System.out.println("I'm a generic Human!"); } }
& just for good measure here's an Oldie class:Code:public class Child extends Human { public void talk() { System.out.println("I'm a Child Human!"); } }
OK! Now we're ready to use our polymorphed Human classes in our program... Here's the example:Code:public class Oldie extends Human { public void talk() { System.out.println("I'm an ancient Human!"); } }
Again, thanks for the tutorial, it was very instructive!Code:public class Tester { public static void main(String[] args) { Human[] myPeople = new Human[3]; myPeople[0] = new Human(); myPeople[1] = new Child(); myPeople[2] = new Oldie(); // lets test their overloaded methods for (int i = 0; i < myPeople.length; i++) { myPeople[i].talk(); } } // end main }
Yes, your understanding seems to be correct.
Glad it came to hands![]()
Nice job turk4n +rep
may i know what package is and what is the function??
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks