









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An excerpt from a computer science course at the university of san francisco, focusing on the concept of inheritance in object-oriented programming. How to create classes for different types of buildings, such as apartments, houses, and office buildings, and the benefits of using inheritance to reduce repetition. The document also covers the use of constructors, access control, and method overriding in the context of inheritance.
Typology: Study notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!
David Galles
Department of Computer Science University of San Francisco
04-0:
Apartment Building:
Square Footage Number of windows Number of floors Number of units
04-2:
Office Building
Square Footage Number of windows Number of floors Number of Elevators
04-3:
Square footage Number of Windows floors We could duplicate these variables in all of ourclasses (so that the House class and Office classand Apartment class would all contain a definitionof an instance variable for Square Footage, forexample)
What are potential problems with thisapproach?
04-5:
class Apartment extends Building { ... } Defines an “is-a” relationship
Apartment is a building Defines a superclass/subclass relationship
Building is the superclass Aparment is the subclass
04-6:
class Apartment extends Building { ... } Subclass inherits all of the methods / data from thesuperclass.
Examples from code
04-8:
Don’t need to worry (too much) about the innerworkings of a superclass when writing asubclass Just concentrate on the new stuff
(examples)
04-9:
Examples
04-11:
class SuperClass {
void print() {
System.out.println("Message from SuperClass"); } } class SubClass extends SuperClass {
void print() {
System.out.println("Message from SubClass");super.print(); } }
04-12: