| Class Inheritance | ||||||||||||
| Intro to Class Inheritance | Examples of Class Inheritance | |||||||||||
| Public vs. Private or Protected | Abstract Classes | |||||||||||
| Home | ||||||||||||
| Example of Class Inheritance
Here is an example of how inheritance works in JAVA: public class Rectangle { // define instance variables: protected double width, height; // define setter methods: public Rectangle (double w, double h ) { width = w; height = h; } // define other methods: 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). |
||||||||||||