Inheritance and Subclasses 1 Inheritance Often we create
Inheritance and Subclasses 1
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 2
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 3 and methods; same methods override those in the base class
Polygon hierarchy 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) { // copy constructor this. x = p. Get. X(); this. y = p. Get. Y(); } // next slide 4
Polygon hierarchy public double Get. X () { return x; } public double Get. Y () { return y; } public void Set. X (double x) { this. x = x; } public void Set. Y (double y) { this. y = y; } // next slide 5
Polygon hierarchy public double Distance (Point p) { return Math. sqrt ((x-p. Get. X())*(xp. Get. X()) + (y-p. Get. Y())*(y-p. Get. Y())); } } // end class 6
Polygon hierarchy class Polygon { private int num. Vertices; public Point vertices[]; public Polygon (Point vertices[]) { int i; num. Vertices = vertices. length; this. vertices = new Point[num. Vertices]; for (i=0; i<num. Vertices; i++) { this. vertices[i] = new Point (vertices[i]); } 7 } // next slide
Polygon hierarchy public Polygon (double x[], double y[]) { int i; num. Vertices = x. length; vertices = new Point[num. Vertices]; for (i=0; i<num. Vertices; i++) { vertices[i] = new Point (x[i], y[i]); } } // next slide 8
Polygon hierarchy public double Perimeter () { int i; double perimeter = 0; // Assume that the vertices are in order for (i=0; i<num. Vertices-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; 9 } // next slide
Polygon hierarchy public boolean is. Regular () { int i; double last. Side = vertices[0]. Distance (vertices[1]); double this. Side; for (i=1; i<num. Vertices-1; i++) { this. Side = vertices[i]. Distance (vertices[i+1]); if (last. Side != this. Side) return false; 10 } // next slide
Polygon hierarchy this. Side = vertices[i]. Distance (vertices[0]); if (last. Side != this. Side) return false; return true; } // next slide 11
Polygon hierarchy public Point Centroid () { int i; double x = 0, y = 0; for (i=0; i<num. Vertices; i++) { x += vertices[i]. Get. X(); y += vertices[i]. Get. Y(); } return (new Point (x/num. Vertices, y/num. Vertices)); } } // end class 12
Polygon hierarchy 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]); } 13 // 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 14
Polygon hierarchy // Add a new method public double Area () { double term 1 = 0. 5*Perimeter () – a; double term 2 = 0. 5*Perimeter () – b; double term 3 = 0. 5*Perimeter () – c; return (Math. sqrt (0. 5*Perimeter () * term 1 * term 2 * term 3)); } // next slide 15
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 16
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 17
Polygon hierarchy 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. sqr t (3. 0)); } 18 } // end class
Polygon hierarchy class Polygon. Builder { public static void main (String a[]) { double x[] = {0, 0. 5, -0. 5}; double y[] = {0. 5*Math. sqrt(3. 0), 0, 0}; Equilateral eq. T = new Equilateral (x, y); System. out. println (“Perimeter: ” + eq. T. Perimeter()); System. out. println (“Area: ” + eq. T. Area()); System. out. println (“Centroid: (” + eq. T. Centroid(). Get. X() + “, ” + eq. T. Centroid(). Get. Y() + “)”); System. out. println (“Median: ” + eq. T. Median()); } 19
protected Data & Methods • A protected data or method in a public can be accessed by any class in the same package or subclass in other package • modifier Same class Same subclass Different package public Y Y y y protected y y y (default) y y n n private y n n 20
Example package p 1; public class C 1{ protected int y; public int x; private int u; protected void m(){ } } 21
public class C 2{ } public class C 3 extends C 1{ } package p 2; public class C 4 extends C 1{ } public class C 5{ } 22
Preventing Extending and Overriding Use Keyword final public final class C{ } //no subclasses public class test{ public final void m(){ } } 23
Abstract classes A 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
public abstract class Geometricobject{ private String color = “white”; private boolean filled; private java. util. Date date. Created; protected Geometricobject(){ date. Created=new java. util. Date(); } public String get. Color(){ return color; } …. public abstract double get. Area(); public abstract double get. Perimeter(); } 25
public class Test. GO{ public static void main(String[] args){ Geometric. Object o 1= new Circle(5); Geometric. Object o 2= new rect(5, 3); System. out. println (“Equal area? ”+ equal. Area (o 1, o 2); display. GO(o 1); display. GO(o 2); } public static boolean equal. Area (Geometric. Object o 1, Geometric. Object o 2){ return o 1. get. Area()==o 2. get. Area(); } public static void display. GO(Geometric. Object o){ System. out. println (); System. out. println (“Area” + o. get. Area()); System. out. println (“Perimeter” + o. get. Perimeter()); } } 26
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 27
Multiple inheritance interface Regular. Polygon { public double Circumradius (); //abstract } class Equilateral extends Triangle implements Regular. Polygon { public Equilateral (Point vertices[]) { super (vertices); } public Equilateral (double x[], double y[]) { super (x, y); } // next slide 28
Multiple inheritance public double Median () { return (0. 5*vertices[0]. Distance(vertices[1])*Ma th. sqrt (3. 0)); } public double Circumradius () { return (2*Median()/3); } } // end class // You can now print eq. T. Circumradius() in 29 // Polygon. Builder class.
Comparable interface package java. lang; public interface Comparable{ public int compare. To(Object o); } public class String extends Object implements Comparable{ } public class Date extends Object implements Comparable{ } Each of these have to define method 30 compare. To in its body.
- Slides: 30