Object Programming  «Prev  Next»
Lesson 5Defining objects with classes
ObjectiveClasses and their relationship to objects.

Defining Objects with Classes

You learned previously that a class is a template, or specification, that defines a type of object. It is important to understand the theoretical relevance of classes, but how does that translate into code? Here is a simple class definition:
class Lion {
  int  energy;
  int aggression;
  boolean hungry;
  Color color;
}

This class is named Lion and might be suited for a zoo simulation applet. The keyword class precedes the class name in the class definition; this is how all Java classes are defined. The state of the Lion class is defined by four data members, which represent the energy, aggression, hunger, and color of the lion. The fact that these variables appear within the curly braces ({}) indicates that they are member variables of the Lion class. Incidentally, you will learn more about data types a little later in the course.Now that you have seen a specific class definition.
The following class diagram contains the general syntax used to define all classes:
class ClassName{
  ClassBody
}
  1. An identifier that specifies the name of the class
  2. The code that defines the functionality of the class, and includes member variables and methods for the class
An identifier that specifies the name of the class .

Purpose of Class in Java

A class is the fundamental building block of object-oriented programs. It generally represents a real-world object. A class definition in Java consists of member variable declarations and method declarations and begins with the class keyword. The body of the class is enclosed with brackets and contains all instance variables and methods:
class classname {
// define class level variables
// define methods
}

A pair of open and close curly braces constitutes a block statement. This is used in many other parts of Java.
  • Classes and objects: A class is a pattern or template for creating multiple objects with similar features. It defines the variables and methods of the class. It declares the capabilities of the class. However, before these capabilities can be used, an object must be created. An object is an instantiation of a class. That is, an object is composed of the memory allocated for the member variables of the class. Each object has its own set of member variables.
  • What occurs when a new object is created?
    1. The new keyword is used to create an instance of a class
    2. Memory is physically allocated for the new instance of the class
    3. Any static initializers are executed (as detailed in the Java initialization sequence)
    4. A constructor is called to do initialization
    5. A reference to the object is returned

    The state of an object is typically hidden from the users of the object and is reflected in the value of its instance variables. The behavior of an object is determined by the methods it possesses. This is an example of data encapsulation.
    An object is the instantiation of a class and ach instance of a class has its own unique set of instance variables.


Defining Objects with Classes

Syntax for defining classes
Syntax for defining classes
  • ClassName: An identifier that specifies the name of the class
  • ClassBody: The Classbody contains the code that defines the functionality of the class and includes
    1. member variables and
    2. methods for the class.

In the "real" world, objects are the entities of which the world is comprised.
Everything that happens in the world is related to the interactions between the objects in the world. Just as atoms combine to form molecules and larger objects, the interacting entities in the world can be thought of as interactions between and among both singular ("atomic") as well as compound ("composed") objects. The real world consists of many, many objects interacting in many ways. While each object may not be overly complex, their myriad of interactions creates the overall complexity of the natural world. It is this complexity that we wish to capture in our software systems.In an object-oriented software system,objects are entities used to represent or model a particular piece of the system.

Principles of Software Testing: White vs. Black Box

Static testing procedures test the program not by executing it, but by inspecting and reviewing the code and performing detailed walk-throughs and is aimed at verification. On the other hand, dynamic testing procedures test the program by executing it with carefully selected test cases. It is thus more related to validation. The test cases can be chosen according to a white box or black box strategy. In white box testing[1], they are selected by thorough inspection of the source code of the program; for example, by making sure all the branches in an if-then-else selection are covered, boundary conditions for loops are verified, and so on. One popular approach is to intentionally inject faults in the source code, which then need to be tracked down in a follow-up step. Black box testing considers the program as a black box and does not inspect its internal source code. One example is a procedure that tries to test all possible input parameter combinations. It is especially important to also test what happens when impossible values are entered (such as a negative value for weight and height, value of 0 for height, missing value for gender, and so on). Obviously, this becomes computationally infeasible in case many inputs are present and intelligent sampling procedures could be adopted to test as many useful input combinations as possible. Software development typically has two phases of testing. Alpha testing is done internally by the application developers before the software is brought to the market. In beta testing, the software is given to a selected target audience and errors are reported back to the development team.

[1]White box testing: White box testing is a software testing method where the internal structure, design, and code of a software application are known to the tester. It involves examining the code paths and logic flows to ensure that the software functions correctly and meets the specified requirements.

SEMrush Software