Title: Inheritance Odds & Ends
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Inheritance Odds & Ends

1. Quick review of textbook's Employee, HourlyEmployee, SalariedEmployee
2. Note the super calls in HourlyEmployee and SalariedEmployee. Note that leaving off a super call results in the default super call.
3. Usage of the keyword "this" to have a constructor call another constructor. Take a look at Employee to see how this could be done.
4. Take a look IsADemo.java and note that both classes can be considered Employee type.
5. Note that we can extend an existing class and get:
  - some methods stay the same and are just inherited
  - some methods are modified and we perhaps still use the original method
  - some methods are just new: Go over StringTokenizer and EnhancedStringTokenizer.java -- note the new routine:
public String[] tokensSoFar()
  - Demonstrate that overriding a method name with a different calling sequence is overloading: Try
String[] nextToken(in num)
  - demonstrate that an overriden method cannot become less accessable with nextToken.
  - Overriden methods can become more accessable:



public class Private2Public {
    private void method()
    {
        System.out.println("Private2Public.method()");
    }

}
// Second file
public class ExtendedClass extends Private2Public
{
    public void method(){
        System.out.println("ExtendedClass.method");
        //super.method();
    }

    public static void main(String[] args)
    {
        ExtendedClass ec = new ExtendedClass();
        ec.method();
    }
}



6. Note that in overriding, the return type must also stay the same with one exception. In release 1.5 it was allowed to return "covariant" return types. So a return of Employee could be changed to HourlyEmployee. Note that an HourlyEmployee has an "Is A" relationship to Employee. Try the method:
Employee getClone()
7. Demonstrate protected method/instance variable.

Last Modified: October 26, 2013 0:16 AM