Inheritance


Shape.java

package math.shapes;

public class Shape {
protected float side;

public float perimeter() {
return 0.f;
}

public float area() {
return 0.f;
}
}


Circle.java

package math.shapes;


public class Circle extends Shape {
public Circle() {
side = 0.f;
}

public Circle(float side) {
this.side = side;
}

@Override
public float perimeter() {
return 2 * (float)Math.PI * side;
}

@Override
public float area() {
return (float)Math.PI * side * side;
}
}


Rectangle.java

package math.shapes;


public class Rectangle extends Shape {
private float side2;

public Rectangle() {
side = side2 = 0.f;
}

public Rectangle(float side, float side2) {
this.side = side;
this.side2 = side2;
}

@Override
public float perimeter() {
return 2 * (side + side2);
}

@Override
public float area() {
return side * side2;
}
}


Square.java

package math.shapes;


public class Square extends Shape {
public Square() {
side = 0.f;
}

public Square(float side) {
this.side = side;
}

@Override
public float perimeter() {
return 4 * side;
}

@Override
public float area() {
return side * side;
}
}


Main.java

package math.shapes;

public class Main {
public static void main(String[] args) {
Circle c = new Circle(15.65f);

System.out.println("Perimeter of circle = " + c.perimeter());
System.out.println("Area of circle      = " + c.area());

Square s = new Square(15.65f);

System.out.println("Perimeter of sqaure = " + s.perimeter());
System.out.println("Area of sqaure      = " + s.area());

Rectangle r = new Rectangle(15.65f, 23.9f);

System.out.println("Perimeter of Rectangle = " + r.perimeter());
System.out.println("Area of Rectangle      = " + r.area());
}
}

Comments

Popular posts from this blog

Zeller's Congruence

Property Event-Delegation

Method with variable arguments