0% found this document useful (0 votes)
13 views21 pages

04 OOP Inheritance

The document outlines the principles of object-oriented programming, focusing on inheritance and code reuse in software engineering. It presents a scenario involving different employee types and their common and unique behaviors, emphasizing the importance of is-a relationships and inheritance hierarchies. Additionally, it discusses the implications of modifying superclass constructors and fields, as well as the need for accessor methods in subclasses.

Uploaded by

tam.le2302220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views21 pages

04 OOP Inheritance

The document outlines the principles of object-oriented programming, focusing on inheritance and code reuse in software engineering. It presents a scenario involving different employee types and their common and unique behaviors, emphasizing the importance of is-a relationships and inheritance hierarchies. Additionally, it discusses the implications of modifying superclass constructors and fields, as well as the need for accessor methods in subclasses.

Uploaded by

tam.le2302220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CS206V: Object Oriented

Programming
(Spring 2024)
Dr. Cao Tien Dung
School of Information Technology – Tan Tao University
The software crisis
• software engineering: The practice of developing,
designing, documenting, testing large computer
programs.

• Large-scale projects face many issues:


• getting many programmers to work together
• getting code finished on time
• avoiding redundant code
• finding and fixing bugs
• maintaining, improving, and reusing existing code

• code reuse: The practice of writing program code


once and using it in many contexts.

Spring'2024 Cao Tien Dung, PhD. 2


Company scenario
• In company, we have many types of employee:
Marketer, Secretary, Legal secretary, Lawyer.
• Common regulations for each employee:
• Work hour: 40h/week
• Vacation: 12days / year
• Salary: 40 000$/year
• yellow form to apply annual leave
• Each type of employee has some unique behavior.
• Marketers know how to advertise
• Secretaries know how to take dictation
• Legal secretaries know how to prepare legal documents
• Lawyers know how to sue

Spring'2024 Cao Tien Dung, PhD. 3


Is-a-Relationship, hierarchies
• is-a relationship: A hierarchical connection
where one category can be treated as a
specialized version of another
• Marketer, Secretary, Lawyer are the employees but:
• Marketer gets an extra salary 5000$.
• Lawyer get en extra salary 10000$ and vacation form is
“prink”.
• A Legal secretary is a secretary.
• inheritance hierarchy: A set of classes connected
by is-a relationships that can share common code.
Employee

Marketer Secretary Lawyer

Spring'2024 Cao Tien Dung, PhD. Legal secretary 4


An Employee class
public class Employee{
public int getWorkHours() {
return 40; //40 hours per week
}
public int getVacationDays() {
return 12; //12 days per year
}
public int getSalary() {
return 40000; //40000$ per year
}
public String getVacationForm() {
return “yellow” //use the yellow form
}
}

Spring'2024 Cao Tien Dung, PhD. 5


Redundant Marketer class
public class Marketer{
public int getWorkHours() {
return 40; //40 hours per week
}
public int getVacationDays() {
return 12; //12 days per year
}
public int getSalary() {
return 45000; //45000$ per year
}
public String getVacationForm() {
return “yellow” //use the yellow form
}
public void advertise(String adv) {
System.out.println(“Advertise: ” + adv);
}
}

Spring'2024 Cao Tien Dung, PhD. 6


Share code
advertise is the only unique behavior in Marketer
getSalary is an modification from Employee

• We'd like to be able to say:


public class Marketer{
copy all the content of Employee to here
public int getSalary() { //modify salary
return 45000; //45000$ per year
}
public void advertise(String adv) { //add new function
System.out.println(“Advertise: ” + adv);
}
}

Spring'2024 Cao Tien Dung, PhD. 7


Inheritance
• inheritance: A way to form new classes based on
existing classes, taking on their
attributes/behavior.
• a way to group related classes
• a way to share code between two or more classes
• One class can extend another, absorbing its
data/behavior.
• superclass: The parent class that is being extended.
• subclass: The child class that extends the superclass
and inherits its behavior.
• Subclass gets a copy of every field and method from superclass

Spring'2024 Cao Tien Dung, PhD. 8


