Skip to content

Commit 61cf64e

Browse files
committed
feat: Add Encapsulation class to demonstrate private fields with getters and setters
WHAT the code does: Defines an Encapsulation class with private attributes: - color, brand, model, countryOrigin, year, speed. Implements behavior methods: - accelerate(int increment): increases speed. - brake(int decrement): decreases speed but prevents it from going below zero. Provides getter and setter methods for all attributes to control access. main() demonstrates usage by: - Setting attributes via setters. - Updating speed with accelerate and brake. - Printing details via getters. WHY this matters: Illustrates encapsulation, a core principle of object-oriented programming: - Fields are hidden (private) and accessed through controlled interfaces (getters/setters). - Prevents direct modification of internal state, enforcing better data integrity. Shows how encapsulation separates representation (fields) from interaction (methods). HOW it works: In main(): - A car object is created and initialized with setters (brand = Tesla, model = Model S, etc.). - accelerate(50) sets speed to 50. - brake(20) reduces speed to 30. - Getter methods retrieve field values for display. Tips and gotchas: Encapsulation is stronger when setters include validation (e.g., rejecting negative year or unrealistic speeds). Overusing setters/getters without logic reduces them to boilerplate; consider immutability or records for simpler cases. The toString() method could be overridden to simplify object state printing instead of multiple getter calls. For real-world systems, builders or constructors may be preferable for setting required fields. Use-cases: Educational demonstration of encapsulation in Java. Useful for modeling entities where state must be controlled and validated. Foundation for larger systems applying OOP principles like abstraction and inheritance. Short key: class-encapsulation private-fields getters-setters oop. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 8a1de8b commit 61cf64e

File tree

1 file changed

+59
-39
lines changed

1 file changed

+59
-39
lines changed
Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,95 @@
1-
//Bundling of data and methods into a single unit.
2-
//Class ke Andar Band Kardeya hai Data and methods ko.
3-
//Single Unit Mai Sab Band keya hai.
4-
//Bundle kar ke data hide kar raha hai Private use karke.
1+
/*Bundling of data and methods into a single unit.
2+
Class ke Andar Band Kardeya hai Data and methods ko.
3+
Single Unit Mai Sab Band keya hai.
4+
Bundle kar ke data hide kar raha hai Private use karke.*/
55

6-
public class Encapsulation
7-
{
8-
private String Color;
9-
private String brand;
10-
private String Model;
11-
private String CountryOrigin;
12-
//Sab Private to set kaise kare Getters and setters method se
13-
private int year;
14-
private int speed;
6+
public class Encapsulation {
7+
// Private attributes
8+
private String color;
9+
private String brand;
10+
private String model;
11+
private String countryOrigin;
12+
private int year;
13+
private int speed;
1514

16-
//Method hai. Speed ko increment kar raha hai.
17-
public void accelerate(int increment)
18-
{
19-
speed += increment;
15+
// Behavior methods
16+
public void accelerate(int increment) {
17+
speed += increment;
18+
}
19+
20+
public void brake(int decrement) {
21+
speed -= decrement;
22+
if (speed < 0) {
23+
speed = 0;
2024
}
25+
}
2126

27+
// Getters and setters
2228
public String getColor() {
23-
return Color;
29+
return color;
2430
}
25-
2631
public void setColor(String color) {
27-
Color = color;
32+
this.color = color;
2833
}
2934

3035
public String getBrand() {
3136
return brand;
3237
}
33-
3438
public void setBrand(String brand) {
3539
this.brand = brand;
3640
}
3741

3842
public String getModel() {
39-
return Model;
43+
return model;
44+
}
45+
public void setModel(String model) {
46+
this.model = model;
4047
}
4148

4249
public String getCountryOrigin() {
43-
return CountryOrigin;
50+
return countryOrigin;
4451
}
45-
4652
public void setCountryOrigin(String countryOrigin) {
47-
CountryOrigin = countryOrigin;
53+
this.countryOrigin = countryOrigin;
4854
}
4955

5056
public int getYear() {
5157
return year;
5258
}
59+
public void setYear(int year) {
60+
this.year = year;
61+
}
5362

5463
public int getSpeed() {
5564
return speed;
5665
}
57-
5866
public void setSpeed(int speed) {
5967
this.speed = speed;
6068
}
6169

62-
//Behavior hai toh method mai likh te hai
63-
public void brak(int decrement)
64-
{
65-
speed -= decrement;
66-
if(speed<0)
67-
{
68-
speed=0;
69-
}
70-
}
71-
public static void main(String[] args) {
72-
ClassBluePrint car = new ClassBluePrint();
70+
// Main method to test encapsulation
71+
public static void main(String[] args) {
72+
Encapsulation car = new Encapsulation();
7373

74-
}
75-
}
74+
// Set values using setters.
75+
car.setBrand("Tesla");
76+
car.setModel("Model S");
77+
car.setColor("Red");
78+
car.setCountryOrigin("USA");
79+
car.setYear(2023);
80+
car.setSpeed(0);
81+
82+
// Use behavior methods
83+
car.accelerate(50);
84+
car.brake(20);
85+
86+
// Access values using getters.
87+
System.out.println("Car Details:");
88+
System.out.println("Brand: " + car.getBrand());
89+
System.out.println("Model: " + car.getModel());
90+
System.out.println("Color: " + car.getColor());
91+
System.out.println("Country of Origin: " + car.getCountryOrigin());
92+
System.out.println("Year: " + car.getYear());
93+
System.out.println("Speed: " + car.getSpeed() + " km/h");
94+
}
95+
}

0 commit comments

Comments
 (0)