0% found this document useful (0 votes)
7 views4 pages

Java Record 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Java Record 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING

Ex no: 6
IMPLEMENTATION OF INHERITANCE
Date:

Question
Write a Java program to implement the following relationship and create a Main class to invoke all
the methods.

Aim
To write a program that implements a “is-a” relationship between circle and cylinder.

Code
package circlecylinder;
public class Circle {
private double radius;
private String color;
public Circle() {
radius=1.0;
color="red"; }
public Circle(double radius) {
this.radius=radius;
color="red"; }
public Circle(double radius,String color) {
this.radius=radius;
this.color=color; } public

14 717823P130
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING

double getRadius() {
return radius; }
public void setRadius(double radius) {
this.radius=radius; }
public String getColor() {
return color; }
public void setColor(String color) {
this.color=color; } public
double getArea(){ return
Math.PI*radius*radius; } public
String toString() {
return "Circle[radius="+radius+",color="+color+"]";
}} package
unit2;
public class Cylinder extends Circle{
private
double height; public
Cylinder(){ height=1.0; } public
Cylinder(double radius) { super(radius); }
public Cylinder(double radius,double
height){ super(radius);
this.height=height; }
public Cylinder(double radius,double height,String color) {
super(radius,color); this.height=height; } public double
getHeight() { return height;
}
public void setHeight(double height) {
this.height=height;
} public
double getVolume() { return
super.getArea()*height;
}} package unit2;
public class TestCircle
{
public static void main(String[] args) {
System.out.println("ROLL NO:717823P130");
System.out.println("NAME:MANOJ");
Circle c1=new Circle();
System.out.println(c1.getRadius());
System.out.println(c1.getColor());
System.out.println(c1.getArea());
System.out.println(c1.toString());
Cylinder cy1=new Cylinder();
cy1.setColor("Black");
cy1.setHeight(3);
System.out.println(cy1.getHeight());
System.out.println(cy1.getVolume());
}}

15 717823P130
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING

Output

Result
Thus, the Java program that implements a “is-a” relationship between circle and cylinder
has been successfully developed and the output was verified.

16 717823P130
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING

17 717823P130

You might also like