Inheritance

Related slides on offer

Figure 413. Guess who's inheriting the money Slide presentation Create comment in forum
Guess who's inheriting the money

Figure 414. Biology and inheritance Slide presentation Create comment in forum
Biology and inheritance

Figure 415. Duplicate code Slide presentation Create comment in forum
public class Rectangle{
  // Center coordinate
  private double x; 
  private double y;

  public move (double dx, double dy){ 
     x += dx;
     y += dy;
  }
  private double width, height; 
  ...
}
public class Circle {
  // Center coordinate
  private double x; 
  private double y;

  public move (double dx, double dy){ 
     x += dx;
     y += dy;
  }
  private double radius; 
  ...
}

The center coordinate (x|y) appears both in Rectangle and Circle.

The move(...) method is being defined identically both in Rectangle and Circle!

  • width and height only appear in class Rectangle.

  • radius only appears in class Circle.


Figure 416. Idea: Centralize common code Slide presentation Create comment in forum
  • Create a parent class Shape containing common code portions.

  • Relate both Rectangle and Circle to Shape.


Figure 417. Common and specific properties Slide presentation Create comment in forum
Common Rectangle and Circle attributes:
double x;
double y;
Rectangle attributes Circle attributes
double width;
double height;
double radius;

Figure 418. Basic shape inheritance Slide presentation Create comment in forum

Figure 419. Inheritance Slide presentation Create comment in forum
  • Derived classes inherit state and behaviour.

  • Refinement, specialization.

  • is-A relationship:

    • A rectangle is a shape.

    • A circle is a shape.


Figure 420. Implementing Shape hierarchy Slide presentation Create comment in forum
public class Shape {
  private double x, y;
}
public class Rectangle extends Shape {
  private double width;
  private double height;
}
public class Circle extends Shape {
  private double radius;
}

Figure 421. Creating instances Slide presentation Create comment in forum
final double x = 2, y = 3;
final Shape shape = new Shape(x, y);

final double width = 2, height = 5;
final Rectangle r = new Rectangle(x, y , width, height);

final double radius = 2.5;
final Circle circle = new Circle(x, y , radius);

Figure 422. Shape constructor Slide presentation Create comment in forum
/**
  * Creating a shape located at center coordinate.
  * @param x The center's x component.
  * @param y The center's y component.
  */
public Shape(double x,double y) {
  this.x = x;
  this.y = y;
}

Figure 423. Creating Rectangle instances Slide presentation Create comment in forum
final Rectangle r = 
   new Rectangle(x, y ,
                 width, height );

Center coordinate components belonging to superclass Shape.

width and height belonging to class Rectangle.

Solution: Nested constructor call. Coming soon ...


Figure 424. Rectangle constructor Slide presentation Create comment in forum
/**
  * Creating a rectangle at (x|y) of given width and height.
  * @param x Center's x component.
  * @param y Center's y component.
  * @param width Rectangle's width.
  * @param height Rectangle's height.
  */
public Rectangle(double x, double y,
             double width, double height) {
  super(x, y) ;
  this.width = width; this.height = height ;
}

Passing center coordinate to superclass Shape's constructor.

Note

This must be the first statement within Rectangle's constructor.

Processing width and h

eight in current Rectangle constructor.