Algonquin College Virtual Methods Java in ObjectOriented Programming
Algonquin College Virtual Methods (Java) in Object-Oriented Programming (also call Polymorphism) Created by Rex Woollard Use Page. Up and Page. Down to move from screen to screen. Click on speaker to play sound.
Virtual Methods Vectored Graphics Draw. Obj: 1: Rectangle Vectored Drawing Programs Draw. Obj: 3: Line Visio Corel. Draw Adobe Illustrator drawing items stored as objects Draw. Obj: 4: Bessier Draw. Obj: 2: Ellipse
Virtual Methods Object-Oriented Draw. Obj: 1: Rectangle Vectored Drawing Programs Draw. Obj: 3: Line Visio Corel. Draw Adobe Illustrator Corel. Draw drawing. Illustrator Adobe items stored as objects drawing items stored as objects Redefine characteristics of an individual object Draw. Obj: 4: Bessier Draw. Obj: 2: Ellipse
Virtual Methods Derivation Draw. Obj: 1: Rectangle Draw. Obj: 3: Line Vectored Drawing Programs specific subclass types derived from fundamental abstract superclass Draw. Obj: 4: Bessier Draw. Obj: 2: Ellipse public abstract class Draw. Obj // Abstract Super Class int origin. X, origin. Y; public abstract void draw(); // no real code public class Ellipse extends Draw. Obj // Sub. Class int end. X, end. Y; public class Bessier extends Draw. Obj // Sub. Class Point[ ] ap. Array. Points; public void draw() // has real code public class Rectangle extends Draw. Obj // Sub. Class int end. X, end. Y; public void draw() // has real code
Virtual Methods Base Class Data Draw. Obj: 1: Rectangle Draw. Obj: 3: Line Vectored Drawing Programs specific subclass types derived from fundamental abstract superclass common fields in superclass Draw. Obj: 4: Bessier Draw. Obj: 2: Ellipse public abstract class Draw. Obj // Abstract Super Class int origin. X, origin. Y; public abstract void draw(); // no real code public class Ellipse extends Draw. Obj // Sub. Class int end. X, end. Y; public class Bessier extends Draw. Obj // Sub. Class Point[ ] ap. Array. Points; public void draw() // has real code public class Rectangle extends Draw. Obj // Sub. Class int end. X, end. Y; public void draw() // has real code
Virtual Methods Derived Class Data Draw. Obj: 1: Rectangle Draw. Obj: 3: Line Vectored Drawing Programs specific subclass types derived from fundamental abstract superclass common fields in superclass unique fields in subclass Draw. Obj: 4: Bessier Draw. Obj: 2: Ellipse public abstract class Draw. Obj // Abstract Super Class int origin. X, origin. Y; public abstract void draw(); // no real code public class Ellipse extends Draw. Obj // Sub. Class int end. X, end. Y; public class Bessier extends Draw. Obj // Sub. Class Point[ ] ap. Array. Points; public void draw() // has real code public class Rectangle extends Draw. Obj // Sub. Class int end. X, end. Y; public void draw() // has real code
Virtual Methods Real Draw Methods Draw. Obj: 1: Rectangle Draw. Obj: 3: Line Vectored Drawing Programs specific subclass types derived from fundamental abstract superclass common fields in superclass unique fields in subclass each object will need to draw itself Draw. Obj: 4: Bessier Draw. Obj: 2: Ellipse public abstract class Draw. Obj // Abstract Super Class int origin. X, origin. Y; public abstract void draw(); // no real code public class Ellipse extends Draw. Obj // Sub. Class int end. X, end. Y; public class Bessier extends Draw. Obj // Sub. Class Point[ ] ap. Array. Points; public void draw() // has real code public class Rectangle extends Draw. Obj // Sub. Class int end. X, end. Y; public void draw() // has real code
Virtual Methods Virtual Draw Function Draw. Obj: 1: Rectangle Draw. Obj: 3: Line Vectored Drawing Programs specific subclass types derived from fundamental abstract superclass common fields in superclass unique fields in subclass each object will need to draw itself superclass is abstract, no code for draw() method — draw() placeholder exists for virtual function organization Draw. Obj: 4: Bessier Draw. Obj: 2: Ellipse public abstract class Draw. Obj // Abstract Super Class int origin. X, origin. Y; public abstract void draw(); // no real code public class Ellipse extends Draw. Obj // Sub. Class int end. X, end. Y; public class Bessier extends Draw. Obj // Sub. Class Point[ ] ap. Array. Points; public void draw() // has real code public class Rectangle extends Draw. Obj // Sub. Class int end. X, end. Y; public void draw() // has real code
Virtual Methods Virtual Draw Function Draw. Obj: 1: Rectangle Draw. Obj: 3: Line But How Does It Work in Program Code? Draw. Obj: 4: Bessier Draw. Obj: 2: Ellipse public abstract class Draw. Obj // Abstract Super Class int origin. X, origin. Y; public abstract void draw(); // no real code public class Ellipse extends Draw. Obj // Sub. Class int end. X, end. Y; public class Bessier extends Draw. Obj // Sub. Class Point[ ] ap. Array. Points; public void draw() // has real code public class Rectangle extends Draw. Obj // Sub. Class int end. X, end. Y; public void draw() // has real code
Non-Virtual Methods // Assume array of references to objects exists Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; // Function to Draw Objects to Screen void draw. Screen() { for (int i = 0; i < num. Objects; ++i) { switch (active. Objects[i]. get. Type()) { case ELLIPSE: active. Objects[i]. draw. Ellipse(); break; case BESSIER: active. Objects[i]. draw. Bessier(); break; case RECTANGLE: active. Objects[i]. draw. Rectangle(); break; : : // hundreds of additional cases ! ! ! : } // end switch } // end for } // end function draw. Screen() Draw. Obj: 1: Rectangle Draw. Obj: 3: Line Draw. Obj: 4: Bessier Draw. Obj: 2: Ellipse
Non-Virtual Methods Memory 1 // Assume array of references to objects exists Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; // Function to Draw Objects to Screen void draw. Screen() { for (int i = 0; i < num. Objects; ++i) { switch (active. Objects[i]. get. Type()) { case ELLIPSE: active. Objects[i]. draw. Ellipse(); break; case BESSIER: active. Objects[i]. draw. Bessier(); break; case RECTANGLE: active. Objects[i]. draw. Rectangle(); break; : : // hundreds of additional cases ! ! ! : } // end switch } // end for } // end function draw. Screen() Draw. Obj: 1: Rectangle Draw. Obj: 3: Line Draw. Obj: 4: Bessier active. Objects Draw. Obj: 2: Ellipse
Non-Virtual Methods Memory 2 // Assume array of references to objects exists Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; // Function to Draw Objects to Screen void draw. Screen() { for (int i = 0; i < num. Objects; ++i) { switch (active. Objects[i]. get. Type()) { case ELLIPSE: active. Objects[i]. draw. Ellipse(); break; case BESSIER: active. Objects[i]. draw. Bessier(); break; Dynamically allocated array of references to case RECTANGLE: Draw. Obj objects. active. Objects[i]. draw. Rectangle(); break; : : // hundreds of additional cases ! ! ! : } // end switch } // end for } // end function draw. Screen() Draw. Obj: 1: Rectangle Draw. Obj: 3: Line Draw. Obj: 2: Ellipse Draw. Obj: 4: Bessier class Draw. Obj // Abstract Base Class int origin. X, origin. Y; virtual Draw() = 0; // no function class Ellipse : public Draw. Obj // Derived Class int end. X, end. Y; class Bessier : public Draw. Obj // Derived Class Point ap. Array. Points[]; Draw(); // contains real code class Rectangle : public Draw. Obj // Derived Class int end. X, end. Y; Draw(); // contains real code
Non-Virtual Methods Memory 3 // Assume array of references to objects exists Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; // Function to Draw Objects to Screen void draw. Screen() { for (int i = 0; i < num. Objects; ++i) { switch (active. Objects[i]. get. Type()) { case ELLIPSE: active. Objects[i]. draw. Ellipse(); break; case BESSIER: active. Objects[i]. draw. Bessier(); break; Dynamically allocated array of references to case RECTANGLE: Draw. Obj objects. active. Objects[i]. draw. Rectangle(); break; : : // hundreds of additional cases ! ! ! : } // end switch } // end for } // end function draw. Screen() Draw. Obj: 1: Rectangle Draw. Obj: 3: Line Draw. Obj: 4: Bessier Draw. Obj: 2: Ellipse Rectangle Line Ellipse Bessier
Non-Virtual Methods Drawing Old Way // Assume array of references to objects exists Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; // Function to Draw Objects to Screen void draw. Screen() { for (int i = 0; i < num. Objects; ++i) { switch (active. Objects[i]. get. Type()) { case ELLIPSE: active. Objects[i]. draw. Ellipse(); break; case BESSIER: active. Objects[i]. draw. Bessier(); break; Dynamically allocated array of references to case RECTANGLE: Draw. Obj objects. active. Objects[i]. draw. Rectangle(); break; : : // hundreds of additional cases ! ! ! : } // end switch } // end for } // end function draw. Screen() Draw. Obj: 1: Rectangle Draw. Obj: 3: Line Draw. Obj: 4: Bessier Draw. Obj: 2: Ellipse Rectangle Line Ellipse Bessier
Non-Virtual Methods Drawing Old Way // Assume array of references to objects exists Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; // Function to Draw Objects to Screen void draw. Screen() { for (int i = 0; i < num. Objects; ++i) { switch (active. Objects[i]. get. Type()) { case ELLIPSE: active. Objects[i]. draw. Ellipse(); break; case BESSIER: active. Objects[i]. draw. Bessier(); break; Dynamically allocated array of references to case RECTANGLE: Draw. Obj objects. active. Objects[i]. draw. Rectangle(); break; : : // hundreds of additional cases ! ! ! : } // end switch Similar Code Elsewhere } // end for resize } // end function draw. Screen save to disk read from disk change line colour, fill colour, pattern etc. Draw. Obj: 1: Rectangle Draw. Obj: 3: Line Draw. Obj: 4: Bessier Draw. Obj: 2: Ellipse Rectangle Line Ellipse Bessier
Virtual Methods Using Virtual Draw. Obj: 1: Rectangle // Assume array of references to objects exists Draw. Obj: 3: Line Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; // Function to Draw Objects to Screen void draw. Screen() { for (int i = 0; i < num. Objects; ++i) { Draw. Obj: 2: Ellipse Draw. Obj: 4: Bessier switch (active. Objects[i]. get. Type()) { (active. Objects[i case ELLIPSE: active. Objects[i]. draw. Ellipse(); public abstract class Draw. Obj break; // Abstract Super Class int origin. X, origin. Y; case BESSIER: public abstract void draw(); // no real code active. Objects[i]. draw. Bessier(); break; public class Ellipse extends Draw. Obj public class Bessier extends Draw. Obj case RECTANGLE: // Sub. Class int end. X, end. Y; Point[ ] ap. Array. Points; active. Objects[i]. draw. Rectangle(); public void draw() // has real code break; : public class Rectangle extends Draw. Obj : // hundreds of additional cases ! ! ! // Sub. Class int end. X, end. Y; : public void draw() // has real code // Assume array of references to objects exists } // end switch Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; } // end for // Function to Draw Objects to Screen } // end function draw. Screen void draw. Screen() { for (int i = 0; i < num. Objects; ++i) active. Objects[i]. draw (); } // end function draw. Screen() Using Virtual Methods
Virtual Methods Adding New Types Draw. Obj: 1: Rectangle // Assume array of references to objects exists Draw. Obj: 3: Line Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; // Function to Draw Objects to Screen void draw. Screen() { for (int i = 0; i < num. Objects; ++i) { Draw. Obj: 2: Ellipse Draw. Obj: 4: Bessier switch (active. Objects[i]. get. Type()) { (active. Objects[i case ELLIPSE: active. Objects[i]. draw. Ellipse(); public abstract class Draw. Obj break; // Abstract Super Class int origin. X, origin. Y; case BESSIER: public abstract void draw(); // no real code active. Objects[i]. draw. Bessier(); break; public class Ellipse extends Draw. Obj public class Bessier extends Draw. Obj case RECTANGLE: // Sub. Class int end. X, end. Y; Point[ ] ap. Array. Points; active. Objects[i]. draw. Rectangle(); public void draw() // has real code break; : public class Rectangle extends Draw. Obj : // hundreds of additional cases ! ! ! // Sub. Class int end. X, end. Y; : public void draw() // has real code // Assume array of references to objects exists } // end switch Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; } // end for // Function to Draw Objects to Screen } // end function draw. Screen public class Image extends Draw. Obj void draw. Screen() // Sub. Class String s. File. Name; { public void draw() // has real code for (int i = 0; i < num. Objects; ++i) active. Objects[i]. draw (); } // end function draw. Screen() Add a new class type to draw images stored in files
Virtual Methods How Many Changes? Draw. Obj: 1: Rectangle // Assume array of references to objects exists Draw. Obj: 3: Line Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; // Function to Draw Objects to Screen void draw. Screen() { for (int i = 0; i < num. Objects; ++i) { Draw. Obj: 2: Ellipse Draw. Obj: 4: Bessier switch (active. Objects[i]. get. Type()) { (active. Objects[i case ELLIPSE: active. Objects[i]. draw. Ellipse(); public abstract class Draw. Obj break; // Abstract Super Class int origin. X, origin. Y; case BESSIER: public abstract void draw(); // no real code active. Objects[i]. draw. Bessier(); break; public class Ellipse extends Draw. Obj public class Bessier extends Draw. Obj case RECTANGLE: // Sub. Class int end. X, end. Y; Point[ ] ap. Array. Points; active. Objects[i]. draw. Rectangle(); public void draw() // has real code break; case IMAGE: public class Rectangle extends Draw. Obj active. Objects[i]. draw. Image(); // Sub. Class int end. X, end. Y; break; public void draw() // has real code // Assume array of pointers to objects exists : Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; : // hundreds of additional cases !!!!! // Function to Draw Objects to Screen : public class Image extends Draw. Obj void draw. Screen() // Sub. Class } // end switch String s. File. Name; { } // end for public void draw() // has real code for (int i = 0; i < num. Objects; ++i) } // end function draw. Screen() active. Objects[i]. draw (); } // end function draw. Screen() Old Way Add Code in Dozens of Locations
Virtual Methods How Many Changes? Draw. Obj: 1: Rectangle // Assume array of references to objects exists Draw. Obj: 3: Line Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; // Function to Draw Objects to Screen void draw. Screen() { for (int i = 0; i < num. Objects; ++i) { Draw. Obj: 2: Ellipse Draw. Obj: 4: Bessier switch (active. Objects[i]. get. Type()) { (active. Objects[i case ELLIPSE: active. Objects[i]. draw. Ellipse(); public abstract class Draw. Obj break; // Abstract Super Class int origin. X, origin. Y; case BESSIER: public abstract void draw(); // no real code active. Objects[i]. draw. Bessier(); break; public class Ellipse extends Draw. Obj public class Bessier extends Draw. Obj case RECTANGLE: // Sub. Class int end. X, end. Y; Point[ ] ap. Array. Points; active. Objects[i]. draw. Rectangle(); public void draw() // has real code break; : public class Rectangle extends Draw. Obj : // hundreds of additional cases ! ! ! // Sub. Class int end. X, end. Y; : public void draw() // has real code // Assume array of references to objects exists } // end switch Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; } // end for // Function to Draw Objects to Screen } // end function draw. Screen public class Image extends Draw. Obj void draw. Screen() // Sub. Class String s. File. Name; { public void draw() // has real code for (int i = 0; i < num. Objects; ++i) active. Objects[i]. draw (); } // end function draw. Screen() Using Virtual Methods No Change
Virtual Methods Early Binding // Assume array of references to objects exists Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; // Function to Draw Objects to Screen void draw. Screen() { for (int i = 0; i < num. Objects; ++i) { switch (active. Objects[i]. get. Type()) { case ELLIPSE: active. Objects[i]. draw. Ellipse(); break; case BESSIER: active. Objects[i]. draw. Bessier(); break; Dynamically allocated array of references to case RECTANGLE: Draw. Obj objects. active. Objects[i]. draw. Rectangle(); break; : : // hundreds of additional cases ! ! ! : } // end switch } // end for } // end function draw. Screen() Each function call implemented with a specific address following compilation and linking Draw. Obj: 1: Rectangle Draw. Obj: 3: Line Draw. Obj: 4: Bessier Draw. Obj: 2: Ellipse Rectangle Line Ellipse Bessier
Virtual Methods Late Binding Draw. Obj: 1: Rectangle // Assume array of references to objects exists Draw. Obj: 3: Line Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; // Function to Draw Objects to Screen void draw. Screen() { for (int i = 0; i < num. Objects; ++i) { Draw. Obj: 2: Ellipse Draw. Obj: 4: Bessier switch (active. Objects[i]. get. Type()) { (active. Objects[i case ELLIPSE: active. Objects[i]. draw. Ellipse(); class Draw. Obj break; // Abstract Base Class int origin. X, origin. Y; case BESSIER: virtual Draw() = 0; // no function active. Objects[i]. draw. Bessier(); break; case RECTANGLE: class Ellipse : public Draw. Obj class Bessier : public Draw. Obj // Derived Class active. Objects[i]. draw. Rectangle(); int end. X, end. Y; Point[ ] ap. Array. Points; break; Draw(); // contains real code : : // hundreds of additional cases ! ! ! class Rectangle : public Draw. Obj // Derived Class : int end. X, end. Y; // Assume array of references to objects exists } // end switch Draw(); // contains real code Draw. Obj[ ] active. Objects = new Draw. Obj[1000]; } // end for // Function to Draw Objects to Screen } // end function draw. Screen void draw. Screen() { for (int i = 0; i < num. Objects; ++i) active. Objects[i]. draw (); } // end function draw. Screen() Using Virtual Methods Method call implemented using virtual tables. Address cannot be known at compile-link time. Address resolved at run-time.
- Slides: 21