Vous êtes sur la page 1sur 10

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

Vous aimerez peut-être aussi