Definition of Java User-defined Data Types
Java User-defined data types are the customized data types created by the developers by exploiting the special features offered by the Java language. This customized data type combines data types of a group of data elements with homogenous or assorted data types. It utilizes the object-oriented functionalities of Java and allows creation of any kind of data types as per the need.
These data types have versatile characteristics and behavior and it provides greater flexibility to developers in improving their productivity and maintainability of the programs. Let’s go through features of these special data types in this article.
Syntax
Data types of all the variables should be declared well ahead of its compilation and it should be one from the standard type that Java offers. The variables cannot hold any other data type than what was declared. But the user-defined data type provides the flexibility to define the data type at a group level.
User-defined Data Types in Java
Two major User defined data types are:
1. Class
Java a true object-oriented language is full of Classes that encapsulate everything from data elements that acts as instance variables and functions to process the data. It also provides templates to create Objects that are instances of Class that contain a method for performing the defined functions and interact with other parts of the program.
A class typically contains
Sl | Syntax | Description |
1 | Accessibility | Public or a Default access |
2 | Class | Text definition |
3 | Class name | Contains alpha and numeric starting with a capital alpha traditionally |
4 | Extends | To declare the parent class (optional) |
5 | Interfaces | Lists the interfaces the class implements. A class can implement multiple Interfaces (optional) |
6 | Data elements | Variables with assorted or uniform standard data types. These elements act as instance variables. |
7 | Method | Data type of the result
Method name Invoking variables with data types Function codes. |
How Objects are created?
Objects are created or instantiated from the class and used as individual instance of the class. Instance variables are created for each of the objects to interact with other components of the program and there could be as many objects created and each object will have
- Clear identity with a unique name
- Different set of instance variables to reflect its state.
- All the functionalities that define its behavior
Each class is a user-defined data type with the combined effect of all the data types of its data elements and each object is a variable data.
Example #1: Class Definition
Code:
// Sample program to define Class, Object and Method
class ValueStore //Class definition with default access
{
int Rate; // Variables with standard data types
int Qty;
void AssignData(int A, int B) // method definition
{
Rate = A; // Assigning the values
Qty = B;
}
int ComputeValue() // Another method to compute value
{
int value; // variable definition
value = Rate * Qty;
return(value); // Returning the value
}
}
Class with name ValueStore with default access is defined. It has two instance variables Rate, QTY used in the method interactions. Two methods AssignData, ComputeValue with respective function codes are created inside the class.
Example #2
public class OrderValueCompute // Public class
{
public static void main(String[] args) // Execution starts here
{
int value1; // variable definition
int value2;
ValueStore VS1; // Object VS1 definition
VS1 = new ValueStore(); // Object VS1 instantiated
ValueStore VS2; // Object VS2 definition
VS2 = new ValueStore(); // Object VS2 instantiated
VS1.AssignData (200,10); // Assigndata method in Object VS1 invoked
//with values
value1 = VS1.ComputeValue(); // Computedata method in object VS1
// invoked
VS2.AssignData (500,20); // Assigndata method in Object VS2
// invoked with values
value2 = VS2.ComputeValue(); // Computedata method in object VS2
//invoked
System.out.println("Order value 1 " +value1); // Output 1 displayed
System.out.println("Order Value 2 " +value2); // Output 2 displayed
}
}
In the execution class, the objects in the other class are defined, instantiated. Methods are invoked. Results obtained and displayed. Class is a user-defined data type and each object is a variable in it.
Result:
2. Interface
Interface is similar to Class in architecture. It has data variables and methods. It broadly suggests what the classes calling it should do but it does not suggest how it should be carried out.
The method in an interface differs from a normal method in a class by
- It is not full-fledged
- It is only an abstract and it has the only a definition and the body is empty.
- It will not perform any functionalities as such
Interfaces cannot instantiate any objects like class. But it facilitates abstraction, Multiple inheritances, and loose coupling of classes.
Class extends to another super class by a level. Interface extends to another super interface by a level. Class implements interface and multiple inheritance is achieved indirectly which is otherwise not possible in a class.
Each interface created by a user has a unique data type with a combined effect of its data elements. Objects created in these classes uses interfaces also and each of these objects are the variables for interface data types.
Example
Below example shows how classes implement interfaces, how objects of these classes are created linking the interfaces, and how they are executed.
Code:
// Sample program to demonstrate Interfaces
interface Intface //Interface is defined
{ // It has its own data type
public void dayname(); //Abstract method within the interface
}
class day1 implements Intface { // Class interacts through
public void dayname() // interface implementation
{ // (each class is a variable for interface)
System.out.println("Monday");
}
}
class day2 implements Intface { // Another class
public void dayname()
{
System.out.println("Tuesday");
}
}
class day3 implements Intface { // Another class
public void dayname()
{
System.out.println("Wednesday");
}
}
public class Subject { // Execution starts here
public static void main(String[] args)
{
Intface t = new day1(); // Object of day1 class thru interface
t.dayname(); // method invoked
Intface tx = new day2(); // Object of day2 class thru interface
tx.dayname();
Intface tx2 = new day3(); // Object of day3 class thru interface
tx2.dayname(); }
} // objects t, tx1, tx2 are the variables of
// the interface data type
Result:
Conclusion
Class and interfaces are the major user-defined data types. Some forums have the opinion that String and Arrays are also part of user-defined types. Strictly speaking, they are part of standard data types of Java and hence they are not discussed.
Recommended Articles
This is a guide to Java User-Defined Exception. Here we discuss the Introduction, syntax, How Objects are created? examples with code implementation. You may also have a look at the following articles to learn more –