Intro to Java Web-Notes_Part69

Intro to Java Web-Notes_Part69 - class Simple Make the the...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } class Simple // Make the the super class to two subclasses { public void run() { System.out.println("Running, running."); } } This is a Visualization of the object created by SimpleApp: If the run method is defined in a super class, and that class has a couple of subclasses (for example, SubA and SubB) and the subclasses override the run method: rum} : void rant}, : void “mil 33 void The code below implements the classes SubA and SubB: public class SimpleApp { public static void main(String args) { Simple ref : new Simple(); ref.run(); SubA sa = new SubA(); sa.run(); Simple core : (Simple) new SubA(); core.run(); } class Simple // Make the the super class to two subclasses { public void run() { System.out.println("Running, running."); } } class SubA extends Simple { public void run() { System.out.println("AAAAAaaaaaaaa"); } } class SubB extends Simple { public void run() { System.out.println("B!"); } In the code shown above, two SubA objects are created. One is referenced by a SubA reference sa and the other by a Simple reference core. Both references are used to call the run method. The call sa.run() directly calls the SubA version of run. The call c0re.run() initially references the run method in Simple, but since the run method has been overridden in the subclass SubA, the rules of polymorphism dictate that the subclass's run method get called (i.e., this also runs the SubA run method). Therefore, both sa.run() and c0re.rzm() generate the output: AAAAAaaaaaaaa The term polymorphism means "many shapes". One superclass reference variable can possibly reference more than one kind of subclass object, and thus be used to call different instances of a given method (such as the run method above) depending on which subclass object is currently being referenced. For example, in the code shown below, a random number generator is used to determine whether the reference ref will be referencing a SubA object or a SubB object: public class SimpleApp { public static void main(String args) { Simple ref; // refrence to the super class Simple if (Math.random() < 0.5) ref = new SubA(); else ref : new SubB(); ref.run(); // What is printed here??? } } class Simple // Make the the super class to two subclasses { public void run() { System.out.println("Running, running."); } } class SubA extends Simple { public void run() { ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern