Inheritance

Defining Classes

 [modifer] class ClassName [extends SuperClassName] [implements ImplementedClassName] {
                .... define attributes and methods }

Defining Variables


 [access specifier] [storage specifier(s)]  TYPE attributeName [ = initial value] ;
 

Defining Methods

 [access specifier] [storage specifier(s)]  TYPE methodName(arguments) { local variables and statements }
 



Object class

public class Employee
{
    private String lastName;
    private String firstName;
    private String socialSecurityNumber;


......


   /**
     * return employee's name for display in list boxes, etc
     */
    public String toString()
    {
        return firstName + " "+ lastName;
     }
   /**
     * compare the two employee's sssn numbers to see if its the same employee (names might not be unique)
     */
     public boolean equals(Object obj)
     {
      return socialSecurityNumber.equals((Employee)obj.socialSecurityNumber);
     }
}
 

Casting Objects

        
         Employee e = new Employee("Doe","Jane","111-22-3333");

         comboBox.addItem(e); // adds Jane Doe's employee object to the combobox
 
       Employee x = comboBox.getItemAt(0);   // this line would cause a compile error since get returns a object of type Object

       Employee x = (Employee)comboBox.getItemAt(0);  // this line would be ok.

        for(int i = 0; i <= comboBox.getItemCount(); i++)
        {
           if(comboBox.getItemAt(i) instanceof Employee)
           {
             Employee e = (Employee)comboBox.getItemAt(i);
             ....
            }
             else
              if((comboBox.getItemAt(i) instanceof Customer)
              {
                Customer c = (Customer)comboBox.getItemAt(i);
                 ....
               }