Page 280 - Introduction to Programming with Java: A Problem Solving Approach
P. 280
246 Chapter 7 Object-Oriented Programming—Additional Details 7.1 Introduction
In Chapter 6, you learned to write simple object-oriented programming (OOP) programs using simple OOP building blocks. In this chapter, you learn to write more advanced OOP programs using more advanced OOP concepts. In particular, you learn the details of what happens behind the scenes when a program instantiates an object and stores its address in a reference variable. That will help you to appreciate and understand what happens when a program assigns one reference variable to another.
One of the OOP concepts you learn about in this chapter is testing objects for equality. It’s common to compareprimitivesforequality(forexample,if (team1Score == team2Score)),andlikewise,it’s common to compare references for equality. Comparing references for equality requires a bit more effort, and in this chapter, you learn what that effort entails. Another concept you learn about is what happens be- hind the scenes when a program passes a reference as an argument. That’s important to know because you’ll often need to pass references as arguments.
In addition to presenting more advanced OOP concepts, this chapter also presents more advanced applications of what you already know in regard to OOP. For example, you learn to call several methods in succession, all within one statement. That’s called method-call chaining, and it can lead to more compact and more elegant code. You also learn about method overloading. That’s when you have different versions of a method and each version operates on different kinds of data. That should sound familiar because you saw it with the Math class. Remember the two versions of the Math.abs method? One version returns the absolute value of a double, and one version returns the absolute value of an int.
In the previous chapter, you learned how to instantiate an object in one statement (for example, Mouse gus = new Mouse();)andassignavaluetotheobjectinaseparatestatement(forexample,
Apago PDF Enhancer
gus.setPercentGrowthRate(10);). In this chapter you learn how to combine those two tasks into one statement. To do that, you’ll use a special kind of method called a constructor. Like methods, construc- tors can be overloaded by using different types of data for the different constructor versions. But unlike methods, constructors are designed specifically for object creation and initialization.
In a final problem-solving section, you learn how to partition large programming problems into several smaller and simpler problems by using multiple driven classes. As this text progresses, the size and com- plexity of problems gradually increases, and you’ll see more and more examples of programs with multiple driven classes.
7.2 Object Creation—A Detailed Analysis
Let’s start the chapter with a behind-the-scenes detailed look at what happens when a program instantiates an object and stores its address in a reference variable. Having a clear understanding will help when it comes time to understand other OOP operations, and it will help with some debugging efforts.
Consider the following code fragment:
Car car1;
car1 = new Car();
car1.year = 1998;
reference variable declaration
objectinstantiation
assign 1998 to car1’s year instance variable