1 inheritance and subclasses. 2 inheritance often we create very similar classes –different types...
DESCRIPTION
3 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 classTRANSCRIPT
![Page 1: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/1.jpg)
1
Inheritance and Subclasses
![Page 2: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/2.jpg)
2
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
![Page 3: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/3.jpg)
3
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
![Page 4: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/4.jpg)
4
Polygon hierarchyclass 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) { // copy constructor this.x = p.GetX(); this.y = p.GetY(); } // next slide
![Page 5: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/5.jpg)
5
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
![Page 6: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/6.jpg)
6
Polygon hierarchy public double Distance (Point p) { return Math.sqrt ((x-p.GetX())*(x-
p.GetX()) + (y-p.GetY())*(y-p.GetY())); }} // end class
![Page 7: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/7.jpg)
7
Polygon hierarchyclass 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
![Page 8: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/8.jpg)
8
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
![Page 9: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/9.jpg)
9
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
![Page 10: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/10.jpg)
10
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; } // next slide
![Page 11: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/11.jpg)
11
Polygon hierarchy thisSide = vertices[i].Distance
(vertices[0]); if (lastSide != thisSide) return false; return true; } // next slide
![Page 12: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/12.jpg)
12
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
![Page 13: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/13.jpg)
13
Polygon hierarchyclass 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
![Page 14: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/14.jpg)
14
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
![Page 15: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/15.jpg)
15
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
![Page 16: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/16.jpg)
16
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
![Page 17: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/17.jpg)
17
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
![Page 18: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/18.jpg)
18
Polygon hierarchyclass 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
![Page 19: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/19.jpg)
19
Polygon hierarchyclass 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
![Page 20: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/20.jpg)
• A protected data or method in a public can be accessed by any class in the same package or subclass in other package
•
20
modifier Same class
Same package
subclass Different package
public Y Y y yprotected y y y
n(default) y y n nprivate y n n n
![Page 21: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/21.jpg)
Examplepackage p1;public class C1{protected int y;public int x;private int u;protected void m(){}}
21
![Page 22: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/22.jpg)
public class C2{}public class C3 extends C1{}
package p2;public class C4 extends C1{}public class C5{}
22
![Page 23: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/23.jpg)
Preventing Extending and Overriding
Use Keyword final
public final class C{} //no subclassespublic class test{ public final void m(){}}
23
![Page 24: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/24.jpg)
Abstract classesA superclass which can not have any
specific instances can be declared abstract.
Methods which can not be implemented in an abstract class use modifier abstract.
24
![Page 25: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/25.jpg)
public abstract class Geometricobject{ private String color = “white”; private boolean filled; private java.util.Date dateCreated; protected Geometricobject(){
dateCreated=new java.util.Date(); } public String getColor(){ return color;}…. public abstract double getArea();public abstract double getPerimeter();}
25
![Page 26: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/26.jpg)
public class TestGO{public static void main(String[] args){
GeometricObject o1= new Circle(5); GeometricObject o2= new rect(5,3); System.out.println (“Equal area?”+ equalArea (o1,o2); displayGO(o1); displayGO(o2);} public static boolean equalArea (GeometricObject o1,
GeometricObject o2){ return o1.getArea()==o2.getArea(); }public static void displayGO(GeometricObject o){ System.out.println (); System.out.println (“Area” + o.getArea());System.out.println (“Perimeter” + o.getPerimeter());}}
26
![Page 27: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/27.jpg)
27
Multiple inheritance• Java does not allow inheritance from
more than one base classes– At most one extension– However, one can “implement” multiple
interfaces– Interface is a collection of abstract
methods i.e. no method body is specified– A class implementing an interface must
provide the suitable method bodies
![Page 28: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/28.jpg)
28
Multiple inheritanceinterface RegularPolygon { public double Circumradius (); //abstract}
class Equilateral extends Triangle implements RegularPolygon {
public Equilateral (Point vertices[]) { super (vertices); } public Equilateral (double x[], double y[]) { super (x, y); } // next slide
![Page 29: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/29.jpg)
29
Multiple inheritance public double Median () { return
(0.5*vertices[0].Distance(vertices[1])*Math.sqrt (3.0));
} public double Circumradius () { return (2*Median()/3); }} // end class// You can now print eqT.Circumradius()
in// PolygonBuilder class.
![Page 30: 1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different](https://reader036.vdocument.in/reader036/viewer/2022062317/5a4d1b1a7f8b9ab05999307f/html5/thumbnails/30.jpg)
Comparable interfacepackage java.lang;public interface Comparable{ public int compareTo(Object o); }public class String extends Object implements
Comparable{ }public class Date extends Object implements
Comparable{ }Each of these have to define method compareTo in
its body.
30