Those years, learn together Java 4-3

Source: Internet
Author: User

/**4-3

* Derive a subclass for the rectangle class defined in Experiment 3: a square class. If the square operation is also to find the circumference and area

* What methods does this subclass need to define in addition to the methods inherited from the parent class?

* Lists all fields and methods of the square class.

* Programming to check, run the written square class

**/

public class firstprogram{public static void main  (String[] args) {Square  square1 = new square (8); Square square2 = new square (25); System.out.println ("the perimeter of square1 is: "  + square1.perimeter ()); System.out.println ("the area of square1 is: "  + square1.area ()); System.out.println ("the perimeter of square2 is: "  + square2.perimeter ()); System.out.println ("the area of square2 is: "  + square2.area ());}} class rectangle{protected int length;protected int width; rectangle  () {}rectangle (int l, int w) {this.length = l;this.width = w;} int perimeter  () {return  (Length + width)  * 2;} int area  () {return length * width;}}  class square  extends&nBsp rectangle{protected int side; square  (int side) {super (); this.side = side;} int perimeter  () {return 4 * side;} int area  () {return side * side;}}


Those years, learn together Java 4-3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.