Introducing Classes and More in Java - Questions and Answers
(Page 7 of 7 )
Q. In effect, methods are functions defined inside classes. If they look like functions and act like functions, why aren't they called functions?
A. Some object-oriented programming languages do call them functions. (C++ calls them member functions.) Other object-oriented languages differentiate between functions inside and outside a body of a class or object because in those languages the use of the separate terms is important to understanding how each function works. Because the difference is relevant in other languages and because the term method is now in common use in object-oriented terminology, Java uses the term as well.
Q. What's the distinction between instance variables and methods and their counterparts, class variables and methods?
A. Almost everything you do in a Java program involves instances (also called objects) rather than classes. However, some behavior and attributes make more sense if stored in the class itself rather than in the object. For example, the Math class in the java.lang package includes a class variable called PI that holds the approximate value of pi. This value does not change, so there's no reason different objects of that class would need their own individual copy of the PI variable. On the other hand, every String object contains a method called length() that reveals the number of characters in that String. This value can be different for each object of that class, so it must be an instance method.
Quiz
Review today's material by taking this three-question quiz.
Questions
What is another word for a class?
Object
Template
Instance
When you create a subclass, what must you define about that class?
It already is defined.
Things that are different from its superclass.
Everything about the class.
What does an instance method of a class represent?
The attributes of that class.
The behavior of that class.
The behavior of an object created from that class.
Answers
b. A class is an abstract template used to create objects similar to each other.
b. You define how the subclass is different from its superclass. The things that are similar are already defined for you because of inheritance. Answer a. is technically correct, but if everything in the subclass is identical to the superclass, there's no reason to create the subclass at all.
c. Instance methods refer to a specific object's behavior. Class methods refer to the behavior of all objects belonging to that class.
Certification Practice
The following question is the kind of thing you could expect to be asked on a Java programming certification test. Answer it without looking at today's material.
Which of the following statements is true?
All objects created from the same class must be identical.
All objects created from the same class can be different from each other.
An object inherits attributes and behavior from the class used to create it.
A class inherits attributes and behavior from its subclass.
The answer is available on the book's Web site at http://www.java21days.com. Visit the Day 1 page and click the Certification Practice link.
Exercises
To extend your knowledge of the subjects covered today, try the following exercises:
In the main() method of the VolcanoRobot class, create a second VolcanoRobot robot named virgil, set up its instance variables, and display them.
Create an inheritance hierarchy for the pieces of a chess set. Decide where the instance variables color, startingPosition, forwardMovement, and sideMovement should be defined in the hierarchy.
Where applicable, exercise solutions are offered on the book's Web site at http://www.java21days.com.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter one of Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280). Buy this book now.
|
|