Lecture on Inheritance - Object-Oriented Programming I | CMSC 131, Study Guides, Projects, Research of Computer Science

Material Type: Project; Professor: Cleaveland; Class: OBJECT-ORIENTED PROG I; Subject: Computer Science; University: University of Maryland; Term: Fall 2006;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 07/30/2009

koofers-user-n20
koofers-user-n20 🇺🇸

9 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
11/17/2006 CMSC 131 Fall 2006
Rance Cleaveland
©2006 Univeristy of Maryland
Lecture 34:
Inheritance
Last time:
1. Inheritance
Today:
1. Project #7 assigned
2. Method overriding
3. Shadowing
4. Access issues in inheritance
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Lecture on Inheritance - Object-Oriented Programming I | CMSC 131 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

11/17/

CMSC 131 Fall 2006Rance Cleaveland

Lecture 34:Inheritance ©2006 Univeristy of Maryland

Last time: 1.

Inheritance

Today: 1.

Project #7 assigned

Method overriding

Shadowing

Access issues in inheritance

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

Project #7 Assigned! 

Project due Wednesday, 11/29 at 11 pm

Project is

closed



You must complete the project by yourself 

Assistance can only be provided by teaching assistants(TAs) and instructors 

You must not look at other students' code

Start now! 

Read entire assignment from beginning to end beforestarting to code 

Check out assignment now from CVS 

Follow the instructions

exactly

, as much of grading is

automated

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

Recap ^

Inheritance occurs when one class (

derived class, subclass

) is

defined from another class (

base / parent class, superclass

).

^

To derive a class D from a base class B, use:

public class

D^

extends

B^

{ …

}

^

Derived class inherits all instance variables, methods from baseclass. It can also define new instance variables, methods ^

In derived-class constructor,

super

(

)

can be used to invoke

constructor from base class ^

Derived class can explicitly refer to entities from base class using super

, e.g.

super.toString(

)

^

Polymorphism

: object in derived class can be used anywhere

base class is expected (a

Student

is a

”^

Person

!)

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

University Person Recap

class

:^

Person

instance variables

:

String

name

String

idNum

methods

:

Person( … )

[various]

String

getName( )

String

getIdNum( )

void

setName(

String

)

void

setIdNum(

String

)

String

toString( )

boolean

equals(

Person

)

class

:^

Student

instance variables

:

int

admitYear double

gpa

methods

:

Student( … )

[various]

int

getAdmitYear( ) double

getGpa( )

void

setAdmitYear(

int

)

void

setGpa(

double

)

String

toString( )

boolean

equals(

Student

)

extends Person

class

:^

Faculty

instance variables

:

int

hireYear methods

:

Faculty( … )

[various]

int

hireYear( ) void

setHireYear(

int

)

String

toString( )

boolean

equals(

Student

)

extends Person

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

super

vs.

this

^

super

: refers to the base class

^

Can invoke any base class constructor using

super( …

^

Can access data and methods in base class (

Person

) via

super

E.g.,

toString(

,^ equals(

invoke the corresponding methods from

Person

base class using

super.toString(

and

super.equals(

^

this

: refers to current class / object

^

Can refer to own data and methods using

this

(usually unnecessary)

^

Can invoke any of own constructors using

this( …

. Like

super:

^

Can only be done within a constructor ^

Must be the first statement of the constructor ^

Example public

Faculty(

Faculty

f

this(

f.getName(

f.getIdNum(

f.hireYear

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

Method Overriding ^

A derived class can define new instance variables and methods (e.g. hireYear

and

getHireYear( )

^

A derived class can also redefine (

override

) existing methods

public

class

Person

public

String

toString(

} public

class

Student

extends

Person

public

String

toString(

} Student

bob

new

Student("Bob

Goodstudent","123-45-6789",2004,4.

System.out.println(

"Bob's

info:

bob.toString(

Overrides base-classdefinition of this method

Since bob is Student,Student toString used

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

Early vs. Late Binding ^

Consider:

Faculty

carol

new

Faculty("Carol

Tuffteacher","999-99-9999",

Person

p

carol;

System.out.println(

p.toString()

^

Which version of

toString

Person

or

Faculty

  • is called?

^

Early (static) binding ^

p^

is declared to be of type

Person

^

Therefore, the

Person

version of

toString

is used

^

Late (dynamic) binding ^

The object to which

p

refers was created as

Faculty

object

^

Therefore, the

Faculty

version of

toString

is used

^

Java uses late binding

(C++ uses early binding)

^

Early binding is more efficient (decisions about method versions can be madeat compile time)

^

Late binding respects encapsulation (object defines its operations when it iscreated)

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

Disabling Overriding with final 

Sometimes you do not want to allow methodoverriding. 

Correctness Redefining a method for a derived class might break it 

Efficiency ^

Late binding is less efficient than early binding ^

If you know that no subclass will redefine your method, youcan force early binding by disabling overriding

We can disable overriding by declaring a method tobe

final

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

Shadowing ^

Can we override instance variables just like methods? ^

Yes, but be careful! ^

Overriding instance variable is called

shadowing

^

Shadowing hides instance variables of base class (can stillaccess them using

super.varName

public

class Person

String

name;

} public

class Administrator extends Person

String

name;

name

refers

to

Administrator’s

name

^

Confusing! Better to pick a new variable name

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

Inheritance and

private

^

Student

inherits all private data (

name

and

idNum

) from

Person

^

However, private members of base class cannot be accessed directly

public

class

Student

extends

Person

… public

void

someMethod(

name

“Mr.

Foobar”;

Illegal!

} public

void

someMethod2(

setName(

“Mr.

Foobar”

OK

^

Why? ^

Although

Student

inherits from

Person

^

… they are

different

classes

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

Package access 

When a class member is not given any accessmodifier (

private, public, protected

) it is

said to have

package access

Such members are accessible any class in the samepackage

Example:

package

person;

public

class

Person

{

int computeAge

()

{

}

//

Package

access

}

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

Which Access Policy To Use? ^

public ^

Methods in the public interface

^

Constant variables (

final

,^ static final

^

private ^

Variables other than constants

^

Internal helper/utility methods not intended for use outside class

^

protected ^

Helper / utility methods that may be useful in subclasses ^

Some style gurus discourage

protected

.^

They consider package

access safer, since trouble can be localized to current package.

^

Package ^

Helper / utility methods that may be useful in rest of package