Beginning Java Objects From Concepts to Code by 2023

Beginning Java Objects From Concepts to Code by 2023

Author:2023
Language: eng
Format: epub


Chapter 7 Some Final objeCt ConCeptS

In this chapter, you’ll learn

• How a single line of code, representing a message—for example,

x.foo();—can exhibit a variety of behaviors at run time

• How we can specify what an object’s mission should be without

going to the trouble of specifying the details of how the object is to

carry out that mission and also under what circumstances we’d want

to be able to do so

• How an object can have a “split personality” by exhibiting the

behaviors of two or more different types of object

• Creative ways for an entire class of objects to easily and efficiently

share data without breaking the spirit of encapsulation

• How features can be defined that are associated with a class as a

whole rather than with an instance of a class and how we can take

advantage of this capability to design utility classes

• How we may declare variables whose values, once assigned, remain

constant while an application is executing

Polymorphism

The term polymorphism refers to the ability of two or more objects belonging to

different classes to respond to exactly the same message (method call) in different class-specific ways.

As an example, if we were to instruct three different people—a surgeon, a hair stylist,

and an actor—to “Cut!” then

• The surgeon would begin to make an incision.

• The hair stylist would begin to cut someone’s hair.

• The actor would abruptly stop acting out the current scene, awaiting

directorial guidance.

These three different professionals may be thought of as Person objects belonging to

different professional subclasses: Surgeon, HairStylist, and Actor. Each was given the

same message—“Cut!”—but carried out the operation differently as prescribed by the

subclass that each belongs to.

368

Chapter 7 Some Final objeCt ConCeptS

Let’s now turn to a software example relevant to the SRS. Assume that we’ve defined

a Student superclass and two subclasses, GraduateStudent and UndergraduateStudent.

In Chapter 5, we discussed the fact that a print method designed to print the values of all of a Student’s attributes wouldn’t necessarily suffice for printing the attribute values

for a GraduateStudent, because the code as written for the Student superclass wouldn’t

know about any attributes that may have been added to the GraduateStudent subclass.

We then looked at how to override the print method of Student to create specialized versions of the method for all of its subclasses. The syntax for doing so, which was first

introduced in Chapter 5 with the GraduateStudent class, is repeated again here for your review. I’ve added the UndergraduateStudent class code, as well:

//-------------

// Student.java

//-------------

public class Student {

private String name;

private String studentId;

private String major;

private double gpa;

// Public get/set methods would also be provided (details omitted) ...

public void print() {

// We can print only the attributes that the Student class

// knows about.

System.out.println("Student Name: " + getName() + "
" +

"Student No.: " + getStudentId() + "
" +

"Major Field: " + getMajor() + "
" +

"GPA: " + getGpa());

}

}

//---------------------

// GraduateStudent.java

//---------------------

369

Chapter 7 Some Final objeCt ConCeptS

public class GraduateStudent extends Student {

// Adding several attributes.

private String undergraduateDegree;

private String undergraduateInstitution;

// Public get/set methods would also be provided (details omitted) ...

// Overriding the print method.

public void print() {

// Reuse code from the Student superclass ...

super.print();

// ... and then go on to print this subclass's specific attributes.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.