Skip to content

Commit b7566f7

Browse files
committed
Migrate development environment from Windows to macOS and continue project updates
- Shifted primary development environment from Windows to macOS to ensure better performance, stability, and streamlined workflow for Java development. - Removed OS-specific metadata and ensured platform-agnostic project compatibility. - Normalized line endings to LF for cross-platform consistency. - Verified and adjusted file paths, permissions, and encoding settings to suit macOS environment. - Cleaned up unnecessary Windows-generated files and updated Git configuration accordingly. - Future commits and development will now be managed entirely from macOS. This migration ensures a cleaner, more consistent setup moving forward and prepares the project for scalable development across Unix-based systems. Future development will continue on macOS for a smoother, Unix-based experience. Signed-off-by: Someshdiwan <Someshdiwan369@gmail.com>
1 parent 3bfa364 commit b7566f7

7 files changed

Lines changed: 275 additions & 0 deletions

File tree

810 Bytes
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Which One Should You Use?
2+
3+
| Method | Pros | When to Use |
4+
|----------------------------------------------|--------------------------------------------|----------------------------------------------------|
5+
| ✅ toString() (Best) | Cleaner, reusable, easy to debug | If you need structured output often |
6+
| printf() | Better formatting | When you need aligned columns |
7+
| `printDetails()` | Simple and separate | If you prefer explicit method calls |
1.43 KB
Binary file not shown.
746 Bytes
Binary file not shown.
629 Bytes
Binary file not shown.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
constructors. In Java, constructors are special methods used to initialize objects of a class.
2+
3+
They have the same name as the class and do not have a return type.
4+
5+
If you were thinking about adding parameters to the class constructor, here's how you would do it:
6+
7+
What is a Constructor?
8+
9+
A constructor is used to initialize the state of an object when it's created. It can take parameters,
10+
and you can use these parameters to initialize the object's fields.
11+
12+
Syntax for Constructor:
13+
14+
If you want to create a constructor that takes parameters (`int a, int b`), you would do it like this:
15+
16+
class Arithmetic {
17+
// Constructor that takes two integers
18+
public Arithmetic(int a, int b)
19+
{
20+
// You could use 'a' and 'b' to initialize the object
21+
System.out.println("Constructor called with values: " + a + " and " + b);
22+
}
23+
24+
// Method to add two integers and return the sum
25+
public int add(int a, int b)
26+
{
27+
return a + b;
28+
}
29+
}
30+
31+
When do you use a constructor?
32+
33+
- A constructor is automatically called when an object is created.
34+
35+
- If you want to pass parameters to initialize an object, you can define a **parameterized constructor**.
36+
37+
Example with Constructor:
38+
39+
Here’s how it would look in your case with `Arithmetic` class having a constructor that takes parameters:
40+
41+
class Arithmetic {
42+
// Constructor with parameters a and b
43+
public Arithmetic(int a, int b) {
44+
System.out.println("Arithmetic object created with values: " + a + " and " + b);
45+
}
46+
47+
// Method to add two integers and return their sum
48+
49+
public int add(int a, int b) {
50+
return a + b;
51+
}
52+
}
53+
54+
55+
class Adder extends Arithmetic {
56+
// Constructor of the subclass Adder
57+
public Adder(int a, int b) {
58+
// Call the superclass constructor
59+
super(a, b);
60+
}
61+
}
62+
63+
64+
public class Solution
65+
{
66+
public static void main(String[] args)
67+
{
68+
// Create an Adder object, passing parameters for the constructor
69+
Adder a = new Adder(10, 20);
70+
71+
// Print the name of the superclass
72+
System.out.println("My superclass is: " + a.getClass().getSuperclass().getName());
73+
74+
// Call the add method and print the result
75+
System.out.print(a.add(10, 32) + " " + a.add(10, 3) + " " + a.add(10, 10) + "\n");
76+
}
77+
}
78+
79+
80+
Breakdown:
81+
82+
1. Arithmetic Class:
83+
84+
- A constructor is defined that takes two parameters (`int a` and `int b`). It can be used to initialize the object or print something.
85+
86+
2. Adder Class:
87+
88+
- The constructor of `Adder` calls the `Arithmetic` class constructor using `super(a, b)` to initialize the base class.
89+
90+
3. Solution Class:
91+
92+
- An `Adder` object is created with parameters `10` and `20`. This triggers the constructor in `Arithmetic` and prints the initialization message.
93+
94+
Output:
95+
96+
Arithmetic object created with values: 10 and 20
97+
98+
My superclass is: Arithmetic
99+
100+
42 13 20
101+
102+
Key Points about Constructors:
103+
104+
- A constructor does not have a return type.
105+
- A constructor with parameters is called a parameterized constructor.
106+
- In inheritance, you can call the superclass constructor using `super()` in the subclass constructor.
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
The difference between a constructor and a method in Java is fundamental in object-oriented programming. Let's break down the key differences:
2+
3+
1. Purpose
4+
- Constructor:
5+
- A constructor is used to initialize a new object. It is called when an object is created.
6+
- Its main role is to set up the initial state of the object, such as initializing fields or setting up resources.
7+
8+
- Method:
9+
- A method is used to define behavior or operations that an object can perform. It can be called after the object is created to perform actions on the object.
10+
11+
2. Name
12+
- Constructor:
13+
- The constructor's name must be the same as the class name.
14+
- For example, in the `Arithmetic` class, the constructor must be named `Arithmetic`.
15+
16+
- Method:
17+
- A method can have any name (except it cannot be the same as the class name, unless it's a constructor).
18+
- For example, a method can be named `add`, `multiply`, or `display`.
19+
20+
3. Return Type
21+
22+
- Constructor:
23+
24+
- A constructor does not have a return type. It doesn't return anything, not even `void`.
25+
26+
- Example:
27+
28+
public Arithmetic(int a, int b) { ... }
29+
30+
- Method:
31+
32+
- A method must have a return type. It can return a value (e.g., `int`, `String`, `void`), or in some cases, it can return nothing (in which case it would have a return type of
33+
void).
34+
35+
- Example:
36+
37+
public int add(int a, int b) { ... }
38+
39+
4. Invocation
40+
41+
- Constructor:
42+
43+
- A constructor is automatically called when you **create an object** using the `new` keyword.
44+
45+
- Example:
46+
47+
Arithmetic obj = new Arithmetic(10, 20); // Constructor is called here
48+
49+
- Method:
50+
51+
- A method is called explicitly after the object has been created, using the object's reference.
52+
53+
- Example:
54+
55+
int sum = obj.add(10, 20); // Calling the add method
56+
57+
5. Parameters:
58+
59+
- Constructor:
60+
61+
- Constructors can take parameters, allowing you to pass values to initialize the object at the time of creation.
62+
63+
- Example:
64+
65+
public Arithmetic(int a, int b) {
66+
// Initializes the object
67+
}
68+
69+
- Method:
70+
71+
- Methods can also take parameters, but they are called after the object is created.
72+
73+
- Example:
74+
75+
public int add(int a, int b) {
76+
return a + b;
77+
}
78+
79+
6. Inheritance
80+
81+
- Constructor:
82+
83+
- Constructors are not inherited. Each class must define its own constructors, although the subclass can call the superclass constructor using `super()`.
84+
85+
- Method:
86+
87+
- Methods are inherited. A subclass can inherit methods from its superclass and can override them if needed.
88+
89+
7. Constructor Overloading vs Method Overloading
90+
91+
- Constructor:
92+
93+
- Constructor overloading is allowed, meaning you can have multiple constructors with different parameters in the same class.
94+
95+
- Example:
96+
97+
public Arithmetic() {
98+
// Default constructor
99+
}
100+
101+
public Arithmetic(int a, int b) {
102+
// Constructor with parameters
103+
}
104+
105+
- Method:
106+
107+
- Method overloading is also allowed, which means you can have multiple methods with the same name but different parameter lists.
108+
- Example:
109+
110+
public int add(int a, int b) { ... }
111+
112+
public int add(int a, int b, int c) { ... }
113+
114+
Summary of Differences:
115+
116+
| Feature | Constructor | Method |
117+
|-------------------------|--------------------------------------------------|--------------------------------------------------|
118+
| Purpose | To initialize the object | To define behavior or operations on the object |
119+
| Name | Same as the class name | Can have any name |
120+
| Return Type | No return type (not even `void`) | Must have a return type (`int`, `void`, etc.) |
121+
| Invocation | Called automatically when an object is created | Called explicitly on an object after creation |
122+
| Parameters | Can take parameters to initialize an object | Can take parameters to perform an action |
123+
| Inheritance | Not inherited; must be defined in each class | Inherited by subclasses |
124+
| Overloading | Can be overloaded with different parameter lists | Can be overloaded with different parameter lists |
125+
126+
127+
Example to Clarify:
128+
129+
class Car {
130+
private String model;
131+
private int year;
132+
133+
// Constructor: Initializes the object with model and year
134+
public Car(String model, int year) {
135+
this.model = model;
136+
this.year = year;
137+
}
138+
139+
// Method: Displays the details of the car
140+
public void displayDetails() {
141+
System.out.println("Model: " + model + ", Year: " + year);
142+
}
143+
}
144+
145+
public class Main {
146+
public static void main(String[] args) {
147+
// Creating a new object of type Car
148+
Car myCar = new Car("Tesla Model 3", 2021); // Constructor is called here
149+
150+
// Calling the method to display car details
151+
myCar.displayDetails(); // Method is called here
152+
}
153+
}
154+
155+
156+
Output:
157+
158+
Model: Tesla Model 3, Year: 2021
159+
160+
- Constructor: `new Car("Tesla Model 3", 2021)` initializes the object with the `model` and `year`.
161+
162+
- Method: `displayDetails()` is called after the object is created to show the car's details.

0 commit comments

Comments
 (0)