Skip to content

Commit e969dce

Browse files
committed
Section 10 Methods
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 6963848 commit e969dce

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Difference Between Constructor and Method in Java
2+
3+
In Java, constructors and methods look similar because both can have parameters and a body of code.
4+
However, they serve different purposes and have distinct rules.
5+
6+
Understanding their differences is crucial for writing clear and maintainable Java programs.
7+
8+
---
9+
10+
## Key Differences
11+
12+
1. Purpose
13+
- Constructor: Used to initialize objects.
14+
- Method: Used to define behavior or functionality of an object.
15+
16+
2. Name
17+
- Constructor: Must have the same name as the class.
18+
- Method: Can have any valid identifier except the class name.
19+
20+
3. Return Type
21+
- Constructor: Has no return type, not even void.
22+
- Method: Must declare a return type (void, int, String, etc.).
23+
24+
4. Invocation
25+
- Constructor: Called automatically when new is used to create an object.
26+
- Method: Must be invoked explicitly using the object reference or class name (for static).
27+
28+
5. Inheritance
29+
- Constructor: Not inherited by subclasses, but subclass constructors may call superclass constructors via super().
30+
- Method: Inherited by subclasses (unless declared private or final).
31+
32+
6. Overloading and Overriding
33+
- Constructor: Can be overloaded (multiple constructors with different parameter lists). Cannot be overridden.
34+
- Method: Can be both overloaded and overridden.
35+
36+
7. Modifiers
37+
- Constructor: Can use access modifiers (public, protected, private). Cannot be abstract, static, final, or
38+
synchronized.
39+
- Method: Can use a wider range of modifiers (public, protected, private, abstract, static, final, synchronized,
40+
native).
41+
42+
8. Execution Order
43+
- Constructor: Executes automatically during object creation, before any method calls.
44+
- Method: Executes only when explicitly invoked.
45+
46+
---
47+
48+
## Example
49+
50+
class Student {
51+
String name;
52+
int age;
53+
54+
// Constructor
55+
public Student(String name, int age) {
56+
this.name = name;
57+
this.age = age;
58+
}
59+
60+
// Method
61+
public void displayDetails() {
62+
System.out.println("Name: " + name + ", Age: " + age);
63+
}
64+
}
65+
66+
class Main {
67+
public static void main(String[] args) {
68+
// Constructor is called automatically when creating an object
69+
Student s1 = new Student("Alice", 21);
70+
71+
// Method is called explicitly using the object reference
72+
s1.displayDetails();
73+
}
74+
}
75+
76+
| Feature | Constructor | Method |
77+
|--------------------|------------------------------------------------------------------------------|----------------------------------------------------------------------------------------|
78+
| Purpose | Initializes object state | Defines object behavior / functionality |
79+
| Name | Must match the class name exactly | Any valid identifier (cannot be the same as the class name if intended as a method) |
80+
| Return Type | None (not even void) | Must declare a return type (void, primitive, or object type) |
81+
| When Called | Automatically, at the time of object creation using new | Explicitly, after object creation or via class reference (if static) |
82+
| Invocation | Implicit via new ClassName() | Explicit: objectName.methodName() or ClassName.methodName() |
83+
| Inheritance | Not inherited; subclass must explicitly call with super() if needed | Inherited by subclasses (unless private or final) |
84+
| Overloading | Supported (multiple constructors with different parameter lists) | Supported |
85+
| Overriding | Not possible | Supported |
86+
| Modifiers | Allowed: public, protected, private<br>Not allowed: abstract, static, final, synchronized | Allowed: public, protected, private, abstract, static, final, synchronized, native |
87+
| Execution Order | Runs before any method, when object is instantiated | Runs only when explicitly invoked |
88+
| Special Use | Ensures mandatory fields are set and object is ready to use | Encapsulates business logic or reusable functionality |

0 commit comments

Comments
 (0)