Chapter 10
Introduction to Inheritance

Student Learning Outcomes.

1.Learn about the concept of inheritance
2. Extend classes
3. Override superclass methods
4. Call constructors during inheritance
5. Access superclass methods
6. Employ information hiding
7. Learn which methods you cannot override

Important Points
1. Inheritance is a mechanism that enables one class to inherit, or assume, both the behavior and the attributes of another class.
a. Unified Modeling Language(UML) consists of many types of diagrams and help illustrate inheritance.
b. A class diagram is a visual tool that provides you with an overview of a class. It consists of a rectangle divided into three sections. Top section is the name of the class, the middle section contains the names and data types of the attributes, and the bottom section contains the methods. Only the method return type, name, and arguments are provided in the diagram - the instructions that make up the method body are omitted.

employee //name of class

-empNum : int          //the - sign is inserted to indicate a private field or
-empSal : double       //method

+getEmpNum  : int    //the + sign is inserted in front of each public field or
+getEmpSal     :double        //method
+setEmpNum(int num)  :  void
+setEmpSal(double sal)  : void

                                              indicates relationship of inheritance with EmployeeWithTerritory from Employee


-empTerritory : int

+getEmpTerritory : int
+setEmpTerritory(int territory) : void

c. Inheritance saves time because employee fields and methods already exist, and they reduce errors because the Employee methods already have been tested. They also reduce the amount of new learning required to use the new class.
d. Base class - aka superclass or parent class is used as a basis for inheritance - in this example it is employee.
e. Derived class - aka subclass or child class - inherits from the base class. You can assign a derived class objectís reference to a base class reference. If a method accepts a base class object reference it will also accept references to its derived classes.
f. Composition - the relationship in which a class contains one or more members of another class, when those members would not continue to exist without the object that contains them.
g. Aggregation -the relationship in which a class contains one or more members of another class, when those members would continue to exist without the object that contains them.

2. Extending Classes: The keyword extends is used to achieve inheritance in Java.

public class EmpolyeeWithTerritory extends Employee
//automatically receives the data fields and methods of the superclass Employee:
        private int empTerritory;
        public int getEmpTerritory()
        †† †††return empTerritory;
        public void setEmpTerritory (int num)

a. Inheritance is a one way street.  A child inherits from the parent only. A parent class does not have access to the child's methods or data.
b. instanceof operator - determines whether an object is a member or descendant of a class.

NorthernRep instanceof EmployeeWithTerrritory †//yields true if operand on the left can be upcast to the operand on the right.

3. Overriding SuperClass methods. Sometimes the superclass data fields and methods are not entirely appropriate for the subclass objects; When true superclass has to be overriden.
a. Polymorphism - "many forms" using the same method name to indicate different implementations.
b. When you create a method in the child class that has the same name and parameter list as a method in its parent class, you override the method in the parent class. The childís version is used.
c. If the parentís method has the same name but a different parameter list, the child overloads the parent class and the child has access to both versions.
d. The key word super can be used to override a parentís method.  
e. subtype polymorphism is the ability of one method name to work appropriately for different child (sub class) objects of the same parent class. 
f. Any child class object has all the attributes of its parent, but all of those attributes might not be directly accessible.
g. When a child class method overrides a parent class method, and you use the method name with a child class object, the child class method version executes.

4. Calling constructors during inheritance. When you instantiate an object that is a member of a subclass two constructors are called: the base class and the derived class. The superclass constructors must execute first and then the subclass constructor executes.
a. Every Java object automatically is a child of a class named Object. When you instantiate any object, you call its constructor and the Objects constructor and when you create parent and child classes of your own, the child classes use three constructors.  
b. When you use a class as a superclass and the class has only constructors that require arguments, you must be certain that any subclasses provide the superclass constructor with the arguments it needs.

5. Accessing Superclass constructors. When a superclass has a default constructor, you can create a subclass with or without its own constructor. If the subclass contains no constructor, all subclass objects use the superclass default constructor when they are instantiated.
Format that calls a superclass constructor:

super(list of arguments)  ††† //super always refers to the superclass of the class in which you use it.

a. The
super() statement must be the first statement in any subclass constructor that uses it.
b. You cannot use both
this() and super() in the same constructor because both are required to be the first statement in any constructor.
c. You can use the keyword super from within a derived class method to access an overridden base class method.
d. You can use the keyword super from within a derived class method to access a base class method that has not been overridden.

6. Employing Information Hiding
. Without public get and set methods, there would be no way to access the private data fields.

You cannot access a private data member of an object with

instead you use

Information Hiding with private data can be altered by the methods you choose and only in ways that you can control. Private members of the parent class are not accessible within a childís class methods.
a. Using the keyword protected provides you with an intermediate level of security between public and private access. Protected members are those that can be used by a class and its descendants. When a child class is allowed direct access to a parentís fields, the likely hood of future errors increases.

7. Methods you cannot override in a subclass are:
a. Static methods - a subclass cannot override declared static method in the superclass.
b. A subclass cannot override final methods in it superclass.
c. In Java, instance method calls are virtual method calls - the method used is determined when the program runs.
d. A subclass cannot override methods that are declared static in the superclass.
e. A subclass cannot override methods that are declared final in the superclass.
f. A subclass can override private methods as well as public or protected ones.