Inheritance- Subclasses | Classes and Objects - Control Structures - Introduction to Computers & Engineering
Inheritance- Subclasses | Classes and Objects - Control Structures - Introduction to Computers & Engineering
00 Lecture 13
Inheritance
Inheritance
• Inheritance allows you to write new classes
based on existing (super or base) classes
– Inherit super class methods and data
– Add new methods and data
• This allows substantial reuse of Java code
– When extending software, we often write new code that
invokes old code (libraries, etc.)
– We sometimes need to have old code invoke new code
(even code that wasnt imagined when the old code was
written), without changing (or even having) the old code
• E.g., A drawing program must manage a new shape
– Inheritance allows us to do this also
1
Access for inheritance
• Class may contain members (methods or data) of
type:
– Private:
• Access only by classs methods
– Protected
• Access by:
– Classs methods
– Methods of inheriting classes, called subclasses or derived
classes
– Classes in same package
– Package:
• Access by methods of classes in same package
– Public:
• Access to all classes everywhere
A Programming Project
• Department has system with Student class
– Has extensive data (name, ID, courses, year, ) for all
students that you need to use/display
– Department wants to manage research projects better
• Undergrads and grads have very different roles
– Positions, credit/grading, pay,
– You want to reuse the Student class but need to add very
different data and methods by grad/undergrad
• Suppose Student was written 5 years ago by someone else
without any knowledge that it might be used to manage
research projects
2
Classes and Objects
Encapsulation Message passing Main method
Inheritance
Class Student
Already written:
firstName printData
lastName
dept
is-a is-a
3
Inheritance, p.2
Class Grad
firstName
lastName
dept
gradSalary is-a
printData
getPay Class SpecGrad
firstName
lastName
dept
gradSalary
printData
getPay specStipend
4
Exercise: Undergrad class
• Write an Undergrad class as a derived or subclass:
– Class declaration:
• public class Undergrad extends Student
– Add private double variables underWage and underHours
– Constructor: How many arguments does it have?
• Invoke superclass constructor in 1st line of body:
super( <arguments> ) // Use actual arguments
• And then set the two new private variables as usual
– Method getPay() returns double underWage *
underHours
– Method printData() prints name and pay (void)
• Use superclass printData() method to print name in 1st line:
super.printData();
• Write a second line to System.out.println weekly pay
5
Exercise: Special Grad class
• Write SpecGrad class as derived or subclass:
– Class declaration: extends _______
– Add private double variable specStipend
– Constructor: How many arguments does it have?
• Invoke superclass constructor: super(<arguments>)
• And then set the new private variable
– Method getPay() returns double specStipend
– Method printData() prints name and pay (void)
• Use superclass printData() method to print name and
monthly salary (which is zero)
• Write second line to print stipend
– A special grad gets only a stipend, not a monthly
salary. Well discuss it in solutions.
Exercise: main()
6
Main method
public class StudentTest {
public static void main(String[] args) {
Undergrad ferd= new Undergrad("Ferd", "Smith", 12.00, 8.0);
ferd.printData();
Grad ann= new Grad("Ann", "Brown", 1500.00);
ann.printData();
SpecGrad mary= new SpecGrad("Mary", "Barrett", 2000.00);
mary.printData();
System.out.println();
7
StudentTest with input
import javax.swing.*;
public class StudentTestWithInput {
public static void main(String[] args) {
Student[] team = new Student[3];
for (int i= 0; i < team.length; i++) {
String type = JOptionPane.showInputDialog("Enter type");
String fname = JOptionPane.showInputDialog("Enter fname");
String lname = JOptionPane.showInputDialog("Enter lname");
String payStr = JOptionPane.showInputDialog("Enter pay");
double pay= Double.parseDouble(payStr);
if (type.equals("Grad"))
team[i]= new Grad(fname, lname, pay);
else if (type.equals("SpecGrad"))
team[i]= new SpecGrad(fname, lname, pay);
else
team[i]= new Undergrad(fname, lname, pay, 8.0);
}
// Polymorphism, and late binding
for (int i = 0; i < 3; i++) {
System.out.print(team[i].getClass()+ ": ");
team[i].printData(); } } }
Exercise
• In class Grad:
– Change printData() to use getPay() instead of
explicitly printing gradSalary
– Save/compile and run StudentTest
– What happens?
– Why?
8
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.