Understanding the differences between classes and objects
As classes and objects are integral to OOP, it is vital to understand their differences. We will discuss the relationship between a class and its objects in this section. As creating objects requires the use of the new
keyword, this will also be covered. Understanding constructors and what they do will also be examined. All of these topics are linked: objects are the in-memory representation of the class (template); to create an object, a constructor is used and to call the constructor, we use the new
keyword. Let’s examine these in turn.
Classes
A class is so integral in Java that you cannot write any program without defining one! A class is a blueprint or template for your object. It is similar to a plan of a house – using a house plan, you can discuss the house all you want; however, you cannot go into the kitchen and make a cup of tea/coffee. The house plan is abstract in that regard and so is the class...