polymorphism
play

Polymorphism Object-oriented programming Inf1 :: 2008 - PowerPoint PPT Presentation

Polymorphism Polymorphism Object-oriented programming Inf1 :: 2008 Object-oriented programming Polymorphism Polymorphism Overview Polymorphism is a concept that allows the programmer to program abstractly Objects of a subclass can be treated


  1. Polymorphism Polymorphism Object-oriented programming Inf1 :: 2008 Object-oriented programming Polymorphism

  2. Polymorphism Overview Polymorphism is a concept that allows the programmer to program abstractly Objects of a subclass can be treated as objects of its superclass(es) The base class of a hierarchy is usually an abstract class Allows for hierarchies to be highly extensible New classes can be introduced and can still be processed without changing other parts of the program Object-oriented programming Polymorphism

  3. Polymorphism Abstract classes Abstract classes define the common behaviour (functionality) of the hierarchy — also known as an interface Subclasses inherit this behaviour and extend it according to their own properties by overriding the methods of the superclass Key concept : subclass objects can be treated as superclass objects through references But, superclass objects are not subclass objects Abstract classes cannot be instantiated Object-oriented programming Polymorphism

  4. Polymorphism More on references A superclass reference can be assigned to a superclass variable Point p = new Point( 50, 50); A subclass reference can be assigned to a subclass variable Circle c = new Circle( 60, 60, 6.0); A subclass reference can be assigned to a superclass variable p = c; A Circle is a ( extends a ) Point Object-oriented programming Polymorphism

  5. Polymorphism Example public class HierarchyRelationshipTest { superclass reference to public static void main (String [] args) { superclass instance Point point = new Point(50, 50); subclass reference to Circle circle = new Circle(60, 60, 6.0); subclass instance System.out.println("point coordinates x = " + point.getX() + ", y = " + point.getY()); System.out.println("circle coordinates x = " + circle.getX() + ", y = " + circle.getY()); superclass reference to subclass instance Point anotherPoint = circle; System.out.println("anotherPoint coordinates x = " + anotherPoint.getX() + ", y = " + anotherPoint.getY()); polymorphism : System.exit(0); } subclass methods } will be called point coordinates x = 50, y = 50 circle coordinates x = 60, y = 60 anotherPoint coordinates x = 60, y = 60 Object-oriented programming Polymorphism

  6. Polymorphism What about the inverse? It is perfectly acceptable to assign a public class HierarchyRelationshipTest2 { subclass reference to a superclass public static void main (String [] args) { variable ; can we do the inverse? Point point = new Point(50, 50); Circle circle; assign superclass i.e., assign a superclass circle = point; reference to subclass } reference to a subclass variable } variable This will generate a compiler error HierarchyRelationshipTest2.java:12: incompatible types found : Point required: Circle The reason is simple: a circle = point; ^ Point is not a Circle 1 error Circle has data/behaviour that Point does not have Object-oriented programming Polymorphism

  7. Polymorphism Downcasting Superclass variable , assigned a subclass reference (permissible) Can we invoke a subclass-specific method? Circle c = new Circle(60, 60, 6.0); Point p = c; Sytstem.out.println(p.getRadius()); This is not permitted ; subclass methods are not superclass methods! The compiler can, however, be explicitly instructed to do it — this is called downcasting System.out.println((Circle) p.getRadius()); Called downcasting , because an object higher in the hierarchy is converted to an object lower (down) in the hierarchy Object-oriented programming Polymorphism

  8. Polymorphism Invoking subclass behaviour from a superclass public class HierarchyRelationshipTest3 { HierarchyRelationshipTest3.java:14: cannot resolve symbol symbol : method getRadius () public static void main (String[] args) { location: class Point Point point; double radius = point.getRadius(); Circle circle = new Circle(120, 89, 2.7); ^ HierarchyRelationshipTest3.java:15: cannot resolve symbol point = circle; no problem symbol : method setRadius (double) invoking superclass int x = point.getX(); location: class Point int y = point.getY(); point.setRadius( 33.33 ); behaviour using a point.setX(10); ^ superclass reference point.setY(20); HierarchyRelationshipTest3.java:16: cannot resolve symbol symbol : method getDiameter () double radius = point.getRadius(); location: class Point point.setRadius(33.33); double diameter = point.getDiameter(); double diameter = point.getDiameter(); ^ double circumference = point.getCircumference(); HierarchyRelationshipTest3.java:17: cannot resolve symbol double area = point.getArea(); symbol : method getCircumference () } location: class Point } double circumference = point.getCircumference(); this is wrong ^ invoking subclass HierarchyRelationshipTest3.java:18: cannot resolve symbol symbol : method getArea () behaviour using a location: class Point double area = point.getArea(); superclass reference ^ 5 errors Object-oriented programming Polymorphism

  9. Polymorphism Downcasting example public class HierarchyRelationshipTest3 { public static void main (String[] args) { Point point; Circle circle = new Circle(120, 89, 2.7); point = circle; downncasting : explicitly instruct the compiler that point is a int x = point.getX(); int y = point.getY(); reference to a Circle instance; point.setX(10); prone to runtime errors point.setY(20); double radius = ((Circle) point).getRadius(); ((Circle) point).setRadius(33.33); double diameter = ((Circle) point).getDiameter(); double circumference = ((Circle) point).getCircumference(); double area = ((Circle) point).getArea(); } } Downcasting is tricky Use downcasting only if you are absolutely certain it will succeed Object-oriented programming Polymorphism

  10. Polymorphism More polymorphism examples Suppose, in a hierarchy of shapes, Rectangle extends Quadrilateral Rectangle is more specific than Quadrilateral Any operation permitted on Quadrilateral can be carried out on a Rectangle Suppose you are designing a video game and you have many subclasses of SpaceShip SpaceShip and all its subclasses have a method called draw() When refreshing the screen, you need not know the exact type of a spaceship Treat them all as SpaceShip instances and call SpaceShip.draw() Each subclass-specific draw() method will be called due to polymorphism You can keep adding different types of spaceship without modifying the code that refreshes the screen Object-oriented programming Polymorphism

  11. Polymorphism Abstract classes and methods Abstract classes Are superclasses (also called abstract superclasses ) Cannot be instantiated , but references to them are possible Are usually incomplete — subclasses fill in the details according to their own behaviour They provide abstract methods , which do not have implementation and must be overriden Are not required in a hierarchy, but reduce code dependency when present Concrete classes Are derived from abstract classes Can be instantiated Implement every single method they declare and provide specific behaviour Object-oriented programming Polymorphism

  12. Polymorphism Shapes extended Shape abstract class : getArea() and getVolume() have default + getArea() : Real (= 0.0) implementations ; getName() and + getVolume() : Real (= 0.0) draw() are subclass-specific , so + getName() : String are abstract methods + draw() : void Point - Integer x = 0 - Integer y = 0 + getX() : Integer + getY() : Integer + setX(Integer) : void + setY(Integer) : void concrete classes : extend abstract Circle class with their own behaviour ; - Real radius = 0.0 must override abstract methods + getRadius() : Real + setRadius(Real) : void + getDiameter() : Real + getCircumference() : Real Cylinder - Real height = 0.0 + getHeight() : Real + setHeight(Real) : void Object-oriented programming Polymorphism

  13. Polymorphism The Shape class abstract keyword : declares the Shape class as an abstract class ; disallows new instances to be created public abstract class Shape extends Object { public double getArea() { return 0.0; default implementation : though } Shape is abstract, it can still public double getVolume() { have default behaviour return 0.0; } public abstract String getName(); public abstract void draw(); abstract methods : declared } without an implementation ; subclasses must override them Object-oriented programming Polymorphism

  14. Polymorphism The Point class concrete class derived from an abstract class public class Point extends Shape { private int x; private int y; public Point() {} public Point (int x, int y) { this.x = x; this.y = y; } public void setX (int x) { this.x = x; } public int getX () { return x; } abstract methods must be overriden by public void setY (int y) { this.y = y; } concrete classes public int getY () { return y; } public String getName () { return "Point"; } non-abstract methods public void draw () { // drawing code goes here do not need to be overriden } ( getArea() , getVolume() ) } if their default behaviour is OK Object-oriented programming Polymorphism

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend