irmantas_radavicius

Untitled

Apr 23rd, 2026
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. // polymorphism in Java
  2. // 2026-04-23
  3. // tested on Win with javac 21.0.2
  4.  
  5. // needed to use various classes in Java library
  6. import java.util.*;
  7.  
  8. // create an abstraction consisting of everything
  9. // which is common/shared among each Shape,
  10. // i.e. the Circle AND the Rectangle AND the Triangle
  11. abstract class Shape {
  12.    
  13.     // .. and apparently the class is empty,
  14.     // i.e. all we need is a possibility to compute areas
  15.     // for various sorts of shapes
  16.     // this means we want shapes to have functionality
  17.     // but we don't know what it is at the moment,
  18.     // thus we declare the class as abstract (meaning,
  19.     // it can have not-implemented methods and thus an
  20.     // instance of such class cannot be created; however,
  21.     // we can use this class to create class hierarchies
  22.     // which we are about to do)
  23.     public double getArea();
  24. }
  25.  
  26. // first class has its own field, constructor, overrides
  27. // toString from Object as Object is parent/base-class/superclass
  28. // for every object you create, also implements getArea
  29. // in its own way
  30. class Circle extends Shape {
  31.     private double radius;
  32.    
  33.     public Circle(double radius){
  34.         this.radius = radius;
  35.     }
  36.     public double getArea(){
  37.         return Math.PI * radius * radius;
  38.     }
  39.     public String toString(){
  40.         return "Circle " + radius;
  41.     }
  42. }
  43.  
  44. // another class, does the same thing but differently;
  45. // note that from an outside world perspective the only
  46. // thing different is constructor, and every method (so far)
  47. // looks the same though implemented differently;
  48. // generally extended/derived class can have their own methods,
  49. // it is just that they will not be accessible through parent class
  50. // which what we intend at the moment, i.e. compute areas for
  51. // various sorts of shapes in one action
  52. class Rectangle extends Shape {
  53.     private double a;
  54.     private double b;
  55.    
  56.     public Rectangle(double a, double b){
  57.         this.a = a;
  58.         this.b = b;
  59.     }  
  60.     public double getArea(){
  61.         return a*b;
  62.     }
  63.     public String toString(){
  64.         return "Rectangle " + a + " " + b;
  65.     }
  66. }
  67. // yet another class, not really different, though
  68. // some values generated randomly do not fit because
  69. // triangle inequality is not satisfied and the formula
  70. // implemented does not work
  71. class Triangle extends Shape {
  72.     private double a;
  73.     private double b;
  74.     private double c;
  75.    
  76.     public Triangle(double a, double b, double c){
  77.         this.a = a;
  78.         this.b = b;
  79.         this.c = c;
  80.     }
  81.     public double getArea(){
  82.         double s = (a + b + c) * 0.5;
  83.         return Math.sqrt(s*(s-a)*(s-b)*(s-c));
  84.     }
  85.     public String toString(){
  86.         return "Triangle " + a + " " + b + " " + c;
  87.     }
  88. }
  89.  
  90.  
  91.  
  92. public class Main {
  93.     public static void main(String[] args){
  94.         // for generating random data
  95.         Random r = new Random();
  96.        
  97.         // a collection to store Shapes
  98.         ArrayList<Shape> al = new ArrayList<Shape>();
  99.        
  100.         for(int i = 0; i < 10; ++i){
  101.             // empty reference for the moment, which can point
  102.             // to any object which is in the class hierarchy,
  103.             // i.e. same class (not currently because it is abstract)
  104.             // or any child (Circle/Rectangle/Square) or their children
  105.             // and so on (not otherwise, Circle is a Shape, but Shape is
  106.             // not (always) a Circle, can be something else (eg. Square,
  107.             // and Circle is not a Square)
  108.             Shape temp = null;
  109.            
  110.             // randomize the shape type, and then
  111.             // call the appropriate constructor along
  112.             // with its parameters - different for every class
  113.             switch(Math.abs(r.nextInt() % 3)){
  114.                 case 0:
  115.                     temp = new Circle(Math.abs(r.nextDouble()));
  116.                     break;
  117.                 case 1:
  118.                     temp = new Rectangle(Math.abs(r.nextDouble()), Math.abs(r.nextDouble()));
  119.                     break;
  120.                 case 2:
  121.                     temp = new Triangle(Math.abs(r.nextDouble()), Math.abs(r.nextDouble()), Math.abs(r.nextDouble()));
  122.                     break;
  123.             }
  124.             al.add(temp);
  125.         }
  126.        
  127.         // however, once we reach this, we no longer
  128.         // care what Shape instances do we have,
  129.         // we simply run through a container and
  130.         // let polymorphism do the thing, i.e.
  131.         // when the particular implementation used
  132.         // depends not on the type of the reference (Shape)
  133.         // but on actually type of the object in memory
  134.         // (Circle does one thing and Triangle does another)
  135.         System.out.println(al.size());
  136.         for(int i = 0; i < al.size(); ++i){
  137.             System.out.print(al.get(i) + " ");
  138.             System.out.println(al.get(i).getArea());
  139.         }      
  140.     }      
  141. }
Advertisement
Add Comment
Please, Sign In to add comment