Program to demonstrate how Inheritance works in Java through extends keyword.
Output of the program :
class Shape { private String s = " Shape: "; public void draw(){ name(" draw() "); } public void redraw(){ name(" redraw() "); } public void area(){ name(" area() "); } public void circumference(){ name(" circumference() "); } public void name(String a){ s+=a; } public String toString() { return s; } public static void main(String[] args) { Shape s = new Shape(); s.draw(); s.redraw(); s.area(); s.circumference(); System.out.println(s); } } public class Circle extends Shape{ public void area(){ name(" Circle.area() "); } public void circumference(){ name(" Circle.circumference() "); } public static void main(String[] args) { Circle c = new Circle(); c.draw(); c.redraw(); c.area(); c.circumference(); System.out.println(c); Shape.main(args); } }
Output of the program :