Inheritance

  • Basic properties of an OOP language
  • Allows one or more classes to obtain properties and functionality that is defined in another class
  • Base class: high-level class that contains attributes that all inherited classes have
  • Derived class: Lower-level class which gets some of its attributes from the base claass and adds others
  • extends: Indicates inheritence
    • Subclass extends the generic nature of the base to a more specialized subclass

Using "super" and "this"

  • When within a subclass method, use super to specify use of a parent class' member
    • Generally not necessary
    • Useful for specifying parent constructor and when a method is shadowed
  • Use this to refer to the object being operated on by a method
public class Point3 extends Point2
{
 double z;

 Point3(
   final double inX,
   final double inY,
   final double inZ
 )

 Point3()
 {
   this(10, 10, 10); // Call Point3(inX, inY, inZ)
 }

 {
   super(inX, inY);
   z = inZ;
 }

 public String toString()
 {
   return (super.toString() + "z: " + z);
 }
}
  • If super is not specified, default parent constructor will be called

results matching ""

    No results matching ""