Inheritance

Goals: increase reusability, reliability and comprehensibility.

Relationships between classes

Inheritance

  • Old method: code copying.
  • New method: Classes with inheritance.
  • You can inherited methods and data.
  • Inheritance permits minor variation and sharing.
  • Polymorphism permits determination of which function to execute at run-time, decided by object type.
  • Java supports single-inheritance, ie. you can inheritance hierarchy from a tree and each object is a member of only one tree. An argument for this is simplicity.
  • A limited form of multiple inheritance is achieved with interfaces, which is the next topic.

    Inheritance Example

     public class NormedComplex
                 extends Complex         // the inheritance 
         {
            private double norm;
            
             NormedComplex(double x, double y)
            {
               super(x,y);    // if this is not here, super() is assumed.
                              // if super() not defined, compiler error.
               norm = x*x-y*y;
            }
            NormedComplex()
            {
              this(0,0);     // No call to super() as "this" will call super.
            } 
           
            public void normalize()
            {
              setReal(getReal()/norm);            // getReal is inherited
              setComplex(getImaginary()/norm);
            }
           ....
          }
    
    

    Shadowing/Overriding

    Casting

    Abstract Classes

    Polymorphism Example

      abstract class Shape  //  can't be instantiated
      {
        double  area();  // notice prototype
        double perimeter();
      }
      class Rectangle extends Shape 
       {                            
         double length, width;
         Rectangle(double length , double width)
          { 
            this.length = length;
            this.width = width;
          }
         double area()
        {
          return length*width;
        }
         double perimeter()
         {
          return 2*length + 2*width;
          }
        }
      class Square extends Rectangle 
         {
            square(double side)
           {
              super(side,side);
           }
         }
     class Circle extends Shape
    
       {
         double radius;
         circle(double radius)
         {
           this.radius = radius;
         }
        double area()
       {
         return Math.PI*radius*radius;
       }
       double perimeter()
      {
        return Math.PI*diameter(); // you don't worry about order of declaration
      }
      double diameter()
      {
        return 2*radius;
     }
    

    Polymorphism Example 2

    Suppose we have graphical objects like buttons, scrollbars, textfields, canvas, radio buttons and the like.
    Suppose each is derived from a superclass class component, which has a paint method.
    Suppose each graphical object overrides the paint method, saying how it should be drawn on display.
    Suppose we have a method update() which has a container for all the objects on display and simple calls paint on each of them.
    Notice how easy it will be to add a new graphical object.
    This is how Applet draw on the screen.

    What have we accomplished?

    Accessibility Rules

    The boundaries of accessibility are determined by class, subclass and package. If you write your code(classes) in one directory, then the classes are all part of the same anonymous package.