Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // polymorphism in Java
- // 2026-04-23
- // tested on Win with javac 21.0.2
- // needed to use various classes in Java library
- import java.util.*;
- // create an abstraction consisting of everything
- // which is common/shared among each Shape,
- // i.e. the Circle AND the Rectangle AND the Triangle
- abstract class Shape {
- // .. and apparently the class is empty,
- // i.e. all we need is a possibility to compute areas
- // for various sorts of shapes
- // this means we want shapes to have functionality
- // but we don't know what it is at the moment,
- // thus we declare the class as abstract (meaning,
- // it can have not-implemented methods and thus an
- // instance of such class cannot be created; however,
- // we can use this class to create class hierarchies
- // which we are about to do)
- public double getArea();
- }
- // first class has its own field, constructor, overrides
- // toString from Object as Object is parent/base-class/superclass
- // for every object you create, also implements getArea
- // in its own way
- class Circle extends Shape {
- private double radius;
- public Circle(double radius){
- this.radius = radius;
- }
- public double getArea(){
- return Math.PI * radius * radius;
- }
- public String toString(){
- return "Circle " + radius;
- }
- }
- // another class, does the same thing but differently;
- // note that from an outside world perspective the only
- // thing different is constructor, and every method (so far)
- // looks the same though implemented differently;
- // generally extended/derived class can have their own methods,
- // it is just that they will not be accessible through parent class
- // which what we intend at the moment, i.e. compute areas for
- // various sorts of shapes in one action
- class Rectangle extends Shape {
- private double a;
- private double b;
- public Rectangle(double a, double b){
- this.a = a;
- this.b = b;
- }
- public double getArea(){
- return a*b;
- }
- public String toString(){
- return "Rectangle " + a + " " + b;
- }
- }
- // yet another class, not really different, though
- // some values generated randomly do not fit because
- // triangle inequality is not satisfied and the formula
- // implemented does not work
- class Triangle extends Shape {
- private double a;
- private double b;
- private double c;
- public Triangle(double a, double b, double c){
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public double getArea(){
- double s = (a + b + c) * 0.5;
- return Math.sqrt(s*(s-a)*(s-b)*(s-c));
- }
- public String toString(){
- return "Triangle " + a + " " + b + " " + c;
- }
- }
- public class Main {
- public static void main(String[] args){
- // for generating random data
- Random r = new Random();
- // a collection to store Shapes
- ArrayList<Shape> al = new ArrayList<Shape>();
- for(int i = 0; i < 10; ++i){
- // empty reference for the moment, which can point
- // to any object which is in the class hierarchy,
- // i.e. same class (not currently because it is abstract)
- // or any child (Circle/Rectangle/Square) or their children
- // and so on (not otherwise, Circle is a Shape, but Shape is
- // not (always) a Circle, can be something else (eg. Square,
- // and Circle is not a Square)
- Shape temp = null;
- // randomize the shape type, and then
- // call the appropriate constructor along
- // with its parameters - different for every class
- switch(Math.abs(r.nextInt() % 3)){
- case 0:
- temp = new Circle(Math.abs(r.nextDouble()));
- break;
- case 1:
- temp = new Rectangle(Math.abs(r.nextDouble()), Math.abs(r.nextDouble()));
- break;
- case 2:
- temp = new Triangle(Math.abs(r.nextDouble()), Math.abs(r.nextDouble()), Math.abs(r.nextDouble()));
- break;
- }
- al.add(temp);
- }
- // however, once we reach this, we no longer
- // care what Shape instances do we have,
- // we simply run through a container and
- // let polymorphism do the thing, i.e.
- // when the particular implementation used
- // depends not on the type of the reference (Shape)
- // but on actually type of the object in memory
- // (Circle does one thing and Triangle does another)
- System.out.println(al.size());
- for(int i = 0; i < al.size(); ++i){
- System.out.print(al.get(i) + " ");
- System.out.println(al.get(i).getArea());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment