Java-oriented abstract programming sample

Source: Internet
Author: User

Import java.util.*;
Abstract class geometry{
public abstract double Getarea ();

}
Class pillar{
Geometry Bottom;
Double height;
Pillar (Geometry bottom, double height) {
This.bottom=bottom;
This.height=height;

}
Public double Getvolume () {
Return Bottom.getarea () *height;
}
}

Class Circle extends geometry{
Double R;
Circle (Double R) {

This.r=r;

}
Public double Getarea () {
return (3.14*R*R);
}
}

Class Rectangle extends geometry{
Double A, B;
Rectangle (double a,double b) {

This.a=a;
This.b=b;
}
Public double Getarea () {
return (A*B);
}
}

public class Main {
public static void Main (String args[]) {
Pillar Pillar;
Geometry Bottom;
Bottom =new Rectangle (12,22);
Pillar=new pillar (bottom,58);
System.out.println ("area of the rectangle" +pillar.getvolume ());

Bottom=new Circle (10);
Pillar=new pillar (bottom,58);
System.out.println ("Volume of Cylinder" +pillar.getvolume ());
}
}

Java for abstract programming examples

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.