-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
60 lines (52 loc) · 1.86 KB
/
Copy pathMain.java
File metadata and controls
60 lines (52 loc) · 1.86 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
// Main.java
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class Main {
public static void main(String[] args) throws IOException {
// Abstract class örneği - URL işleyici
URLProcessorImpl processor = new URLProcessorImpl();
processor.process(new URL("https://www.example.com"));
}
}
// Abstract sınıf: nesne yaratılamaz, bazı metotlar abstract olabilir
abstract class MyAbstractClass {
public abstract void abstractMethod();
public void normalMethod() {
System.out.println("Normal method in abstract class");
}
}
// Abstract sınıfın alt sınıfı, abstract metodu override etmek zorunda
class MySubClass extends MyAbstractClass {
@Override
public void abstractMethod() {
System.out.println("Implemented abstractMethod");
}
}
// Abstract sınıf ve Template Method Design Pattern örneği
abstract class URLProcessorBase {
// Şablon metot: bağlantıyı açar, veriyi işler, bağlantıyı kapatır
public void process(URL url) throws IOException {
URLConnection connection = url.openConnection();
InputStream input = connection.getInputStream();
try {
processURLData(input); // abstract metot, alt sınıfta implement edilecek
} finally {
input.close();
}
}
// İşlem metodu abstract, alt sınıf tarafından tanımlanacak
protected abstract void processURLData(InputStream input) throws IOException;
}
// Alt sınıf sadece veri işleme metodunu implement eder
class URLProcessorImpl extends URLProcessorBase {
@Override
protected void processURLData(InputStream input) throws IOException {
int data = input.read();
while (data != -1) {
System.out.print((char) data);
data = input.read();
}
}
}