Accessing Private Attributes and Invoking Inherited Methods in Java: A Guide, Slides of Java Programming

An overview of accessing private attributes from ancestor classes and invoking inherited methods of the same name in java. It covers the use of protected modifier, super keyword, and strategies for creating classes for different types of rocks. The document also includes examples and coding instructions.

Typology: Slides

2011/2012

Uploaded on 07/07/2012

kolii
kolii 🇮🇳

21 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Accessing Private Information from an
Ancestor Class
When the ancestor class declares an
attribute as private
both users and descendents of the class are
prevented from accessing the attribute
Java provides the protected modifier
users of the class are prevented
descendents of the class are allowed to
access the attribute
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Accessing Private Attributes and Invoking Inherited Methods in Java: A Guide and more Slides Java Programming in PDF only on Docsity!

Accessing Private Information from an

Ancestor Class

 When the ancestor class declares an

attribute as private

 both users and descendents of the class are

prevented from accessing the attribute

 Java provides the protected modifier

 users of the class are prevented

 descendents of the class are allowed to

access the attribute

Invoking an Inherited Method

of the Same Name

 We know that an inherited method can be

overridden by another method of the same

name

 most local (overriding) method takes precedence

 Occasionally we wish to call the inherited

method

 If B extends A and both have a method m()

 The m() method for A can be called from inside B

 Use the syntax super.m()

Objects

Object Type Kind Name

A rock Rock^ varying aRock

chalk Chalk^ constant

shale Shale^ constant

......

description String^ varying aRock.getDescription()

We need classes for

each kind of rock

Strategy

 Create a class for each kind of rock

 Note that classification is a characteristic

of rocks

 represent classification with a String

attribute

 supply a getClassification() method

 Summarize relationships with class

hierarchy diagram

Operations

At the very least, each class should have:

 A constructor

 Accessor methods for class attributes

 A toString() method to facilitate output

Also:

 getDescription() method in each class

 Attribute variable and accessor for which of the

three categories

Coding

 Class declarations, Figures 11.

through 11.

 Algorithm

1. Prompt user for name of rock

2. Read name into rockString

3. Build instance of class whose name is

stored in rockString

4. Display result of sending that object the

getDescription() message

Constructing an Object from a String

 Returns instance of class whose name stored in

StringVariable

 Created using default constructor of that class

 newInstance returns that instance as an Object

 It must be cast into appropriate type (usually nearest

ancestor)

Form:

class.forName(StringVariable).newInstance()

Where:

StringVariable refers to a String containing

the name of a class

Purpose:

11.4 Example: An O-O Payroll Program

 Consider a program to generate monthly

paychecks for employees.

 Includes different kinds of workers, paid

different ways

 managers & programmers, salary

 secretaries & consultants, hourly

 Program should read sequence of

employees from file, compute pay, print

paycheck

Analysis

 Common attributes

 salary for managers, programmers

 hours and hourly wage for secretaries and

consultants

 name, id, pay, etc. for all employees

 Suggests superclass of Employee,

subclasses of:

Salaried employee

manager

programmer

Hourly employee

Consultant

secretary docsity.com

Hierarchy

Payroll
Generator
Manager Programmer
Salaried
Employee
Consultant Secretary
Hourly
Employee
Employee Paycheck
Object

File Format

 First line contains number of employees to

be read from file

 Subsequent lines contain info in employee

records

 note that different kinds of records contain

different kinds of attributes

 salaried employee records have salary

 hourly employee records have both hours and

wages

Algorithm

1. Read the sequence of employee records

from input file into employee array

2. For each index i of employee

a) build a paycheck for employee[i]

b) output that paycheck

 Note class declarations, Figures 11.

through 11.23, PayrollGenerator

program, Figure 11.

11.5 Graphical/Internet Java:

A Function Plotter

 We seek a program for use in a

mathematics class

 plots the graph of a function

 helps visualize function behavior

 Program allows user to:

 choose a function to be plotted

 specify range of values

 Program should be easily expandable for

including new functions docsity.com

Behavior

Function Plotter

Function X max 4.0 Y max 4.

X min 4.0^ Y min 4.0^ Colors

(Clear)

Function chosen will be

graphed on the axes

Sine

Cosine

Log

Power