Home arrow Java arrow Page 7 - Introducing Classes and More in Java
JAVA

Introducing Classes and More in Java


This article introduces you to the Java programming language. The second of two parts, it 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).

Author Info:
By: Sams Publishing
Rating: 3 stars3 stars3 stars3 stars3 stars / 2
May 11, 2006
TABLE OF CONTENTS:
  1. · Introducing Classes and More in Java
  2. · Running the Program
  3. · Organizing Classes and Class Behavior
  4. · Creating a Class Hierarchy
  5. · Inheritance in Action
  6. · Summary
  7. · Questions and Answers

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
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

  1. What is another word for a class?

  1. Object

  2. Template

  3. Instance

  1. When you create a subclass, what must you define about that class?

  1. It already is defined.

  2. Things that are different from its superclass.

  3. Everything about the class.

  1. What does an instance method of a class represent?

  1. The attributes of that class.

  2. The behavior of that class.

  3. The behavior of an object created from that class.

Answers

  1. b. A class is an abstract template used to create objects similar to each other.

  2. 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.

  3. 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?

  1. All objects created from the same class must be identical.

  2. All objects created from the same class can be different from each other.

  3. An object inherits attributes and behavior from the class used to create it.

  4. 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:

  1. In the main() method of the VolcanoRobot class, create a second VolcanoRobot robot named virgil, set up its instance variables, and display them.

  2. 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.

blog comments powered by Disqus
JAVA ARTICLES

- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials