Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Introduction to Inheritance in Computer Science, Study notes of Computer Science

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

Pre 2010

Uploaded on 07/30/2009

koofers-user-f0q
koofers-user-f0q 🇺🇸

2.7

(3)

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Computer Science
II
CS112-2008S-04
Inheritance
David Galles
Department of Computer Science
University of San Francisco
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Introduction to Inheritance in Computer Science and more Study notes Computer Science in PDF only on Docsity!

Introduction to Computer Science

II

CS112-2008S-

Inheritance

David Galles

Department of Computer Science University of San Francisco

04-0:

Classes Want to create a bunch of classes for a real estateprogram

Apartment Building:

Square Footage Number of windows Number of floors Number of units

04-2:

Classes Want to create a bunch of classes for a real estateprogram

Office Building

Square Footage Number of windows Number of floors Number of Elevators

04-3:

Classes Several data members (and methods) that areshared across all building types:

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:

Inheritace Add “extends ” to class defition

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:

Inheritace Add “extends ” to class defition

class Apartment extends Building { ... } Subclass inherits all of the methods / data from thesuperclass.

Examples from code

04-8:

Super & Constructors If you do not call the constructor of the superclassexplicitly, then the default (no parameter) version ofthe constructor is called automatically at thebeginning of the constructor Each class takes care of itself

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:

Access control public: Anyone use it protected: Only subclasses can use it public: No one but the original class can use it.

Examples

04-11:

Using Super

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:

Class Hierarchy