User Tools

Site Tools


cs176:501b:inheritance_intro

Introduction to Inheritance


Topics we'll cover:

  • inheritance,
  • super() (superclass constructors),
  • overriding superclass methods,
  • “protected” access modifier,

Motivation

  • Suppose you will define classes to model circles, rectangles, and triangles. These classes have many features in common. What is the best way to design these classes to avoid redundancy? The answer is to use inheritance.
  • Suppose you are building a GUI app in Java and find yourself wishing that a GUI button could hold more information than it currently can. That is, you wish you could add more features to an existing class. The answer is to use inheritance.

What is Inheritance?

  • Inheritance is one of the three pillars (foundational principles) of object-oriented programming.
    • Encapsulation/Data Hiding
    • Inheritance
    • Polymorphism
  • There is a parent class.
    • Child classes inherit from the parent class.
    • Other ways to express parent ↔ child classes:
      • Super ↔ sub classes
      • Base ↔ derived classes
    • Child classes may themselves be parent classes of lower child classes
      • So, you can have a hierarchy of classes
      • Generally, the parent classes are more general, and the child classes are more specific.

Simple Inheritance

  • Inheritance example: GradedActivity ← FinalExam (FinalExam.zip)
    • The keyword extends denotes an inheritance relationship between a superclass (GradedActivity) and a subclass (FinalExam).
    • There is no special keyword that designates a class to be a superclass.
    • The subclass inherits the public or protected instance variables and methods of the superclass.
      • See section below on the protected keyword.
      • NOTE: private instance variables and methods are NOT inherited.
    • The superclass is intended to be the starting point or basis of the development of the subclass.
    • The subclass inherits the public or protected instance variables and methods of the superclass.
      • In order for this to happen, the subclass constructor has to call the superclass constructor.
      • This call to the superclass constructor can happen explicitly or implicitly (automatically).
  • Calling superclass constructor example 1: SuperClass1 ← SubClass1 (SuperClass1.zip)
  • Calling superclass constructor example 2: SuperClass2 ← SubClass2 (SuperClass2.zip)
    • Superclass constructors are NOT inherited.
    • A superclass constructor is called explicitly or implicitly by a subclass constructor.
      • The call to the superclass constructor has to be the first thing that's done in the subclass constructor.
      • If explicitly calling the superclass constructor, use the super keyword.
      • If the keyword super is not explicitly used, the superclass's default (no-arg) constructor is automatically called at the start of a subclass constructor.
        • So, a superclass' constructor must always be called by the subclass constructor.
  • The keyword super refers to the superclass of the current subclass.
    • The super keyword can be used in two ways:
      • 1 - To call a superclass constructor
      • 2 - To call a superclass method
  • “Constructor Chaining”
    • Constructing an instance of a subclass calls a superclasses' constructor.

Method Overrides

  • A subclass can inherit certain properties (data) and methods from the superclass.
    • But the subclass can also …
      • Add new properties (instance variables)
      • Add new methods
      • Or override the methods of the superclass
  • Sometimes it is necessary for a subclass to modify the implementation (body) of a method defined in the superclass.
    • This is referred to as method overriding.
  • Overriding superclass methods example 1: GradedActivity ← CurvedActivity (CurvedActivity.zip)
  • Overriding superclass methods example 2: Geometric1 ← Triangle
  • NOTE: An instance method of a superclass can only be overridden if it is accessible.
    • A private superclass method cannot be overridden, because it is not accessible outside its own class.
  • NOTE: Overriding vs. Overloading
    • (Overloading: See first lecture on “Classes and Object-Oriented Programming” for notes on “Overloading Methods and Constructors”.)
    • A subclass method overrides a superclass method only if the method signature is exactly the same (same method name and same input parameter list).
    • If the method signature is different in the subclass than in the superclass, that would be an example of overloading a superclass method, not overriding it.

Overriding vs. Overloading a superclass method

Protected

  • The protected visibility modifier is different from public and private and is sometimes used to control inheritance.
    • The protected modifier can be applied to instance variables and methods of a class.
    • A protected instance variable or method in a public class can be accessed by any class in the same package (directory) or its subclasses, even if the subclasses are in a different package.
      • A protected instance variable in a superclass is the same as a public instance variable to its subclasses.
        • Some frown upon the use of protected for this reason and recommend that private continue to be used in superclasses.
    • NOTE: A subclass may override a protected method in its superclass and change its visibility to public.
  • Protected visibility modifier example: GradedActivity2 ← FinalExam2 (ProtectedDemo.zip)

cs176/501b/inheritance_intro.txt · Last modified: 2017/03/21 13:54 by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki