{"id":26567,"date":"2021-04-28T23:10:07","date_gmt":"2021-04-29T03:10:07","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/example-illustrating-inheritance-and-abstract-classes-programming-code-examples-java-j2ee-j2me-object-oriented-programming\/"},"modified":"2021-04-28T23:10:07","modified_gmt":"2021-04-29T03:10:07","slug":"example-illustrating-inheritance-and-abstract-classes-programming-code-examples-java-j2ee-j2me-object-oriented-programming","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26567","title":{"rendered":"Example illustrating inheritance and abstract classes #Programming Code Examples #Java\/J2EE\/J2ME #Object Oriented Programming"},"content":{"rendered":"<pre>\n***********************************\n# Example illustrating inheritance and abstract classes.\n\n    * Shape.java The parent class (abstract) for all closed, open, curved, and straight-edged shapes.\n    * Curve.java An (abstract) curved Shape (open or closed).\n    * StraightEdgedShape.java A Shape with straight edges (open or closed).\n    * Measurable.java Interface defining classes with measurable areas.\n    * Circle.java A circle that extends Shape and implements Measurable.\n    * MeasureUtil.java Operates on Measurable instances.\n    * Polygon.java A closed Shape with straight edges; extends StraightEdgedShape and implements Measurable.\n    * Rectangle.java A rectangle that satisfies the Measurable interface; extends Polygon.\n    * MeasureTest.java Driver for example.\n**************************************\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nShape.java \n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\/** The parent class for all closed, open, curved, and \n *  straight-edged shapes.\n *\n ############################\npublic abstract class Shape {\n  protected int x, y;\n\n  public int getX() {\n    return(x);\n  }\n\n  public void setX(int x) {\n    this.x = x;\n  }\n\n  public int getY() {\n    return(y);\n  }\n\n  public void setY(int y) {\n    this.y = y;\n  }\n}\n#############################\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nCurve.java An (abstract) curved Shape (open or closed)\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\/** A curved shape (open or closed). Subclasses will include\n *  arcs and circles.\n *\n***********************\n\npublic abstract class Curve extends Shape {}\n##############################\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nStraightEdgedShape.java A Shape with straight edges (open or closed). \n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\/** A Shape with straight edges (open or closed). Subclasses\n *  will include Line, LineSegment, LinkedLineSegments,\n *  and Polygon.\n *\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\npublic abstract class StraightEdgedShape extends Shape {}\n################################\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nMeasurable.java Interface defining classes with measurable areas\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\/** Used in classes with measurable areas. \n *\n **************\n\npublic interface Measurable {\n  double getArea();\n}\n#################################\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nCircle.java A circle that extends Shape and implements Measurable.\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\/** A circle. Since you can calculate the area of\n *  circles, class implements the Measurable interface.\n *\n***********************************\npublic class Circle extends Curve implements Measurable {\n  private double radius;\n\n  public Circle(int x, int y, double radius) {\n    setX(x);\n    setY(y);\n    setRadius(radius);\n  }\n\n  public double getRadius() {\n    return(radius);\n  }\n\n  public void setRadius(double radius) {\n    this.radius = radius;\n  }\n\n  \/** Required for Measurable interface. *\/\n\n  public double getArea() {\n    return(Math.PI * radius * radius);\n  }\n}\n############################\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nMeasureUtil.java Operates on Measurable instances\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\/** Some operations on Measurable instances. \n *\n\n************************\npublic class MeasureUtil {\n  public static double maxArea(Measurable m1,\n                               Measurable m2) {\n    return(Math.max(m1.getArea(), m2.getArea()));\n  }\n\n  public static double totalArea(Measurable[] mArray) {\n    double total = 0;\n    for(int i=0; i&lt;marray .length; i++) {\n      total = total + mArray[i].getArea();\n    }\n    return(total);\n  }\n}\n#########################\n~~~~~~~~~~~~~~~~~~~~~~~~~\nPolygon.java A closed Shape with straight edges; extends StraightEdgedShape and implements Measurable.\n~~~~~~~~~~~~~~~~~~~~~~~~~\n\/** A closed Shape with straight edges. \n *\n***************************************\n\npublic abstract class Polygon extends StraightEdgedShape\n                              implements Measurable {\n  private int numSides;\n\n  public int getNumSides() {\n    return(numSides);\n  }\n\n  protected void setNumSides(int numSides) {\n    this.numSides = numSides;\n  }\n}\n###########################\n~~~~~~~~~~~~~~~~~~~~~~~~~~~\nRectangle.java A rectangle that satisfies the Measurable interface; extends Polygon.\n~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\/** A rectangle implements the getArea method. This satisfies \n *  the Measurable interface, so rectangles can be instantiated.\n *\n*****************************\n\npublic class Rectangle extends Polygon {\n  private double width, height;\n  \n  public Rectangle(int x, int y, \n                   double width, double height) {\n    setNumSides(2);\n    setX(x);\n    setY(y);\n    setWidth(width);\n    setHeight(height);\n  }\n\n  public double getWidth() {\n    return(width);\n  }\n\n  public void setWidth(double width) {\n    this.width = width;\n  }\n\n  public double getHeight() {\n    return(height);\n  }\n\n  public void setHeight(double height) {\n    this.height = height;\n  }\n\n  \/** Required to implement Measurable interface. *\/\n\n  public double getArea() {\n    return(width * height);\n  }\n}\n##########################\n~~~~~~~~~~~~~~~~~~~~~~~~~~\nMeasureTest.java Driver for example.\n~~~~~~~~~~~~~~~~~~~~~~~~~~\n\/** Test of MeasureUtil. Note that we could change the \n *  actual classes of elements in the measurables array (as\n *  long as they implemented Measurable) without changing\n *  the rest of the code.\n ************************\npublic class MeasureTest {\n  public static void main(String[] args) {\n    Measurable[] measurables =\n      { new Rectangle(0, 0, 5.0, 10.0),\n        new Rectangle(0, 0, 4.0, 9.0),\n        new Circle(0, 0, 4.0),\n        new Circle(0, 0, 5.0) };\n    System.out.print(&quot;Areas:&quot;);\n    for(int i=0; i&lt;measurables.length; i++)\n      System.out.print(&quot; &quot; + measurables[i].getArea());\n    System.out.println();\n    System.out.println(&quot;Larger of 1st, 3rd: &quot; +\n       MeasureUtil.maxArea(measurables[0],\n                     measurables[2]) +\n                     &quot;nTotal area: &quot; +\n                     MeasureUtil.totalArea(measurables));\n  }\n}\n@@@@@@@@@@@@@@@@@@@@@@@@\n~~~~~~~~~~~~~~~~~~~~~~~\n<\/pre>\n<p>~~~~~~~~~~~~~~~~~~~~~~<\/p>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10394<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Object Oriented Programming<br \/>Tags:Java\/J2EE\/J2MEObject Oriented Programming<br \/> Post Data:2017-01-02 16:04:39<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>*********************************** # Example illustrating inheritance and abstract classes. * Shape.java The parent class (abstract) for all closed, open, curved, and straight-edged shapes. * Curve.java An (abstract) curved Shape (open or closed). * StraightEdgedShape.java A Shape with straight edges (open or closed). * Measurable.java Interface defining classes with measurable areas. * Circle.java A circle that extends &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26567\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-26567","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10541,"url":"http:\/\/bangla.sitestree.com\/?p=10541","url_meta":{"origin":26567,"position":0},"title":"Example illustrating inheritance and abstract classes","author":"","date":"August 29, 2015","format":false,"excerpt":"*********************************** # Example illustrating inheritance and abstract classes. \u00a0\u00a0\u00a0 * Shape.java The parent class (abstract) for all closed, open, curved, and straight-edged shapes. \u00a0\u00a0\u00a0 * Curve.java An (abstract) curved Shape (open or closed). \u00a0\u00a0\u00a0 * StraightEdgedShape.java A Shape with straight edges (open or closed). \u00a0\u00a0\u00a0 * Measurable.java Interface defining classes\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10539,"url":"http:\/\/bangla.sitestree.com\/?p=10539","url_meta":{"origin":26567,"position":1},"title":"Example illustrating inheritance and abstract classes","author":"","date":"August 2, 2015","format":false,"excerpt":"illustrating inheritance \u098f\u09ac\u0982 abstract classes \u098f\u09b0 \u0989\u09a6\u09be\u09b9\u09b0\u09a3 Shape.java \u09b8\u09ac, \u09ac\u09a6\u09cd\u09a7 \u0996\u09cb\u09b2\u09be, \u09ac\u09be\u0981\u0995\u09be, \u098f\u09ac\u0982 \u09b8\u09cb\u099c\u09be \u09aa\u09be\u09b0\u09cd\u09b6\u09cd\u09ac\u09c7 \u09a7\u09be\u09b0\u09ac\u09bf\u09b6\u09bf\u09b7\u09cd\u099f \u0986\u0995\u09be\u09b0 \u098f\u09b0 \u099c\u09a8\u09cd\u09af \u09aa\u09cd\u09af\u09be\u09b0\u09c7\u09a8\u09cd\u099f \u0995\u09cd\u09b2\u09be\u09b8 (\u09b8\u09be\u09b0\u09be\u0982\u09b6)\u0964 Curve.java \u098f\u0995\u099f\u09bf (\u09b8\u09be\u09b0\u09be\u0982\u09b6) \u09ac\u09be\u0981\u0995\u09be \u0986\u0995\u09be\u09b0 (\u0996\u09cb\u09b2\u09be \u09ac\u09be \u09ac\u09a8\u09cd\u09a7)\u0964 StraightEdgedShape.java \u09b8\u09b0\u09be\u09b8\u09b0\u09bf \u09a7\u09be\u09b0 \u09b8\u09ae\u09cd\u09ac\u09b2\u09bf\u09a4 \u098f\u0995\u099f\u09bf \u0986\u0995\u09c3\u09a4\u09bf (\u0996\u09cb\u09b2\u09be \u09ac\u09be \u09ac\u09a8\u09cd\u09a7)\u0964 Measurable.java \u09aa\u09b0\u09bf\u09ae\u09be\u09aa\u09af\u09cb\u0997\u09cd\u09af \u098f\u09b2\u09be\u0995\u09be\u09af\u09bc \u0987\u09a8\u09cd\u099f\u09be\u09b0\u09ab\u09c7\u09b8 \u09a1\u09bf\u09ab\u09be\u0987\u09a8\u09bf\u0982 \u0995\u09cd\u09b2\u09be\u09b8\u0964 Circle.java \u098f\u0995\u099f\u09bf \u09ac\u09c3\u09a4\u09cd\u09a4 \u09af\u09be \u0986\u0995\u09be\u09b0\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10536,"url":"http:\/\/bangla.sitestree.com\/?p=10536","url_meta":{"origin":26567,"position":2},"title":"Code examples for interfaces","author":"","date":"August 29, 2015","format":false,"excerpt":"**************************** Code examples for interfaces: \u00a0\u00a0\u00a0 * Class1.java implements Interface1.java \u00a0\u00a0\u00a0 * Abstract Class2.java implements Interface1.java and Interface2.java \u00a0\u00a0\u00a0 * Class3.java extends abstract class Class2.java \u00a0\u00a0\u00a0 * Interface3.java extends Interface1.java and Interface2.java *************************** ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Class1.java ~~~~~~~~~~~~~~~~~~~~~~~~~~~ \/\/ This class is not abstract, so it must provide \/\/ implementations of method1\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26565,"url":"http:\/\/bangla.sitestree.com\/?p=26565","url_meta":{"origin":26567,"position":3},"title":"Code examples for interfaces #Programming Code Examples #Java\/J2EE\/J2ME #Object Oriented Programming","author":"Author-Check- Article-or-Video","date":"April 28, 2021","format":false,"excerpt":"**************************** Code examples for interfaces: * Class1.java implements Interface1.java * Abstract Class2.java implements Interface1.java and Interface2.java * Class3.java extends abstract class Class2.java * Interface3.java extends Interface1.java and Interface2.java *************************** ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Class1.java ~~~~~~~~~~~~~~~~~~~~~~~~~~~ \/\/ This class is not abstract, so it must provide \/\/ implementations of method1 and method2. public class\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10252,"url":"http:\/\/bangla.sitestree.com\/?p=10252","url_meta":{"origin":26567,"position":4},"title":"Template illustrating the second approach for creating a class with thread behavior.","author":"","date":"August 25, 2015","format":false,"excerpt":"Template illustrating the second approach for creating a class with thread behavior. In this case, the class implements the Runnable interface while providing a run method for thread execution. public class ThreadedClass extends AnyClass implements Runnable { \u00a0 public void run() { \u00a0\u00a0\u00a0 \/\/ Thread behavior here. \u00a0 } \u00a0\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":16206,"url":"http:\/\/bangla.sitestree.com\/?p=16206","url_meta":{"origin":26567,"position":5},"title":"OOP concepts in PHP 5 in brief","author":"Sayed","date":"September 21, 2019","format":false,"excerpt":"OOP concepts in PHP 5 in brief OOP concepts in PHP 5 in brief OOP concepts in PHP 5 in short Why this short\u200a\u2014\u200anote? if you are familiar with OOD and any OOP language such as Java\/C++, this short note will give you enough information to start with PHP 5\u2026","rel":"","context":"In &quot;\u09ac\u09cd\u09b2\u0997 \u0964 Blog&quot;","block_context":{"text":"\u09ac\u09cd\u09b2\u0997 \u0964 Blog","link":"http:\/\/bangla.sitestree.com\/?cat=182"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26567"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26567\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26567"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}