1 / 25

Object Oriented Programming

Object Oriented Programming. Polymorphism Method overriding Method overloading Dynamic binding. Introduction to polymorphism. Polymorphism is the ability to take more than one form

chad
Download Presentation

Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming Polymorphism Method overriding Method overloading Dynamic binding

  2. Introduction to polymorphism • Polymorphism is the ability to take more than one form • Earlier on we discussed polymorphism through operator overloading where an operator like “+” can be used for math addition, string concatenation, and for matrix addition. • This is equivalent to a word having different meanings depending on the context

  3. Introduction to polymorphism – cont’d • Polymorphism enables objects with different internal structures to share the same external interface • This means same interface may be accessed by different classes that work differently as shown in figure below:

  4. <Shape> Circle Rectangle Triangle Draw() Draw() Draw() Draw() Fig: Polymorphism

  5. Introduction to polymorphism – cont’d • As we saw earlier, Java doesn’t implement operator overloading except the “+” operator • This lecture discusses how to implement polymorphism using method overriding and method overloading.

  6. Method overriding • Overriding means redefining a method of a class in its subclass • The method name and signature (number and type of parameters it requires) remain unchanged i.e. only the method body changes

  7. Example: Overriding public class Super { int x; Super(int x) { this.x=x; } void display() { System.out.println("Super x = "+x); } }

  8. Example: Overriding public class Sub extends Super { int y; Sub (int x, int y) { super(x); this.y = y; } void display() { System.out.println("Super x = "+ x); System.out.println("Sub y = "+ y); } }

  9. Example: Overriding public class OverrrideTest { public static void main(String args[]) { Sub a = new Sub(100,200); a.display(); } }

  10. Example: Overriding • We use this keyword to access a variable of the current class • We use the inbuilt super method to access a variable of the super class • In the above example, the Sub class display() method overrides the Super class display() method • If we call the Sub class, we get a different behavior from that of the Super class

  11. Example: Overriding in Applets • Applets always inherit from the Applet class • This class has methods such as init(), start(), stop(), destroy • These methods are available for use by applets to give them their runtime behavior

  12. Example: Overriding in Applets • Applets are good examples of both the inheritance and the polymorphism principles. • To make an applet unique, various life cycle methods are overridden as shown in example below

  13. Example: Overriding in Applets import java.awt.*; import java.applet.*; public class NestedApplet extends Applet { // display params int width=400; int height=200; int level=100; int inc=10; // first override the life cycle methods public void init() { System.out.println("Initializing"); incNesting(); }

  14. Example: Overriding in Applets public void start() { System.out.println("Starting."); incNesting(); } public void stop() { System.out.println("Stopping."); incNesting(); } public void destroy() { System.out.println("Shutting down."); incNesting(); }

  15. Example: Overriding in Applets public void paint(Graphics g) { int i, shift=0; g.setColor(Color.blue); for (i=0;i<level;i++) { g.drawRect(shift,shift,width-2*shift-1,height-2*shift-1); shift = shift + inc; } g.drawString("Nesting level = "+level,width/2-50,height/2+5); } public void incNesting() { level++; repaint(); } }

  16. Overloading • Overloading is another technique to implement polymorphism • Here the method name is retained, but the signature changes • Used when a method is required to perform similar tasks but using different input parameters

  17. Overloading – cont’d • When we call a method, Java matches the • Method name, • Number of parameters, and • Type of parameters • The matching method is then executed

  18. Overloading – cont’d • Overloading does not require inheritance • All we need to do is define several versions of the same method • Defined methods should have different number and type of parameters • Change their body definition as appropriate • We can overload both constructors and ordinary methods

  19. Overloading – cont’d • In the example below, the constructor method is overloaded such that one takes one integer parameter and the other takes two integer parameters • The matching will be done at runtime, depending on how many parameters you supply

  20. Example: Overloading class Room { int length, breadth; Room(int x, int y) { length = x; breadth = y; } Room(int x) { length = breadth=x; } int area() { return(length*breadth); } }

  21. Example: Overloading class RectangleSquare { public static void main(String args[]) { Room room1 = new Room(25, 15); Room room2 = new Room(20); int area1 = room1.area(); int area2 = room2.area(); System.out.println("Area of Rectangle = "+area1); System.out.println("Area of Square = "+area2); } }

  22. Overloading vs. Overriding • Don't confuse the concepts of overloading and overriding • Overloading deals with multiple methods in the same class with the same name but different signatures • Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature • Overloading lets you define a similar operation in different ways for different data • Overriding lets you define a similar operation in different ways for different object types

  23. Dynamic Binding • Binding is the • Linking of a procedure call to the code to be executed in response to the call • Dynamic binding is the • The code to be executed is not known until the time of the call at runtime • Associated with polymorphism and inheritance.

  24. Example: Dynamic Binding Shape Circle Object Box Object Rectangle Object Draw() Draw(Circle) Draw(Box) Draw(Rectangle)

  25. Dynamic binding example • By inheritance, • Each of the object will invoke the draw procedure • The algorithm will however be known at runtime depending on whether the user wants to draw circle or box or rectangle • Thus, at runtime • The code matching the object under current reference will be called.

More Related