Inheritance

Cards (63)

  • Inheritance
    In the Object-Oriented Programming paradigm, a characteristic that allows the creation of a new class from an existing class
  • Inheritance
    • The new class inherits the members of the existing class
    • Data members and method members are inherited
    • Constructors are NOT inherited
  • Class declaration
    1. Includes the extends clause
    2. Used to indicate the class which the declared class extends (or is derived, or inherits, from)
  • Superclass
    The class that is extended (a.k.a. supertype or base class)
  • Subclass
    The class that extends another class (a.k.a. subtype or derived class)
  • A class may inherit only from one superclass, but may, in turn, have any number of subclasses
  • Java supports single inheritance and multiple inheritance
  • Single inheritance
    The subclass has a single superclass
  • Multiple inheritance
    The subclass has more than one superclass
  • The private members of a superclass cannot be accessed directly by the subclass
  • A subclass can redefine or override a public method of its superclass
  • Any redefinition of a method applies only to the subclass
  • A constructor of a subclass has direct access to the additional data member(s) declared in the subclass
  • The constructor of the subclass should initialize the private data members of the parent class by executing a constructor of the parent class
  • Inheritance chain
    The sequence of classes involved in superclass subclass relationships
  • Ancestor classes

    Classes in the inheritance chain that are superclasses of a given class
  • Descendant classes
    Classes in the inheritance chain that are subclasses of a given class
  • Object class
    The predefined class that is the only class that does not inherit from another class
  • Any class that does not explicitly specify its superclass through an extends declaration implicitly extends the predefined Object class
  • Inheritance hierarchy
    The set of all classes and their relationships (i.e., the set of all inheritance chains taken collectively), which can be visualized as a tree structure with the class Object as its root
  • Inheritance
    • Provides for state and/or behavior sharing
    • Provides for substitutability
  • A derived class inherits the state and behavior associated with its base class, and may be provided with additional characteristics of its own
  • A derived class may override (i.e., modify) the behaviors that it inherits from its base class
  • Depending on the accessibility of the features of the superclass, some features inherited by a subclass may not be accessible from within the subclass itself
  • Instance methods may be inherited "as is" from superclasses, but subclasses may also provide their own implementations of inherited instance methods (i.e., subclasses may "override" a method that is inherited from a superclass)
  • Overridden methods must be provided with access modifiers that is either as, or less restrictive than, the methods defined in the superclass, but cannot be made more restrictive in the subclass
  • The overriding method must have exactly the same method header (i.e., same return type, name, and number and types of parameters) as the method which is being overridden (otherwise, the method will be overloaded across classes)
  • Static methods cannot be overridden. Instance methods cannot be overridden by static methods
  • Constructors, unlike methods, are not inherited by subclasses, and cannot be overridden
  • Constructors can be overloaded, but only within the scope of the class where they are declared
  • When subclasses inherit instance fields from their superclasses, the instantiation of the state of the subclass object which was inherited from the superclass is still considered a responsibility of the constructor for the superclass
  • During a subclass instantiation, a call to the superclass constructor must be performed to properly initialize the state of the subclass which consists of the instance fields inherited from the superclass
  • super keyword
    Used in a subclass constructor to explicitly control the superclass constructor that must be invoked before the execution of the subclass constructor proceeds
  • If provided, the super keyword must be the first statement in a subclass constructor
  • If the super keyword is omitted, it is implicitly assumed that a call to the default superclass constructor is performed before the subclass constructor proceeds (if in case the superclass has no default constructor, and no explicit call to super is done, the instantiation of the subclass object will fail)
  • An object reference to a subclass can be assigned to a superclass variable, but not vice-versa (i.e., a subclass reference may be "substituted" for a superclass reference, but not the other way around)
  • A class type cast can be used to force a subclass reference that was assigned to a superclass variable to be converted back into an object reference of the subclass type (class type casts, however, cannot be used to force an object reference to be converted into another object reference of a class type which is not the original type, or a subclass, of the object reference being typecasted)
  • In the face of substitutability, invocation of overloaded methods makes use of dynamic binding, in which the method version that is executed is determined at runtime based on the actual type of the current object reference held by the object variable when the method is invoked
  • instanceof operator
    Can be used to determine whether or not the current object reference held by an object variable is of a particular class type
  • Package

    A named group of related classes