190 likes | 365 Views
Inheritance and Subclasses. Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in. Inheritance. Often we create very similar classes Different types of triangles: equilateral, isosceles, etc. Different types of quadrilaterals: squares, rectangles, parallelograms, etc.
E N D
Inheritance and Subclasses Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in
Inheritance • Often we create very similar classes • Different types of triangles: equilateral, isosceles, etc. • Different types of quadrilaterals: squares, rectangles, parallelograms, etc. • Different types of animals: dogs, cows, etc. • It seems wasteful to write these classes from scratch • These have many features in common • Inheritance in Java allows you to extend a base class to another class with similar features
Inheritance • Usually a class hierarchy is implemented through inheritance • Example: a polygon hierarchy • The class that inherits from a base class becomes a subclass of the base class • The constructors are not inherited • Each subclass must offer its own constructor; however it is possible to access the constructors of the base class • All public members and methods are inherited in the subclass • The subclass may define additional members and methods; same methods override those in the base class
Polygon hierarchy public class Point { // to be used later private double x; private double y; public Point (double x, double y) { this.x = x; this.y = y; } public Point (Point p) { this.x = p.GetX(); this.y = p.GetY(); } // next slide
Polygon hierarchy public double GetX () { return x; } public double GetY () { return y; } public void SetX (double x) { this.x = x; } public void SetY (double y) { this.y = y; } // next slide
Polygon hierarchy public double Distance (Point p) { return Math.sqrt ((x-p.GetX())*(x-p.GetX()) + (y-p.GetY())*(y-p.GetY())); } } // end class
Polygon hierarchy public class Polygon { private int numVertices; public Point vertices[]; public Polygon (Point vertices[]) { int i; numVertices = vertices.length; this.vertices = new Point[numVertices]; for (i=0; i<numVertices; i++) { this.vertices[i] = new Point (vertices[i]); } } // next slide
Polygon hierarchy public Polygon (double x[], double y[]) { int i; numVertices = x.length; vertices = new Point[numVertices]; for (i=0; i<numVertices; i++) { vertices[i] = new Point (x[i], y[i]); } } // next slide
Polygon hierarchy public double Perimeter () { int i; double perimeter = 0; // Assume that the vertices are in order for (i=0; i<numVertices-1; i++) { perimeter += vertices[i].Distance (vertices[i+1]); } perimeter += vertices[i].Distance (vertices[0]); System.out.println (“This is perimeter of Polygon class.”); return perimeter; } // next slide
Polygon hierarchy public boolean isRegular () { int i; double lastSide = vertices[0].Distance (vertices[1]); double thisSide; for (i=1; i<numVertices-1; i++) { thisSide = vertices[i].Distance (vertices[i+1]); if (lastSide != thisSide) return false; else lastSide = thisSide; } // next slide
Polygon hierarchy thisSide = vertices[i].Distance (vertices[0]); if (lastSide != thisSide) return false; return true; } // next slide
Polygon hierarchy public Point Centroid () { int i; double x = 0, y = 0; for (i=0; i<numVertices; i++) { x += vertices[i].GetX(); y += vertices[i].GetY(); } return (new Point (x/numVertices, y/numVertices)); } } // end class
Polygon hierarchy public class Triangle extends Polygon { private double a, b, c; // new members public Triangle (Point vertices[]) { super (vertices); // base class constr. // Other member initialization must // come after call to super a = vertices[0].Distance (vertices[1]); b = vertices[1].Distance (vertices[2]); c = vertices[2].Distance (vertices[0]); } // next slide
Polygon hierarchy public Triangle (double x[], double y[]) { super (x, y); a = vertices[0].Distance (vertices[1]); b = vertices[1].Distance (vertices[2]); c = vertices[2].Distance (vertices[0]); } // next slide
Polygon hierarchy // Add a new method public double Area () { double term1 = 0.5*Perimeter () – a; double term2 = 0.5*Perimeter () – b; double term3 = 0.5*Perimeter () – c; return (Math.sqrt (0.5*Perimeter () * term1 * term2 * term3)); } // next slide
Polygon hierarchy // One more new method public double[] Angles () { double angles[] = new double[3]; angles[0] = Math.asin (2*Area()/(b*c)); angles[1] = Math.asin (2*Area()/(c*a)); angles[2] = Math.asin (2*Area()/(a*b)); return angles; } // next slide
Polygon hierarchy // Override Perimeter with a simpler one public double Perimeter () { System.out.println (“This is perimeter of Triangle class.”); return (a+b+c); } } // end class // next slide
Polygon hierarchy public class Equilateral extends Triangle { public Equilateral (Point vertices[]) { super (vertices); } public Equilateral (double x[], double y[]) { super (x, y); } public double Median () { return (0.5*vertices[0].Distance(vertices[1])*Math.sqrt (3.0)); } } // end class
Polygon hierarchy class PolygonBuilder { public static void main (String a[]) { double x[] = {0, 0.5, -0.5}; double y[] = {0.5*Math.sqrt(3.0), 0, 0}; Equilateral eqT = new Equilateral (x, y); System.out.println (“Perimeter: ” + eqT.Perimeter()); System.out.println (“Area: ” + eqT.Area()); System.out.println (“Centroid: (” + eqT.Centroid().GetX() + “, ” + eqT.Centroid().GetY() + “)”); System.out.println (“Median: ” + eqT.Median()); } } // end class