Reusing Code in Java: CSCI 1302 Guided Notes: Chapter 11 - Inheritance Dr. Phelps

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CSCI 1302 Guided Notes: Chapter 11 – Inheritance Dr.

Phelps

Reusing Code in Java

There are two ways to reuse code in Java:


 Aggregation (Composition) – A class has a field that is an object.
An Employee has different attributes. An Employee HAS A Date field named hireDate
 Inheritance (Extending classes) – A new class extends the definition of an existing class by adding
fields and methods. This new class is a specialized version of the existing class.
There are different types of Employees – Hourly Worker, Salaried Worker and Commissioned Worker.
An HourlyWorker IS AN Employee.

Employee
-name: String
-hireDate: Date AGGREGATION
+ Employee():
+setName(n:String):void
+setHireDate(d:Date):void
+getName():String
+getHireDate(): Date
+ toString(): String

Date HourlyWorker
- month: int - hours: double
- day: int - payRate:double
- year : int INHERITANCE
+ Date(): +setHours(h:double):void
+Date(obj:Date): +setRate(r:double):void
+Date(m:int, d:int, year:int): +getHours():double
+Date(d:String): +getRate():doubble
+setMonth(m: int): void +getPay(): void
+set Day(d: int): void +toString():String
+setYear(y:int): void
+ toString(): String
+copy():Date
+getDay(): int
+getMonth(): int
+getYear(): int

An HourlyWorker can access all of the public methods in Employee:

HourlyWorker hw = new HourlyWorker();


hw.setName(“Erica Hill”);
hw.setHireDate( new Date(10,11,2000));
hw.setRate(10.00);
hw.setHours(40);
System.out.println(“Hourly Worker: “+ hw);

1302GuideC11Inheritance.docx Page 1 of 3
CSCI 1302 Guided Notes: Chapter 11 – Inheritance Dr. Phelps

public class Employee public class HourlyWorker extends Employee


{ {
private String name; private double hours;
private Date hireDate; private double rate;

public Employee() public void setHours(double h)


{ {
name =""; hours = h;
hireDate = new Date(); //allocate space for date }
}
public void setRate(double r)
public void setName(String n) {
{ rate = r;
name = n; }
}
public double getHours()
public void setHireDate(Date d) {
{ //copies date into hireDate return hours;
hireDate .setMonth(d.getMonth()); }
hireDate.setDay(d.getDay());
hireDate.setYear(d.getYear()); public double getRate()
} {
return hours;
public String getName() }
{
return name; public double getPay()
} {
return (rate *hours);
public Date getHireDate() }
{ // don’t return address of object. instead
// send the address of a copy of the object. public String toString()
return hireDate.copy(); {
} // Calls Employee’s methods to get name,
// and hireDate
public String toString() return “Name:” + getName()
{ //implicit call to Date’s toString() + ”*“ +getHireDate()+ “+”
return name+"-"+hireDate; + ” Pay:” + getPay()
} + “Hours: “ +getHours()
+ “ Rate: “ +getRate();
} }
}

1302GuideC11Inheritance.docx Page 2 of 3
CSCI 1302 Guided Notes: Chapter 11 – Inheritance Dr. Phelps

Inheritance Exercises
1. Aggregation and Inheritance are two ways to reuse code in Java. How does inheritance differ from
aggregation?

2. What is a super class? What is a subclass?

3. In this first line of a class declaration, what is the name of the superclass? What is the name of the
subclass? public class Checking extends BankAccount

4. Look at the following class declaration and answer the questions that follow them:
public class DemoA public class DemoB extends DemoA
{ {
private int a; private int b;
public void setA(int x) public void setB(int x)
{ {
a = x; b = x;
} }
public void getA() public void getB()
{ {
return a; return b;
} }
} }

a) When a DemoB object is created what are it public members?

b) What members of DemoA class are not accessible to the DemoB class’ methods?

c) Indicate whether the following statements are illegal or legal


1. DemoA objA = new DemoA();
2. DemoB objB = new DemoB();
3. objA.setA(1);
4. objB.setA(2);
5. objA.setB(3);
6. objB.setB(4);

1302GuideC11Inheritance.docx Page 3 of 3

You might also like