lecture12

lecture12 - 1.00 Lecture 12 October 4, 2005 Inheritance...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1.00 Lecture 12 October 4, 2005 Inheritance Inheritance Inheritance allows you to write new classes based on existing (super) 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! 1 Review: Member Access Class may contain members (methods or data) of type: Private: Access only by classs methods Protected (rarely used in Java; its pretty unsafe) Access by: Classs methods Methods of inherited classes, called subclasses 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 Dept 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 ... ... } Student s1 firstName ) Message passing public main(){ Student s1(Mary,Wang); S1.printData( ); // Prints s1 data lastName dept Request(args) Response(ret val, args printData() Encapsulation Main event loop private: Inheritance Class Student Class Grad is-a is-a firstName lastName printData() Class Undergrad underWage underHours firstName lastName dept gradSalary firstName lastName dept printData() getPay() printData() getPay() Already written: You next write: 3 dept Inheritance, p.2 Class Grad Class SpecGrad is-a gradSalary firstName lastName dept printData() getPay() firstName lastName dept printData() getPay() gradSalary specStipend Example: Student class class Student { // Constructor public Student(String fName, String lName) { firstName= fName; lastName= lName;} // Method public void printData() { System.out.println(firstName + " " + lastName);} // Data private String firstName; private String lastName; } This is the super (or base) class. 4 Undergrad student class class Undergrad extends Student { // extends keyword // Constructor, calls superclass constructor in 1 st line public Undergrad(String fName, String lName, double rate, double hours) { super(fName, lName); // invokes Student constructor underWage= rate; underHours= hours; } public double getPay() { return underWage * underHours; } public void printData() { super.printData(); // Student getData() method...
View Full Document

This note was uploaded on 11/29/2011 for the course CIVIL 1.00 taught by Professor Georgekocur during the Spring '05 term at MIT.

Page1 / 15

lecture12 - 1.00 Lecture 12 October 4, 2005 Inheritance...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online