Skip to content

Commit 732361e

Browse files
committed
object-oriented-prging
1 parent 0aa472a commit 732361e

7 files changed

+1272
-7
lines changed

docs/java/object-oriented-programming/classes-and-objects.md

Lines changed: 187 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,190 @@ sidebar_label: Classes and Objects
55
sidebar_position: 1
66
tags: [java, classes, objects, programming, java classes, java objects]
77
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+
class ClassName {
22+
// Fields (variables)
23+
dataType fieldName;
24+
25+
// Constructor
26+
public ClassName(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+
public class Person {
41+
// Fields
42+
private String name;
43+
private int age;
44+
45+
// Constructor
46+
public Person(String name, int age) {
47+
this.name = name;
48+
this.age = age;
49+
}
50+
51+
// Getter for name
52+
public String getName() {
53+
return name;
54+
}
55+
56+
// Setter for name
57+
public void setName(String name) {
58+
this.name = name;
59+
}
60+
61+
// Getter for age
62+
public int getAge() {
63+
return age;
64+
}
65+
66+
// Setter for age
67+
public void setAge(int age) {
68+
this.age = age;
69+
}
70+
71+
// Method to display person details
72+
public void display() {
73+
System.out.println("Name: " + name + ", Age: " + age);
74+
}
75+
}
76+
```
77+
78+
## Creating Objects
79+
80+
### Syntax
81+
82+
```java
83+
ClassName objectName = new ClassName(parameters);
84+
```
85+
86+
### Example
87+
88+
```java
89+
public class Main {
90+
public static void main(String[] args) {
91+
// Creating an object of the Person class
92+
Person person1 = new Person("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+
public class Person {
115+
private String name;
116+
private int age;
117+
118+
// No-argument constructor
119+
public Person() {
120+
this.name = "Unknown";
121+
this.age = 0;
122+
}
123+
124+
// Parameterized constructor
125+
public Person(String name, int age) {
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+
public class Calculator {
142+
// Method to add two numbers
143+
public int add(int a, int b) {
144+
return a + b;
145+
}
146+
147+
// Method to subtract two numbers
148+
public int subtract(int a, int b) {
149+
return a - b;
150+
}
151+
152+
// Method to multiply two numbers
153+
public int multiply(int a, int b) {
154+
return a * b;
155+
}
156+
157+
// Method to divide two numbers
158+
public double divide(int a, int b) {
159+
if (b == 0) {
160+
throw new IllegalArgumentException("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+
public class Example {
180+
public int publicField;
181+
private int privateField;
182+
protected int protectedField;
183+
int defaultField; // Default access
184+
185+
public void publicMethod() {}
186+
private void privateMethod() {}
187+
protected void protectedMethod() {}
188+
void defaultMethod() {} // 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.

docs/java/object-oriented-programming/custom-exceptions-in-java.md

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,178 @@ sidebar_label: Custom Exceptions
55
sidebar_position: 5
66
tags: [java, exceptions, programming, custom-exceptions, java exceptions]
77
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.
22+
23+
### Example: Creating a Custom Checked Exception
24+
25+
#### Definition
26+
27+
```java
28+
public class InvalidAgeException extends Exception {
29+
// Default constructor
30+
public InvalidAgeException() {
31+
super();
32+
}
33+
34+
// Constructor that accepts a message
35+
public InvalidAgeException(String message) {
36+
super(message);
37+
}
38+
39+
// Constructor that accepts a message and a cause
40+
public InvalidAgeException(String message, Throwable cause) {
41+
super(message, cause);
42+
}
43+
44+
// Constructor that accepts a cause
45+
public InvalidAgeException(Throwable cause) {
46+
super(cause);
47+
}
48+
}
49+
```
50+
51+
#### Usage
52+
53+
```java
54+
public class Person {
55+
private int age;
56+
57+
public void setAge(int age) throws InvalidAgeException {
58+
if (age < 0 || age > 150) {
59+
throw new InvalidAgeException("Age must be between 0 and 150.");
60+
}
61+
this.age = age;
62+
}
63+
}
64+
65+
public class Main {
66+
public static void main(String[] args) {
67+
Person person = new Person();
68+
try {
69+
person.setAge(200);
70+
} catch (InvalidAgeException e) {
71+
System.out.println("Caught exception: " + e.getMessage());
72+
}
73+
}
74+
}
75+
```
76+
77+
### Example: Creating a Custom Unchecked Exception
78+
79+
#### Definition
80+
81+
```java
82+
public class InvalidParameterException extends RuntimeException {
83+
// Default constructor
84+
public InvalidParameterException() {
85+
super();
86+
}
87+
88+
// Constructor that accepts a message
89+
public InvalidParameterException(String message) {
90+
super(message);
91+
}
92+
93+
// Constructor that accepts a message and a cause
94+
public InvalidParameterException(String message, Throwable cause) {
95+
super(message, cause);
96+
}
97+
98+
// Constructor that accepts a cause
99+
public InvalidParameterException(Throwable cause) {
100+
super(cause);
101+
}
102+
}
103+
```
104+
105+
#### Usage
106+
107+
```java
108+
public class Calculator {
109+
public int divide(int numerator, int denominator) {
110+
if (denominator == 0) {
111+
throw new InvalidParameterException("Denominator cannot be zero.");
112+
}
113+
return numerator / denominator;
114+
}
115+
}
116+
117+
public class Main {
118+
public static void main(String[] args) {
119+
Calculator calculator = new Calculator();
120+
try {
121+
int result = calculator.divide(10, 0);
122+
} catch (InvalidParameterException e) {
123+
System.out.println("Caught exception: " + e.getMessage());
124+
}
125+
}
126+
}
127+
```
128+
129+
## Best Practices for Custom Exceptions
130+
131+
- **Inherit from the appropriate base exception class**: Use `Exception` for checked exceptions and `RuntimeException` for unchecked exceptions.
132+
- **Provide multiple constructors**: Include constructors that take messages and causes to provide more context.
133+
- **Use meaningful exception names**: The name of the exception class should clearly indicate the error condition it represents.
134+
- **Document the exception**: Provide Javadoc comments to explain when and why the custom exception should be used.
135+
136+
### Example with Documentation
137+
138+
```java
139+
/**
140+
* Thrown to indicate that the age provided is invalid.
141+
*/
142+
public class InvalidAgeException extends Exception {
143+
/**
144+
* Constructs a new exception with {@code null} as its detail message.
145+
*/
146+
public InvalidAgeException() {
147+
super();
148+
}
149+
150+
/**
151+
* Constructs a new exception with the specified detail message.
152+
*
153+
* @param message the detail message
154+
*/
155+
public InvalidAgeException(String message) {
156+
super(message);
157+
}
158+
159+
/**
160+
* Constructs a new exception with the specified detail message and cause.
161+
*
162+
* @param message the detail message
163+
* @param cause the cause of the exception
164+
*/
165+
public InvalidAgeException(String message, Throwable cause) {
166+
super(message, cause);
167+
}
168+
169+
/**
170+
* Constructs a new exception with the specified cause.
171+
*
172+
* @param cause the cause of the exception
173+
*/
174+
public InvalidAgeException(Throwable cause) {
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

Comments
 (0)