Inheritance snippet – Shape (Java)
April 27, 2016
Categorised in: Java Codes
//******************* 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
————————
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.