Java
  Home arrow Java arrow Page 7 - Introducing Classes and More in Java
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Introducing Classes and More in Java
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2006-05-11

    Table of Contents:
  • Introducing Classes and More in Java
  • Running the Program
  • Organizing Classes and Class Behavior
  • Creating a Class Hierarchy
  • Inheritance in Action
  • Summary
  • Questions and Answers

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

       · This article is an excerpt from the book "Sams Teach Yourself Java 2 in 21 Days, 4th...
     

    Buy this book now. 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.

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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek