Example
of Class Inheritance
Here is an example
of how inheritance works in JAVA:
public
class Rectangle
// define instance variables:
protected double width, height;
public Rectangle (double w, double h )
width
= w;
height = h;
public double calcArea()
return
width * height;
public double calcPerimeter()
return 2 * (width + height); }
public class Square extends Rectangle {
public Square(double side
super(side, side);
Class Square is derived from a base class Rectangle. Thus, public variables and methods of Rectangle are inherited in Square class. Notice the use of keyword ‘extends’ to derive a child class (Square) from a parent class (Rectangle).