Reusing Code in Java: CSCI 1302 Guided Notes: Chapter 11 - Inheritance Dr. Phelps
Reusing Code in Java: CSCI 1302 Guided Notes: Chapter 11 - Inheritance Dr. Phelps
Reusing Code in Java: CSCI 1302 Guided Notes: Chapter 11 - Inheritance Dr. Phelps
Phelps
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
1302GuideC11Inheritance.docx Page 1 of 3
CSCI 1302 Guided Notes: Chapter 11 – Inheritance Dr. Phelps
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?
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;
} }
} }
b) What members of DemoA class are not accessible to the DemoB class’ methods?
1302GuideC11Inheritance.docx Page 3 of 3