Question: Implementing a Subclass and Abstract Methods Definition 1. Inheritance is the process by which one class by extending the behavior of an existing dlass is
Implementing a Subclass and Abstract Methods Definition 1. Inheritance is the process by which one class by extending the behavior of an existing dlass is called the superclass and the new class is called a subclass. The Object class in Java is the cosmic class: that is, every class inherits the Object class. In today's lab session, you will define three classes: Polygon, a superclass that describes a polygon, Trlangle, a subclass of the Polygon class, and TriangleDemo, a program to test your implementation of the superclass and subclass. s delined In Java the existing class The Polygon Class Definition 2. A polygon can be defined as a geometric object consisting of a number of points (called vertices) and an equal number of lne segments (called sides) namely a cyclically or dered set of points in a plane, with no three successive points colinear, together with the line segments joining consecutive pairs of the points The Polygon class will consist of an array of type Point2D.Double, named vertices, as its instance variable. This class will be an abstract class. View the online Java API documentation to find the package in which Point2D. Double is defined. This class will consist of the following methods: 1. public Polygon(): a default constructor that sets vertices to null. 2. public Polygon(Point2D.Double!l points): a parameterized constructor that takes an array of points as an explicit parameter and creates a polygon. 3. public double distance(Point2D.Double p1, Point2D.Double p2): an auxiliary method used to find the Euclidean distance between two points. Given points P-( and/ (.r2- 2), the Euclidean distance between them is given by the formula: %) abstract public double areatl:: an abstract method which when implemented computes the area of the polygon. 5 public double perimeter): an instance method that computes the perimeter of the polygon
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
