[go: up one dir, main page]

0% found this document useful (0 votes)
25 views2 pages

Chapter 3 Inhertanice Example 1 and Casting Object

The document defines classes for geometric shapes Circle and Rectangle that extend an abstract Geometry class. It creates instances of the classes and calls methods like getArea() and getColor() to output properties.

Uploaded by

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

Chapter 3 Inhertanice Example 1 and Casting Object

The document defines classes for geometric shapes Circle and Rectangle that extend an abstract Geometry class. It creates instances of the classes and calls methods like getArea() and getColor() to output properties.

Uploaded by

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

public class Mainmethod {

// child class
public static void main(String[] args) {
class Rectangle extends Geometry{
Circle circle1=new Circle(5.0);
int l,w,h;
Circle circle2=new Circle("green");
Rectangle (int l,int w,int h) {
System.out.println("the area of the circle ="+circle1.getarea());
this.l=l;
System.out.println("the color of the circle is "+circle2.getcolor());
this.h=h;
System.out.println("the date of circle is created on "+circle1.getdate());
this.w=w;
Rectangle rectangle1=new Rectangle(12,4,23);
}
Rectangle rectangle2=new Rectangle ("yellow");
Rectangle(String color) {
System.out.println("the area of the rectangle ="+rectangle1.getarea());
setcolor(color); // super class method
System.out.println("the color of the rectangle is "+rectangle2.getcolor());
}
System.out.println("the date of rectangle is created on " +rectangle1.getdate());
int getarea() {
}
return l*w*h;
}
}

} Package acess; // supper class


import java.util.Date;
// child class class Geometry{
class Circle extends Geometry{
String color="white";
double raduis;
Date date;
Circle (double raduis ) {
void setcolor(String color) {
this.raduis=raduis;
this.color=color;
}
}
Circle (String color){

setcolor(color); //super class method void setdate() {

} date =new Date();

double getarea() { }
return raduis*raduis*Math.PI;
String getcolor() {
}
return color;
}
}

Date getdate() {

return this .date=new Date();

}
public class Access {

static void m(Geometry obj){

if (obj instanceof Circle ){

System.out.println("the area of the circle ="+((Circle)obj).getarea());

else

if (obj instanceof Rectangle){

System.out.println("the area of the rectangle ="+((Rectangle)obj).getarea());

public static void main(String[] args) {

Geometry circle1=new Circle(5.0);

Geometry circle2 =new Circle("green");

Geometry rectangle1=new Rectangle(12,4,23);

Geometry rectangle2=new Rectangle ("brown");

m(circle1);

m(rectangle1);

m(rectangle2);

System.out.println("the date of circle is created on "+circle1.getdate());

System.out.println("the color of the circle is "+circle2.getcolor());

System.out.println("the date of rectangle is created on " +rectangle1.getdate());

System.out.println("the color of the rectangle is "+rectangle2.getcolor());

}
Out put
the area of the circle = 78.53981633974483

the area of the rectangle = 1104

the area of the rectangle = 0

the date of circle is created on Thu Jan 26 02:00:24 EAT 2023

the color of the circle is green

the date of rectangle is created on Thu Jan 26 02:00:24 EAT 2023

the color of the rectangle is brown

You might also like