{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
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 wasn’t 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
Image of page 1

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

View Full Document Right Arrow Icon
Review: Member Access Class may contain members (methods or data) of type: Private: Access only by class’s methods Protected (rarely used in Java; it’s pretty unsafe) Access by: Class’s 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
Image of page 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
Image of page 3

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

View Full Document Right Arrow Icon
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.
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern