Please disable adblock to view this page.

← Go home

Inheritance snippet – Shape (Java)

April 27, 2016
Published By : Pratik Kataria
Categorised in:

//******************* Shape.java *******************

public class Shape {
	
	private int x;
	private int y;
	private String color;
	private String shape;
	
	Shape(){							//This is needed to be defined since we are going to work with inheritance
		x = 0; y = 0; color = " "; shape = "polygon";
	}
	
	Shape(int x, int y, String color, String shape){
		this.x = x;
		this.y = y;
		this.color = color;
		this.shape = shape;
	}
	
	public int getXCoordinate(){
		return this.x;
	}
	
	public void setXCoordinate(int x){
		this.x = x;
	}
	
	public int getYCoordinate(){
		return this.y;
	}
	
	public void setYCoordinate(int y){
		this.y = y;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}	
	
	public int Area(){
		return 0;
	}
	
	
	public void display(){
		System.out.println("The x coordinate is : " + x);
		System.out.println("The y coordinate is : " + y);
		System.out.println("The color of " + shape + " is : " + color);
		
	}
	
	public void printinfo( Shape s ){
		System.out.println("The area of " + s.shape + " is : " + s.Area());
	}
	
	
}


//******************* Rectangle.java *******************

public class Rectangle extends Shape{
 
	private int length;
	private int breadth;
	
	Rectangle(){
		
	}
	
	Rectangle(int x, int y, String color, String shape, int length, int breadth){
		super(x,y,color, shape);
		this.length = length;
		this.breadth = breadth;
		
	}
	
	public int Area(){
		return length*breadth;
	}
	
}


//******************* Square.java *******************

public class Square extends Shape{

	private int side;
	
	Square(int side){
		this.side = side;
	}
	
	public int Area(){
		return side*side;
	}
	
}


//******************* Circle.java *******************

public class Circle extends Shape{

	private int radius;
	final private double pie = 3.14;
	
	Circle(int x, int y, String color, String shape, int radius){
		super(x, y, color, shape);
		this.radius = radius;
	}
	
	public int Area(){
		return (int)(pie*(radius * radius));
	}
	
}


//******************* Main.java *******************

public class Main {

	public static void main( String [] args){
		
		Shape s = new Shape(0, 0, "Black", "Polygon");
		Rectangle r = new Rectangle(1, 1, "Blue", "Rectangle", 3, 5);
		Circle c = new Circle(2, 2, "Orange", "Circle" , 5);
		
		s.display();
		
		System.out.println("------------------------");
		
		r.display();
		s.printinfo(r);
		
		System.out.println("------------------------");
		
		c.display();
		s.printinfo(c);
		
		System.out.println("------------------------");
	}
	
}

Output:

The x coordinate is : 0
The y coordinate is : 0
The color of Polygon is : Black
————————
The x coordinate is : 1
The y coordinate is : 1
The color of Rectangle is : Blue
The area of Rectangle is : 15
————————
The x coordinate is : 2
The y coordinate is : 2
The color of Circle is : Orange
The area of Circle is : 78
————————

View Article Page
Download

Pratik Kataria is a budding programmer, web designer and developer.