Outline



Special methods



Inheritance



Special class variables: this and super

  • the predefined field this in the code of any class refers to the object of this class
  • the predefined field super in the code of a subclass refers to the object of the superclass:
    public class Child extends Parent {
      private String myName;
      public Child (String name)
      {
        super ();      /* call no-argument constructor for parent */
        this.myName = name; /* initialize local data */
      }
      /* omitting the hasFeature method would give the same result */
      public boolean hasFeature(String feature)
      {
        return super.hasFeature(feature);
      }
      public String name()
      {
        return myName;
      }
    }
    



    Overriding methods



    Overloading methods



    Wrapper classes



    Polymorphism



    Abstract Classes



    Abstract Class Example

    public abstract class AgriculturalPlant {
      protected double plantSize;
    
      // cannot be instantiated, so no constructor needed or possible
    
      // non-abstract (i.e., implemented) methods may appear in an abstract class
      public void grow(double fraction)
      {
        plantSize = plantSize * (1.0 + fraction);
      }
    
      // abstract methods must be implemented by the subclass(es)
      public abstract double harvest();  // returns weight harvested
      public abstract void fertilize(double fertilizerAmount);
    }
    



    class Object



    Exception class Hierarchy