-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
80 lines (65 loc) · 1.84 KB
/
Copy pathMain.java
File metadata and controls
80 lines (65 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// main.java
// Java Constructors Notları
class MyClass {
// No-arg constructor (boş kurucu)
public MyClass() {
// Nesne oluşturulunca çalışır
}
// Constructor Overloading
private int number;
public MyClass(int number) {
this.number = number;
}
}
class Employee {
private String firstName;
private String lastName;
private int birthYear;
// Constructor with parameters
public Employee(String firstName, String lastName, int birthYear) {
this.firstName = firstName;
this.lastName = lastName;
this.birthYear = birthYear;
}
// Constructor overloading
public Employee(String firstName, String lastName) {
this(firstName, lastName, -1); // Diğer kurucuyu çağırma
}
}
class Vehicle {
private String regNo;
public Vehicle(String regNo) {
this.regNo = regNo;
}
}
class Car extends Vehicle {
private String brand;
// Calling superclass constructor using super
public Car(String brand, String regNo) {
super(regNo);
this.brand = brand;
}
// Constructor throwing exception
public Car(String brand) throws Exception {
super("UNKNOWN");
if (brand == null) {
throw new Exception("Brand cannot be null");
}
this.brand = brand;
}
}
public class Main {
public static void main(String[] args) {
// Constructor çağırma örnekleri
MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass(42);
Employee emp1 = new Employee("Ali", "Yılmaz", 1999);
Employee emp2 = new Employee("Zeynep", "Kaya");
Car car1 = new Car("Toyota", "34ABC123");
try {
Car car2 = new Car(null);
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
}