Inheritance syntax
public class name extends superclass {

• Example:
public class Marketer extends Employee {
...
}
• By extending Employee, each Marketer object now:
• receives a getWorkHours, getSalary, getVacationDays,
and getVacationForm method automatically
• can be treated as an Employee by client code (seen
later)

Spring'2024 Cao Tien Dung, PhD. 9


Improved Marketer code
// A class to represent marketers.
public class Marketer extends Employee {
//overrides
public int getSalary(){
return 45000;
}
public void advertise(String avd) {
System.out.println(”Advertise: " + adv);
}
}

• Now we only write the parts unique to each type.


• Marketer inherits getWorkHours, getSalary, getVacationDays, and
getVacationForm methods from Employee.
• Maketer modifies the getSalary method.
• Marketer adds the advertise method.

Spring'2024 Cao Tien Dung, PhD. 10


Levels of inheritance
• Multiple levels of inheritance in a hierarchy are
allowed.
• Example: A legal secretary is the same as a regular
secretary but can file legal briefs.

public class LegalSecretary extends Secretary {


...
}

• Exercise: Complete the Secretary and LegalSecretary


class.

Spring'2024 Cao Tien Dung, PhD. 11


Interacting with the super-class
• Company now change the common behavior
• The based salary of Employee now is raised to 50,000$, then
Marketers get 55,000$ and Lawyers get 60,000$.
• We must modify our code to reflect this policy
change?
public class Employee {
...
public double getSalary() {
return 50000.0; // $50,000.00 / year
}
...
}
• Are we finished?
• The Employee subclasses are still incorrect.
• They have overridden getSalary to return other values.
Spring'2024 Cao Tien Dung, PhD. 12
Calling overridden methods
• We modify getSalary of all subclass ? YES, BUT:
• Problem: The subclasses' salaries are based on the Employee salary, but the
getSalary code does not reflect this.

• Subclasses can call overridden methods with super


super.method(parameters)
• Example:
public class Marketer extends Employee{
public double getSalary() {
double baseSalary = super.getSalary();
return baseSalary + 5000.0;
}
...
}

Spring'2024 Cao Tien Dung, PhD. 13


Inheritance and constructors
• Imagine that we want to give employees more vacation
days the longer they've been with the company.
• For each year worked, we'll award 2 additional vacation
days.
• When an Employee object is constructed, we'll pass in the
number of years the person has been with the company.
• This will require us to modify our Employee class and add
some new state and behavior.
• Exercise: Make necessary modifications to the Employee
class.

Spring'2024 Cao Tien Dung, PhD. 14


Modified Employee class
public class Employee { public int getVacationDays() {
return 10 + 2 * years;
private int years;
}
public Employee(int initialYears) { public String getVacationForm() {
years = initialYears; return "yellow";
} }
}
public int getHours() {
return 40;
}
public double getSalary() {
return 50000.0;
}

Spring'2024 Cao Tien Dung, PhD. 15


Problem with constructors
• Now that we've added the constructor to the Employee
class, our subclasses do not compile. The error:
Lawyer.java:2: cannot find symbol
symbol : constructor Employee()
location: class Employee
public class Lawyer extends Employee {
^

• The short explanation: Once we write a constructor (that


requires parameters) in the superclass, we must now write
constructors for our employee subclasses as well.

• The long explanation: (next slide)

Spring'2024 Cao Tien Dung, PhD. 16


The detailed explanation
• Constructors are not inherited.
• Subclasses don't inherit the Employee(int) constructor.
• Subclasses receive a default constructor that contains:
public Lawyer() {
super(); // calls Employee() constructor
}

• But our Employee(int) replaces the default


Employee().
• The subclasses' default constructors are now trying to
call a non-existent default Employee constructor.

Spring'2024 Cao Tien Dung, PhD. 17


Calling superclass constructor
super(parameters);
• Example:
public class Lawyer extends Employee {
public Lawyer(int years) {
super(years); // calls Employee constructor
}
...
}
• The super call must be the first statement in the
constructor.

• Exercise: Make a similar modification to the


Marketer class.
Spring'2024 Cao Tien Dung, PhD. 18
Inheritance and fields
• Try to give lawyers $10000 for each year at the company:
public class Lawyer extends Employee {
...
public double getSalary() {
return super.getSalary() + 10000 * years;
}
...
}
• Does not work; the error is the following:
Lawyer.java:7: years has private access in Employee
return super.getSalary() + 10000 * years;
^

• Private fields cannot be directly accessed from


subclasses.
• One reason: So that subclassing can't break encapsulation.
• How can we get around this limitation?

Spring'2024 Cao Tien Dung, PhD. 19


Improved Employee code
• Add an accessor for any field needed by the
subclass.
public class Employee {
private int years;
public Employee(int initialYears) {
years = initialYears;
}
public int getYears() { return years; }
...
}
public class Lawyer extends Employee {
public Lawyer(int years) {
super(years);
}
public double getSalary() {
return super.getSalary() + 10000 * getYears();
}
...
Spring'2024 Cao Tien Dung, PhD. 20
Excercise
• Auto Showroom case study
• Vehicle
Vehicle Customer
• Car (sedan, SUV, van) buy

• Truck
order
• Customer
approve
• Individual sale

• Enterprise
• Employee Saler Manager
• Saler
• Manager

Spring'2024 Cao Tien Dung, PhD. 21

You might also like