Chapter 5. OOP Part II—Constructors and Visibility

  • Creating New Objects: Constructors

  • More About Methods

  • Variable-Arity Methods

  • Packages

  • How the JDK Finds Classes

  • Access Modifiers

  • Exercises

  • Some Light Relief—It's Not Your Father's IBM

All the way back in Chapter 2, we briefly mentioned two concepts that are used a lot with classes: constructors (to create new objects), and access control (to deliberately restrict or increase the visibility of classes and things in classes). This chapter is where we deliver the full details on these two topics. Constructors are usually described as being like methods “with a few differences” and method is the object-oriented name for a function or procedure. So there's a section on methods and parameter passing in here too. We look at the ways you can organize groups of classes into packages, and how you can store packages in jar files. At the end of this chapter, you'll have enough knowledge to read and write basic Java object-oriented programs.

Polymorphism Is a Long Word for a Short Topic

In Chapter 8, we cover inheritance and polymorphism. Don't be put off by the term polymorphism. It's just a long word meaning “how child classes provide specialized versions of the methods inherited from the parent”. You'll need to read and understand the advanced OOP chapter be an effective Java programmer. The good news is that object-oriented programming is based on a few simple ideas. Here are the key OOP ideas that we have met so far:

Object-oriented programming: key ideas so far

  • A class is another name for a datatype.

  • An object is a variable belonging to a class datatype.

  • The methods and data defined in a class implement the operations of the type. The compiler permits only these operations on objects of this type. You have to provide a specific object when you call a method. The statements are executed on that specific object.

  • Every class has a parent class, and has full access to the data and operations of its parent (access to your parent's stuff is called “inheritance”).

We also saw that Java object variables are really just pointers to objects, “references” in Java terminology. This is a big difference between Java and most other object-oriented programming languages. Some implications that flow from this design decision are summarized in the next section.

Reminder: Java object variables are really references to objects

  • All objects are accessed through references (these are, effectively, pointers).

  • Declaring what looks like an object variable actually gets you a variable that can hold a reference to an object.

  • When you declare that variable, it contains a null value that does not point to any object. To start using the object, you must first make the variable point to an object. You can make it point to an existing object, or you can create a new object for it to reference.

We have reinforced the point that object variables don't start out pointing to an object. It's time to describe in detail how you create a new object when you want one.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.29.119