battle programminga forum for elite programmerswith extreme will powerto sharpen theire skills
 
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in
Share | 
 

 polymorphism in JAVA eclipse

Go down 
AuthorMessage
Admin
Admin
avatar

Posts : 120
Join date : 2011-08-01

PostSubject: polymorphism in JAVA eclipse   Wed Jan 31, 2018 9:20 pm

polymorphism, exampled via the shape class, and its overridden surface area methode,
in the Main class.

shape super class :
Code:
package com.shapes.objects;

public class Shape {
   
   public Shape() {
      // TODO Auto-generated constructor stub
   }
   public double surfaceArea() {
      return 0;
   }
}

square sub class of shape

Code:
package com.shapes.objects;

public class Square extends Shape {
   private double height;

   public double getHeight() {
      return height;
   }

   public void setHeight(double height) {
      this.height = height;
   }
   public Square(double height) {
      this.height = height;
   }
   public double surfaceArea() {
      return height*height;
   }
}

rectangle sub class of square
Code:
package com.shapes.objects;

public class Rectangle extends Square{
   private double width;

   public double getWidth() {
      return width;
   }

   public void setWidth(double width) {
      this.width = width;
   }

   public Rectangle(double height, double width) {
      super(height);
      this.width = width;
   }
   @Override
   public double surfaceArea() {
      return super.getHeight()*width;
   }
   

}

triangle sub class of shape

Code:
package com.shapes.objects;

public class Triangle extends Shape {
   private double height;
   private double width;
   
   public Triangle(double height, double width) {
      super();
      this.height = height;
      this.width = width;
   }
   public double getHeight() {
      return height;
   }
   public void setHeight(double height) {
      this.height = height;
   }
   public double getWidth() {
      return width;
   }
   public void setWidth(double width) {
      this.width = width;
   }
   @Override
   public double surfaceArea() {
      return (height*width)/2;
   }
   
}

circle class extends (sub class of) shape super class:
Code:
package com.shapes.objects;

public class Circle extends Shape {
   private double Radius;
   public double getRadius() {
      return Radius;
   }
   public void setRadius(double radius) {
      Radius = radius;
   }
   
   public Circle(double radius) {
      super();
      Radius = radius;
   }
   public double surfaceArea() {
      return Radius*Radius*3.1415;
   }
}

main class plays around with the classes using polymorphism to refer to all objects as a shape
and using the override methods of the sub classes even though they were declared as the super class :

Code:
package com.shapes.objects;

import java.util.Scanner;

import org.w3c.dom.css.Rect;

public class Main {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      Shape sh1 = new Shape();
      System.out.println(sh1.surfaceArea());
      Rectangle rec1 = new Rectangle(10, 5);
      System.out.println(rec1.surfaceArea());
      Shape sh2 = rec1;
      System.out.println(sh2.surfaceArea());
      
      Shape[] shapeArray = new Shape[5];
      
      for (int i = 0; i < shapeArray.length; i++) {
         System.out.println("enter shape");
         Scanner scanner2 = new Scanner(System.in);
         String x2 = scanner2.next();
         shapeArray[i] = getShape(x2);
         System.out.println(shapeArray[i].surfaceArea());
      }
   }
   public static Shape getShape(String form) {
      Shape result = null;
      Scanner scanner11 = new Scanner(System.in);
      switch (form) {
      case "square":
         System.out.println("enter square side size");
         double x1 = scanner11.nextDouble();
         return new Square(x1);
      case "circle":
         System.out.println("enter circle radius");
         double Radius1 = scanner11.nextDouble();
         return new Circle(Radius1);
      case "rectangle":
         System.out.println("enter rectangle height");
         double height = scanner11.nextDouble();
         System.out.println("enter rectangle width");
         double width = scanner11.nextDouble();
         return new Rectangle(height, width);
      case "triangle":
         System.out.println("enter triangle height");
         double Theight = scanner11.nextDouble();
         System.out.println("enter triangle width");
         double Twidth = scanner11.nextDouble();
         return new Triangle(Theight, Twidth);
      default:
         break;
      }
      return result;
   }

}

_________________
kurayami no kagi  _____    
Back to top Go down
View user profile http://yotamarker.forumotion.com
 
polymorphism in JAVA eclipse
Back to top 
Page 1 of 1
 Similar topics
-
» [Help] How to use Selenium RC with Java Eclipse IDE
» Verify command when used with Java,JUNIT and Eclipse
» Getting error when i run selenium script in Eclipse, please help
» Eclipse setup with selenium RC
» Response_Code = -1 Error_Message = Request Error

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: chit chat-
Jump to: