Classes. The use of classes in Java programs. Class and class object definition
Contents
- 1. Why classes are used in programs?
- 2. Class definition. The general form of the class declaration. The ‘class’ keyword
- 3. What is the object of a class? The general form of declaring a class object. Operator ‘new’
- 4. What is the difference between the concepts “class” and “class object”?
- 5. The simplest examples of declaring and using classes that contain data and methods for processing them
- 6. What type of access (private, public) does the member of the class have by default?
- 7. What are “wrapper” classes? What is the purpose of “wrapper” classes? Example
- Related topics
Search other websites:
1. Why classes are used in programs?
In the Java programming language, the development of all programs is based on an object-oriented approach. We can say that Java is this strictly object-oriented programming language. Object-oriented programming is based on the concepts “class” and “object”. A programmer who develops a program on the basis of an object-oriented approach should be able to allocate logically completed units in the problem area (task). Each logically completed unit must be implemented as a certain class. In general, when programming tasks using the object-oriented paradigm, the programmer should be able to:
- structurе the task into completed logical units that can be implemented by separate classes;
- correctly define and build relationships between different classes in the program;
- determine the reusable program code, which can be repeated in different parts of the program, and also correctly select it;
- to build a hierarchy between classes on the principle of “from simple to complex”;
- understand the features of the implementation of a variety of opportunities (encapsulation, inheritance, polymorphism), which gives the use of classes for the purpose of effective program development.
⇑
2. Class definition. The general form of the class declaration. The ‘class’ keyword
From the point of view of programming, a class is a set of data and methods of a class. The methods operate on these class data (perform work on class data). With the help of many methods of the class, a program-logic code that operates on the data of the class is formed.
When using classes, there are two components in the programs:
- the declaration of a class as a separate composite data type;
- the implementation of an object (variable) of this class.
A class declaration is the declaration of a new composite data type. This data type contains methods for processing this data.
The class declaration begins with the keyword ‘class’. The class declaration begins with the keyword ‘class’:
class ClassName
{
type variable1;
type variable2;
...
type variableN;
type method1(parameters1)
{
// ...
}
type method2(parameters2)
{
// ...
}
...
type methodN(parametersN)
{
// ...
}
};
where
- type – the some type of data. It can be a primitive (basic) data type or a composite data type, for example, some class;
- variable1, variable2, …, variableN – variables of class instance;
- method1, method2, …, methodN – methods of class;
- parameters1, parameters2, parameters – parameters that respectively receive class methods method1, method2, methodN.
⇑
3. What is the object of a class? The general form of declaring a class object. Operator ‘new’
A class object is a declaration of a variable. A variable type is a type with a class name. When you declare a class object, an instance of the class is created. The notion of “class object” and “instance of a class” can be considered synonymous.
General view of declaring a class object in Java language:
ClassName ObjName = new ClassName();
where
- ClassName – the name of a class;
- ObjName – the name of an object of a class.
Alternative variant of declaring a class object is possible:
ClassName ObjName;
ObjName = new ClassName();
After the above declarations, the ObjName variable contains the memory address of a particular object of type ClassName.
In Java, the creation of a class object occurs in 2 stages:
- first, a variable (object) of the type ‘class’ is declared. This variable does not define the object yet. It is a variable that can refer to an object;
- A specific physical copy of the object is created, which is assigned to this variable of the type ‘class’ (memory is allocated). This is done using the ‘new’ operator.
⇑
4. What is the difference between the concepts “class” and “class object”?
A class is just information about a new composite (complex) type of data. In fact, the class describes the format of the data. The definition of a class is declarative. The class name is unique throughout the program. Unlike the class declaration, for the object of class the memory is allocated.
A class object is a specific instance of the class in which the class data has some values. For a class object, memory is allocated. There can be several objects of the same class.
⇑
5. The simplest examples of declaring and using classes that contain data and methods for processing them
Example 1. The class Circle, which realizes the geometric shape of the circle. In the class are declared:
- three private variables (data members) of the class with the names x, y, r. These variables determine the coordinate of the center of the circle and its radius;
- four methods SetXYR(), GetX(), GetY(), GetR(). These methods are public. By default, methods and variables in Java classes are public if they are not preceded by the ‘private’ keyword.
public class Circle { private int x, y; // coordinates of the center of a circle private int r; // radius of a circle // method of the class that sets the new values of x, y, r void SetXYR(int nx, int ny, int nr) { x = nx; y = ny; r = nr; } // class methods that return a value int GetX() { return x; } int GetY() { return y; } int GetR() { return r; } }
Using a class in another method
// declare a variable of type Circle Circle c1 = new Circle(); // c1 - object of class 'Circle', memory allocation c1.SetXYR(3, 5, 2); // call the method that sets the new x, y, r // test int d; d = c1.GetX(); // d = 3, call the GetX() method of the class d = c1.GetR(); // d = 2
The above code declares an object (instance) of the class named c1. The memory for object c1 is allocated using the ‘new’ operator. The operation of allocating memory by the operator new is mandatory.
Next, the public methods of the Circle class are called.
Example 2. The ‘MyName’ class that implements the surname, name and patronymic.
The class declares:
- 3 instance variables of class with names name, surname, patr;
- 7 methods of class with names GetName(), GetSurname(), GetPatr(), SetName(), SetSurname(), SetPatr(), SetMyName().
// the declaration of MyName class class MyName { // instance variables of the MyName class String name; String surname; String patr; // class methods String GetName() { return name; } String GetSurname() { return surname; } String GetPatr() { return patr; } void SetName(String nName) { name = nName; } void SetSurname(String nSurname) { surname = nSurname; } void SetPatr(String nPatr) { patr = nPatr; } void SetMyName(String nName, String nSurname, String nPatr) { name = nName; surname = nSurname; patr = nPatr; } }
Using the MyName class in another method or program code
... // using the MyName class in some method // creating an object (instance) of the class named nm1 MyName nm1 = new MyName(); // creating an object (instance) of the class named nm2 MyName nm2; nm2 = new MyName(); String s; // calling the class methods from object nm1 nm1.SetName("Johnson"); nm1.SetSurname("John"); nm1.SetPatr(""); s = nm1.GetSurname(); // s = "John" // calling the class methods from object nm2 nm2.SetMyName("David", "Albright", ""); s = nm2.GetName(); // s = "David" nm2.name = "Michael"; s = nm2.GetName(); // s = "Michael" ...
⇑
6. What type of access (private, public) does the member of the class have by default?
By default, class members have the ‘public’ access type. They are publicly available.
⇑
7. What are “wrapper” classes? What is the purpose of “wrapper” classes? Example
Wrap classes are intended to represent primitive types in the form of objects. That is, they convert a primitive type to a reference type. For each primitive type, there is its own class (type) of wrapper.
Classes-wrappers are also called packing classes.
The list of correspondences of the primitive type and the packaging type (class):
boolean => Boolean char => Character byte => Byte short => Short int => Integer long => Long float => Float double => Double void => Void
For example. Below are examples of converting different primitive types into package types and vice versa
// type conversion int <=> Integer int d = 25; Integer D = new Integer(d); // D = 25 D = new Integer(38); d = D.intValue(); // d = 38 // type conversion float <=> Float float f = 3.85f; Float F = new Float(f); // F = 3.85 F = new Float(7.77f); f = F.floatValue(); // f = 7.77 // type conversion char <=> Character char c = 'A'; Character C = new Character(c); // C = 'A' C = new Character('Z'); c = C.charValue(); // c = 'Z'
⇑
Related topics
- Constructors. Parameterized constructors. The keyword ‘this’. Garbage collection. The finalize() method. Examples
- Overloading of methods in classes. Overloading of constructors