You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: In this tutorial, we will learn about classes and objects in Java. We will learn about what classes and objects are, how to define classes, how to create objects of a class, and how to access and use objects in Java.
8
-
---
8
+
---
9
+
10
+
# Classes and Objects in Java
11
+
12
+
## Introduction
13
+
14
+
Classes and objects are fundamental concepts in Java's object-oriented programming (OOP) paradigm. A class serves as a blueprint for creating objects, encapsulating data for the object, and methods to manipulate that data.
15
+
16
+
## Defining a Class
17
+
18
+
### Syntax
19
+
20
+
```java
21
+
classClassName {
22
+
// Fields (variables)
23
+
dataType fieldName;
24
+
25
+
// Constructor
26
+
publicClassName(parameters) {
27
+
// Initialization code
28
+
}
29
+
30
+
// Methods
31
+
returnType methodName(parameters) {
32
+
// Method code
33
+
}
34
+
}
35
+
```
36
+
37
+
### Example
38
+
39
+
```java
40
+
publicclassPerson {
41
+
// Fields
42
+
privateString name;
43
+
privateint age;
44
+
45
+
// Constructor
46
+
publicPerson(Stringname, intage) {
47
+
this.name = name;
48
+
this.age = age;
49
+
}
50
+
51
+
// Getter for name
52
+
publicStringgetName() {
53
+
return name;
54
+
}
55
+
56
+
// Setter for name
57
+
publicvoidsetName(Stringname) {
58
+
this.name = name;
59
+
}
60
+
61
+
// Getter for age
62
+
publicintgetAge() {
63
+
return age;
64
+
}
65
+
66
+
// Setter for age
67
+
publicvoidsetAge(intage) {
68
+
this.age = age;
69
+
}
70
+
71
+
// Method to display person details
72
+
publicvoiddisplay() {
73
+
System.out.println("Name: "+ name +", Age: "+ age);
74
+
}
75
+
}
76
+
```
77
+
78
+
## Creating Objects
79
+
80
+
### Syntax
81
+
82
+
```java
83
+
ClassName objectName =newClassName(parameters);
84
+
```
85
+
86
+
### Example
87
+
88
+
```java
89
+
publicclassMain {
90
+
publicstaticvoidmain(String[] args) {
91
+
// Creating an object of the Person class
92
+
Person person1 =newPerson("Alice", 30);
93
+
94
+
// Accessing object methods
95
+
person1.display();
96
+
97
+
// Modifying object properties
98
+
person1.setName("Alice Smith");
99
+
person1.setAge(31);
100
+
101
+
// Displaying modified details
102
+
person1.display();
103
+
}
104
+
}
105
+
```
106
+
107
+
## Constructors
108
+
109
+
A constructor is a special method that is called when an object is instantiated. It is used to initialize the object.
110
+
111
+
### Example
112
+
113
+
```java
114
+
publicclassPerson {
115
+
privateString name;
116
+
privateint age;
117
+
118
+
// No-argument constructor
119
+
publicPerson() {
120
+
this.name ="Unknown";
121
+
this.age =0;
122
+
}
123
+
124
+
// Parameterized constructor
125
+
publicPerson(Stringname, intage) {
126
+
this.name = name;
127
+
this.age = age;
128
+
}
129
+
130
+
// Other methods...
131
+
}
132
+
```
133
+
134
+
## Methods
135
+
136
+
Methods define the behavior of objects created from a class. They can be used to perform operations on the object's data.
137
+
138
+
### Example
139
+
140
+
```java
141
+
publicclassCalculator {
142
+
// Method to add two numbers
143
+
publicintadd(inta, intb) {
144
+
return a + b;
145
+
}
146
+
147
+
// Method to subtract two numbers
148
+
publicintsubtract(inta, intb) {
149
+
return a - b;
150
+
}
151
+
152
+
// Method to multiply two numbers
153
+
publicintmultiply(inta, intb) {
154
+
return a * b;
155
+
}
156
+
157
+
// Method to divide two numbers
158
+
publicdoubledivide(inta, intb) {
159
+
if (b ==0) {
160
+
thrownewIllegalArgumentException("Division by zero is not allowed.");
161
+
}
162
+
return (double) a / b;
163
+
}
164
+
}
165
+
```
166
+
167
+
## Access Modifiers
168
+
169
+
Access modifiers define the visibility of classes, methods, and variables. The most common access modifiers are:
170
+
171
+
-`public`: The member is accessible from any other class.
172
+
-`private`: The member is accessible only within its own class.
173
+
-`protected`: The member is accessible within its own package and by subclasses.
174
+
-`default` (no modifier): The member is accessible only within its own package.
175
+
176
+
### Example
177
+
178
+
```java
179
+
publicclassExample {
180
+
publicint publicField;
181
+
privateint privateField;
182
+
protectedint protectedField;
183
+
int defaultField; // Default access
184
+
185
+
publicvoidpublicMethod() {}
186
+
privatevoidprivateMethod() {}
187
+
protectedvoidprotectedMethod() {}
188
+
voiddefaultMethod() {} // Default access
189
+
}
190
+
```
191
+
192
+
## Conclusion
193
+
194
+
Understanding classes and objects is crucial for mastering Java's object-oriented programming. By defining classes, creating objects, and using methods and constructors, you can build robust and modular applications.
description: In this tutorial, you will learn how to create and use custom exceptions in Java. We will learn how to define custom exception classes, throw exceptions, and handle exceptions in Java programs.
8
-
---
8
+
---
9
+
10
+
# Custom Exceptions in Java
11
+
12
+
## Introduction
13
+
14
+
Custom exceptions in Java allow you to create your own exception classes to handle specific error conditions in a more meaningful way. By creating custom exceptions, you can provide more informative error messages and handle specific scenarios that are unique to your application.
15
+
16
+
## Creating Custom Exceptions
17
+
18
+
### Steps to Create a Custom Exception
19
+
20
+
1.**Extend the Exception class or one of its subclasses**: Most commonly, you extend `Exception` for checked exceptions or `RuntimeException` for unchecked exceptions.
21
+
2.**Provide constructors**: Include constructors that take a message, a cause, or both, as parameters.
* Constructs a new exception with the specified cause.
171
+
*
172
+
* @param cause the cause of the exception
173
+
*/
174
+
publicInvalidAgeException(Throwablecause) {
175
+
super(cause);
176
+
}
177
+
}
178
+
```
179
+
180
+
## Conclusion
181
+
182
+
Custom exceptions in Java provide a powerful mechanism for handling application-specific error conditions. By creating and using custom exceptions, you can make your error handling code more robust, readable, and maintainable. Ensure that you follow best practices when defining and using custom exceptions to maximize their effectiveness.
0 commit comments