Inheritance

What is inheritance
Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.
http://java.sun.com/docs/books/tutorial/java/concepts/inheritance.html

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses:

http://java.sun.com/docs/books/tutorial/java/concepts/inheritance.html

Exercise
In Groups of 3/4 do the following • If I have a class Shape – what variations might you have and what would the Superclass have in common with all of the sub classes • If I have a class Clock – what variations might you have and what would the Superclass have in common with all of the sub classes • If you have a class Employee – what variations might you have and what would the Superclass have in common with all the sub classes

Extends
The important thing with all of the relationships of the sub-classes to that of the super-class is that they extend the super-classs’ functionality • Triangle extends shape • Alarmclock extends clock • Manager extends employee

Employee
• package employeemanagerrelationship; public class Manager extends Employee { private Employee[] m_empList; public Employee[] getEmployeeList() { return m_empList; } public void initList(int _count) { m_empList = new Employee[_count]; } /* ... add the rest of the methods ...*/ /** Creates a new instance of Manager */ public Manager() { } public Manager(String _mngrName, int _empNo) { setName(_mngrName); setEmployeeNumber(_empNo); } } /** * * @author Annamarie Kelly */ public class Employee { private String m_name; private int m_empNo; public String getName() { return m_name; } public void setName(String _name) { m_name = _name; } public int getEmployeeNumber() { return m_empNo; } public void setEmployeeNumber(int _empNo) { m_empNo = _empNo; } public Employee() {} /** Creates a new instance of Employee */ public Employee(String _name, int _empNo) { m_name = _name; m_empNo = _empNo; } }

Driver for Employee
public class Main { /** Creates a new instance of Main */ public Main() { } /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Employee[] company = new Employee[2]; company[0] = new Employee("Sales guy 1", 1024); company[1] = new Manager("Manager guy 1", 1025); for(int i=0; i< 2; i++) { System.out.println(company[i].getName() + " Emp#" + company[i].getEmployeeNumber()); }

Exercise 2
Enter in the 3 classes as shown • Employee • Manager • Main Follow what is happening and put in comments as appropriate

Exercise 3
• Add in a new class – parttime • Change your Main program to output the new guy (parttime guy who is number 1026)

Homework
• Complete the exercises handed out

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.