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
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.
Ajax | Assembly Language Programming |
Borland Delphi | C & C++ |
C# | CSS |
Compiler Design | Compilers |
DHTML | Debugging |
Delphi | Fortran |
Java | Lisp |
Perl | Prolog |
Python | RPG |
Ruby | Swift |
Visual Basic | XHTML |
XML | XSL |
Deep Learning with Python by François Chollet(12555)
Hello! Python by Anthony Briggs(9904)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9785)
The Mikado Method by Ola Ellnestam Daniel Brolund(9769)
Dependency Injection in .NET by Mark Seemann(9329)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8282)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7756)
Grails in Action by Glen Smith Peter Ledbrook(7686)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7550)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7007)
Microservices with Go by Alexander Shuiskov(6774)
Practical Design Patterns for Java Developers by Miroslav Wengner(6684)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6629)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6399)
Angular Projects - Third Edition by Aristeidis Bampakos(6035)
The Art of Crafting User Stories by The Art of Crafting User Stories(5566)
NetSuite for Consultants - Second Edition by Peter Ries(5497)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5302)
Kotlin in Action by Dmitry Jemerov(5048)
