├── README.md ├── BirResmiKodlamakMain ├── polymorphism_example.java ├── BirResmiKodlamakClass.java ├── BirResmiKodlamakClassCizgiOlusturma.java ├── class_property_method_example.java ├── inheritance_example.java ├── constructor_example.java └── encapsulation_example.java /README.md: -------------------------------------------------------------------------------- 1 | # JAVA_OOP_Fundamentals 2 | Medium JAVA OOP Temelleri yazısı için oluşturulan örnekler 3 | Link: https://medium.com/@kahil_kubilay/java-oop-temelleri-5ebd0b7d062b 4 | -------------------------------------------------------------------------------- /BirResmiKodlamakMain: -------------------------------------------------------------------------------- 1 | package birgörselikodlamak; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * @author mefisto 8 | */ 9 | public class BirGörseliKodlamak { 10 | 11 | /** 12 | * @param args the command line arguments 13 | */ 14 | public static void main(String[] args) { 15 | // TODO::Bir görseli kodlamak 16 | 17 | Point nokta1 = new Point(); 18 | Point nokta2 = new Point(); 19 | 20 | System.out.println("x noktası: " + nokta2.getX() + 21 | "\ny noktası: " + nokta2.getY()); 22 | 23 | Point nokta3 = new Point(12, 5); 24 | 25 | System.out.println("x noktası: " + nokta3.getX() + 26 | "\ny noktası: " + nokta3.getY()); 27 | 28 | Point nokta4 = new Point(6, 8); 29 | 30 | System.out.println("x noktası: " + nokta4.getX() + 31 | "\ny noktası: " + nokta4.getY()); 32 | 33 | Point nokta5 = new Point(); 34 | 35 | nokta5.setX(10); 36 | nokta5.setY(9); 37 | 38 | System.out.println("x noktası: " + nokta5.getX() + 39 | "\ny noktası: " + nokta5.getY()); 40 | 41 | 42 | Point nokta6 = new Point(6, 9); 43 | 44 | System.out.println(nokta6.toString()); 45 | 46 | 47 | Point nokta7 = new Point(5, 7); 48 | 49 | System.out.println(Arrays.toString(nokta7.getXY())); 50 | 51 | 52 | Point nokta8 = new Point(3,5); 53 | 54 | System.out.println(Arrays.toString(nokta8.getXY())); 55 | 56 | nokta8.setXY(4, 6); 57 | 58 | System.out.println(Arrays.toString(nokta8.getXY())); 59 | 60 | 61 | Point nokta9 = new Point(4,2); 62 | 63 | System.out.println(nokta9.distance(8,5)); 64 | 65 | Point nokta10 = new Point(4,2); 66 | 67 | System.out.println(nokta10.distance(new Point(8,5))); 68 | 69 | 70 | Point nokta11 = new Point(4,2); 71 | 72 | System.out.println(nokta11.distance()); 73 | 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /polymorphism_example.java: -------------------------------------------------------------------------------- 1 | package first_test; 2 | 3 | class Cat{ 4 | 5 | public String name; 6 | public int age; 7 | 8 | 9 | //default constructor 10 | public Cat() { 11 | this.name = "İsim değeri girilmedi"; 12 | this.age = 0; 13 | } 14 | 15 | //params constructor 16 | public Cat(String name, int age) { 17 | this.name = name; 18 | this.age = age; 19 | } 20 | 21 | //getter 22 | public String getName() { 23 | return this.name; 24 | } 25 | public int getAge() { 26 | return this.age; 27 | } 28 | 29 | //setter 30 | public void setName(String name) { 31 | this.name = name; 32 | } 33 | public void setAge(int age) { 34 | this.age = age; 35 | } 36 | 37 | public void ioCat(){ 38 | System.out.println("Kedimizin adı: " + this.getName() 39 | + "\nKedimizin yaşı: " + this.getAge() 40 | + "\nKedimiz göz renkleri farklı değildir." + "\n"); 41 | } 42 | 43 | } 44 | 45 | class VanCat extends Cat{ 46 | private boolean eyeColor; 47 | 48 | //params constructor 49 | public VanCat(String name, int age, boolean eyeColor) { 50 | super(name, age); 51 | this.eyeColor = eyeColor; 52 | } 53 | 54 | //get eyeColor 55 | public boolean getEyeColor() { 56 | return this.eyeColor; 57 | } 58 | 59 | //set eyeColor 60 | public void setEyeColor(boolean eyeColor) { 61 | this.eyeColor = eyeColor; 62 | } 63 | 64 | public void ioCat() { 65 | if(this.eyeColor == true) { 66 | System.out.println("Kedimizin adı: " + this.getName() 67 | + "\nKedimizin yaşı: " + this.getAge() 68 | + "\nKedimiz göz renkleri farklıdır." + "\n"); 69 | }else { 70 | System.out.println("Kedimizin adı: " + this.getName() 71 | + "\nKedimizin yaşı: " + this.getAge() 72 | + "\nKedimiz göz renkleri farklı değildir." 73 | + "\nNesne yanlış yerde tanımlanmıştır, lütfen ait olduğu sınıfı düzeltiniz." + "\n"); 74 | } 75 | } 76 | 77 | } 78 | 79 | public class polymorphism_example { 80 | 81 | public static void main(String[] args) { 82 | // TODO polymorphism 83 | 84 | Cat cat1, cat2, cat3; 85 | 86 | cat1 = new Cat("Tarçın", 2); 87 | cat2 = new VanCat("Mesir", 3, true); 88 | cat3 = new VanCat("Macun", 4, false); 89 | 90 | cat1.ioCat(); 91 | cat2.ioCat(); 92 | cat3.ioCat(); 93 | 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /BirResmiKodlamakClass.java: -------------------------------------------------------------------------------- 1 | package birgörselikodlamak; 2 | 3 | public class Point { 4 | 5 | private int x, y; 6 | 7 | //default constructor 8 | public Point(){ 9 | this.x = 0; 10 | this.y = 0; 11 | } 12 | 13 | //params constructor 14 | public Point(int x, int y){ 15 | this.x = x; 16 | this.y = y; 17 | } 18 | 19 | //setter and getter method 20 | public int getX(){ 21 | return this.x; 22 | } 23 | public int getY(){ 24 | return this.y; 25 | } 26 | 27 | public void setX(int x){ 28 | this.x = x; 29 | } 30 | public void setY(int y){ 31 | this.y = y; 32 | } 33 | 34 | //override toString() method 35 | @Override 36 | public String toString(){ 37 | return "(" + getX() + ", " + getY() + ")"; 38 | } 39 | 40 | //getXY():int[2] method 41 | public int[] getXY(){ 42 | int[] pointArr = new int[2]; 43 | pointArr[0] = getX(); 44 | pointArr[1] = getY(); 45 | return pointArr; 46 | } 47 | 48 | //setXY() methodu 49 | public void setXY(int x, int y){ 50 | this.x = x; 51 | this.y = y; 52 | } 53 | 54 | //distance(x:int, y:int) methodu 55 | public double distance(int x, int y){ 56 | int p1, p2, p3, p4; 57 | double result; 58 | 59 | p1 = getX(); //nesnenin x noktası 60 | p2 = x; // uzaklığı hesaplanacak olan mesafenin x noktası 61 | p3 = getY(); //nesnenin y noktası 62 | p4 = y; // uzaklığı hesaplanacak olan mesafenin y noktası 63 | 64 | result = Math.sqrt( Math.pow( (p1-p2), 2 ) + Math.pow( (p3-p4), 2 ) ); 65 | 66 | return result; 67 | } 68 | 69 | //distance(Point:another) methodu 70 | public double distance(Point another){ 71 | int p1, p2, p3, p4; 72 | double result; 73 | 74 | p1 = getX(); //nesnenin x noktası 75 | p2 = another.getX(); // uzaklığı hesaplanacak olan mesafenin x noktası 76 | p3 = getY(); //nesnenin y noktası 77 | p4 = another.getY(); // uzaklığı hesaplanacak olan mesafenin y noktası 78 | 79 | result = Math.sqrt( Math.pow( (p1-p2), 2 ) + Math.pow( (p3-p4), 2 ) ); 80 | 81 | return result; 82 | } 83 | 84 | //distance() methodu 85 | public double distance(){ 86 | int p1, p2; 87 | double result; 88 | 89 | p1 = getX(); //nesnenin x noktası 90 | p2 = getY(); //nesnenin y noktası 91 | 92 | result = Math.sqrt( Math.pow( (p1-0), 2 ) + Math.pow( (p2-0), 2 ) ); 93 | 94 | return result; 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /BirResmiKodlamakClassCizgiOlusturma.java: -------------------------------------------------------------------------------- 1 | package BirGörseliKodlamak; 2 | 3 | public class Line { 4 | 5 | private Point begin, end; 6 | 7 | //parameters constructor 8 | public Line(int x1, int y1, int x2, int y2) { 9 | this.begin = new Point(x1, y1); 10 | this.end = new Point(x2, y2); 11 | } 12 | 13 | //point value constructor 14 | public Line(Point firstPoint, Point lastPoint) { 15 | this.begin = new Point(firstPoint.getX(), firstPoint.getY()); 16 | this.end = new Point(lastPoint.getX(), lastPoint.getY()); 17 | } 18 | 19 | //setter & getter 20 | public Point getBegin() { 21 | return this.begin; 22 | } 23 | 24 | public Point getEnd() { 25 | return this.end; 26 | } 27 | 28 | public void setBegin(Point firstPoint) { 29 | this.begin.setX(firstPoint.getX()); 30 | this.begin.setY(firstPoint.getY()); 31 | } 32 | 33 | public void setEnd(Point lastPoint) { 34 | this.end.setX(lastPoint.getX()); 35 | this.end.setY(lastPoint.getY()); 36 | } 37 | 38 | //get set begin & get set end 39 | public int getBeginX() { 40 | return this.begin.getX(); 41 | } 42 | 43 | public int getBeginY() { 44 | return this.begin.getY(); 45 | } 46 | 47 | public int getEndX() { 48 | return this.end.getX(); 49 | } 50 | 51 | public int getEndY() { 52 | return this.end.getY(); 53 | } 54 | 55 | public void setBeginX(int x) { 56 | this.begin.setX(x); 57 | } 58 | 59 | public void setBeginY(int y) { 60 | this.begin.setY(y); 61 | } 62 | 63 | public void setEndX(int x) { 64 | this.end.setX(x); 65 | } 66 | 67 | public void setEndY(int y) { 68 | this.end.setY(y); 69 | } 70 | 71 | //getBeginXY:int[2] method 72 | public int[] getBeginXY() { 73 | int[] pointArr = new int[2]; 74 | pointArr[0] = this.getBeginX(); 75 | pointArr[1] = this.getBeginY(); 76 | 77 | return pointArr; 78 | } 79 | 80 | //getEndXY:int[2] method 81 | public int[] getEndXY() { 82 | int[] pointArr = new int[2]; 83 | pointArr[0] = this.getEndX(); 84 | pointArr[1] = this.getEndY(); 85 | 86 | return pointArr; 87 | } 88 | 89 | //setBeginXY(x:int, y:int):void method 90 | public void setBeginXY(int x, int y) { 91 | this.begin.setX(x); 92 | this.begin.setY(y); 93 | } 94 | 95 | //setBeginXY(x:int, y:int):void method 96 | public void setEndXY(int x, int y) { 97 | this.end.setX(x); 98 | this.end.setY(y); 99 | } 100 | 101 | //toString() method 102 | public String toString() { 103 | return "'Line[begin=(" + this.getBeginX() + ", " + this.getBeginY() + "), end=(" + 104 | this.getEndX() + ", " + this.getEndY() + ")]'"; 105 | } 106 | 107 | //getLength():double 108 | public double getLength() { 109 | double result = begin.distance(end); 110 | 111 | return result; 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /class_property_method_example.java: -------------------------------------------------------------------------------- 1 | package mediumBlog; 2 | 3 | import java.util.Scanner; 4 | 5 | //kedi nesnemiz 6 | class Cat { 7 | 8 | String name; 9 | String color; 10 | int age; 11 | int hungry; 12 | 13 | void eatFood(int foodVal) { 14 | this.hungry += foodVal; 15 | } 16 | 17 | } 18 | 19 | //bilgisayar nesnemiz 20 | class Computer{ 21 | 22 | String brand = "bilgisayar markası girilmedi."; //default değerimiz 23 | String processor_name = "İşlemci markası girilmedi."; //default değerimiz 24 | double processor_ghz = 0.0; //default değerimiz 25 | double price = 0.0; //default değerimiz 26 | int ram = 0; //default değerimiz 27 | 28 | void io_method(String value) { 29 | System.out.println(value); 30 | } 31 | 32 | } 33 | 34 | //üniversite nesnemiz 35 | class university{ 36 | 37 | String name; 38 | String department; 39 | int years; 40 | 41 | void io_university_info(String name, String department, int years) { 42 | this.name = name; //atama işlemini yaptığımız değişkenler, 43 | this.department = department; //parametremizden gelecek olan değişkeni 44 | this.years = years; //classımız içerisinde bulunan değere atayacaktır. 45 | 46 | System.out.println("\nDeğişiklikler kaydedildi.."); 47 | System.out.println("Üniversite adı: " + this.name 48 | + "\nÜniversite bölümü: " + this.department 49 | + "\nÖğrenim süresi: " + this.years); 50 | } 51 | 52 | } 53 | 54 | public class OOP_examples { 55 | 56 | public static void main(String[] args) { 57 | 58 | //kedi nesnemiz 59 | Cat cat1 = new Cat(); 60 | 61 | cat1.name = "Tarcin"; 62 | cat1.color = "yellow"; 63 | cat1.age = 3; 64 | cat1.hungry = 2; 65 | 66 | System.out.println("Kedinin ismi: " + cat1.name + "\nKedinin yaşı: " + cat1.age + "\nKedinin rengi: " + cat1.color); 67 | System.out.println("Kedinin açlık durumu: " + cat1.hungry); 68 | 69 | cat1.eatFood(3); 70 | 71 | System.out.println("Kedinin açlık durumu: " + cat1.hungry); 72 | 73 | //bilgisayar nesnemiz 74 | Computer asus = new Computer(); 75 | 76 | asus.brand = "asus"; 77 | asus.price = 2.130; 78 | asus.processor_name = "Intel(R) Core i5-5200"; 79 | asus.processor_ghz = 3.40; 80 | asus.ram = 4; 81 | 82 | asus.io_method("\nBilgisayar I_O methodlarını uygular.."); 83 | System.out.println("Sistem Özellikleri" 84 | + "\nMarka: " + asus.brand 85 | + "\nFiyat: " + asus.price 86 | + "\nİşlemci modeli: " + asus.processor_name 87 | + "\nİşlemci Dönüş Hızı: " + asus.processor_ghz 88 | + "\nRAM: " + asus.ram); 89 | 90 | //üniversite nesnemiz 91 | university uni_1 = new university(); 92 | Scanner input = new Scanner(System.in); 93 | 94 | System.out.print("\nÜniversite adını giriniz: "); 95 | uni_1.name = input.nextLine(); 96 | System.out.print("Bölüm adını giriniz: "); 97 | uni_1.department = input.nextLine(); 98 | System.out.print("Öğrenim süresini giriniz: "); 99 | uni_1.years = input.nextInt(); 100 | 101 | input.close(); 102 | 103 | uni_1.io_university_info(uni_1.name, uni_1.department, uni_1.years); 104 | 105 | System.exit(0); 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /inheritance_example.java: -------------------------------------------------------------------------------- 1 | package first_test; 2 | 3 | //kedi sınıfımız 4 | class Cat{ 5 | 6 | public String name; 7 | public String color; 8 | public int age; 9 | public int hungry; 10 | 11 | void eatFood(int foodVal) { 12 | this.hungry += foodVal; 13 | } 14 | 15 | } 16 | 17 | //kedi sınıfından türetilen, van kedisi sınıfımız 18 | class VanCat extends Cat{ 19 | 20 | } 21 | 22 | //kedi sınıfından türetilen, mavi rus kedisi sınıfımız 23 | class BlueRussiaCat extends Cat{ 24 | 25 | } 26 | 27 | //kedi sınıfından türetilen, fars kedisi sınıfımız 28 | class PersianCat extends Cat{ 29 | 30 | } 31 | 32 | //araba sınıfımız 33 | class Car{ 34 | 35 | //field 36 | public int door; 37 | public int wheel; 38 | public int engine; 39 | int speed; 40 | 41 | //methods 42 | void go(int speed) { 43 | System.out.println("Gaza basıldı"); 44 | if(this.speed == 0) { 45 | System.out.println("araç hareket etmeye başladı"); 46 | } 47 | this.speed += speed; 48 | } 49 | public int stop(int speed) { 50 | System.out.println("Frene basıldı"); 51 | this.speed -= speed; 52 | 53 | if(this.speed <= 0) { 54 | this.speed = 0; 55 | System.out.println("araç durdu"); 56 | } 57 | return this.speed; 58 | 59 | } 60 | 61 | //main constructor 62 | public Car(){ 63 | this.door = 4; 64 | this.wheel = 4; 65 | this.engine = 1; 66 | this.speed = 0; 67 | } 68 | 69 | //params constructor 70 | public Car(int door, int wheel, int engine, int speed) { 71 | this.door = door; 72 | this.wheel = wheel; 73 | this.engine = engine; 74 | this.speed = speed; 75 | } 76 | 77 | //all variable getter 78 | public int getDoor() { 79 | return door; 80 | } 81 | public int getWheel() { 82 | return wheel; 83 | } 84 | public int getEngine() { 85 | return engine; 86 | } 87 | public int getSpeed() { 88 | return speed; 89 | } 90 | 91 | //all variable setter 92 | public void setDoor(int door) { 93 | this.door = door; 94 | } 95 | public void setWheel(int wheel) { 96 | this.wheel = wheel; 97 | } 98 | public void setEngine(int engine) { 99 | this.engine = engine; 100 | } 101 | public void setSpeed(int speed) { 102 | this.speed = speed; 103 | } 104 | } 105 | 106 | //Araba sınıfımızdan türetilen Mercedes sınıfımız 107 | class Mercedes extends Car{ 108 | 109 | } 110 | 111 | public class inhertitance_ex { 112 | 113 | public static void main(String[] args) { 114 | // TODO Auto-generated method stub 115 | 116 | //kedi ve alt nesnelerimiz 117 | VanCat cat1 = new VanCat(); 118 | BlueRussiaCat cat2 = new BlueRussiaCat(); 119 | PersianCat cat3 = new PersianCat(); 120 | 121 | cat1.name = "Tarçın"; 122 | cat2.color = "blue"; 123 | cat3.age = 3; 124 | 125 | System.out.println(cat1.name); 126 | System.out.println(cat2.color); 127 | System.out.println(cat3.age + "\n"); 128 | 129 | //araba ve alt nesnelerimiz 130 | Mercedes c250 = new Mercedes(); 131 | 132 | c250.go(100); 133 | 134 | System.out.println(c250.getSpeed()); 135 | 136 | c250.go(20); 137 | 138 | System.out.println(c250.getSpeed()); 139 | 140 | c250.go(30); 141 | 142 | System.out.println(c250.getSpeed()); 143 | 144 | c250.stop(45); 145 | 146 | System.out.println(c250.getSpeed()); 147 | 148 | c250.stop(435); 149 | 150 | System.out.println(c250.getSpeed()); 151 | 152 | c250.go(10); 153 | 154 | System.out.println(c250.getSpeed()); 155 | 156 | c250.stop(10); 157 | 158 | System.out.println(c250.getSpeed()); 159 | 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /constructor_example.java: -------------------------------------------------------------------------------- 1 | package first_test; 2 | 3 | import java.util.Scanner; 4 | 5 | //kedi nesnemiz 6 | class Cat{ 7 | 8 | String name; 9 | String color; 10 | int age; 11 | int hungry; 12 | 13 | void eatFood(int foodVal) { 14 | this.hungry += foodVal; 15 | } 16 | 17 | //constructor classımız 18 | public Cat() { 19 | this.name = "isim değeri girilmedi"; 20 | this.color = "renk değeri girilmedi"; 21 | this.age = 0; 22 | this.hungry = 0; 23 | 24 | } 25 | 26 | public Cat(String name, int age) { 27 | this.name = name; 28 | this.age = age; 29 | } 30 | 31 | public Cat(String name, String color, int age, int hungry) { 32 | this.name = name; 33 | this.color = color; 34 | this.age = age; 35 | this.hungry = hungry; 36 | } 37 | 38 | } 39 | 40 | class Book{ 41 | 42 | String name; 43 | String author; 44 | double price; 45 | int pageNumb; 46 | 47 | //parametresiz constructor 48 | // ****constructor kullanmak istiyorsak, muhakkak parametresiz bir constructor yapısı 49 | //oluşturmamız gerekiyor. 50 | public Book() { 51 | this.name = "kitap ismi girilmedi"; 52 | this.author = "kitap yazarı girilmedi"; 53 | this.price = 0.0; 54 | this.pageNumb = 0; 55 | } 56 | 57 | //parametreli constructor 58 | public Book(String name, String author, double price, int pageNumb) { 59 | this.name = name; 60 | this.author = author; 61 | this.price = price; 62 | this.pageNumb = pageNumb; 63 | } 64 | 65 | // 66 | 67 | } 68 | 69 | public class constructor_example { 70 | 71 | public static void main(String[] args) { 72 | // TODO constructor 73 | 74 | //kedi nesnemiz 75 | Cat cat1 = new Cat(); 76 | Cat cat2 = new Cat(); 77 | Cat cat3 = new Cat("Mesir", "gri", 2, 3); 78 | 79 | cat2.name = "Tarcin"; 80 | cat2.age = 2; 81 | 82 | System.out.println(cat1.name + "\n" 83 | + cat1.color + "\n" 84 | + cat1.age + "\n" 85 | + cat1.hungry + "\n"); 86 | 87 | System.out.println(cat2.name + "\n" 88 | + cat2.color + "\n" 89 | + cat2.age + "\n" 90 | + cat2.hungry + "\n"); 91 | 92 | System.out.println(cat3.name + "\n" 93 | + cat3.color + "\n" 94 | + cat3.age + "\n" 95 | + cat3.hungry + "\n"); 96 | 97 | //kitap nesnemiz 98 | Book book1 = new Book(); 99 | Book book2 = new Book("Fırat Suyu Kan Akıyor Baksana", "Yaşar Kemal", 21.11, 328); 100 | Book book3 = new Book(); 101 | 102 | book1.name = "Devlet Ana"; 103 | book1.author = "Kemal Tahir"; 104 | book1.price = 22.78; 105 | book1.pageNumb = 651; 106 | 107 | System.out.println("\nKitap adı: " + book1.name 108 | + "\nKitap yazarı: " + book1.author 109 | + "\nKitap fiyatı: " + book1.price 110 | + "\nKitap sayfa sayısı: " + book1.pageNumb); 111 | 112 | System.out.println("\nKitap adı: " + book2.name 113 | + "\nKitap yazarı: " + book2.author 114 | + "\nKitap fiyatı: " + book2.price 115 | + "\nKitap sayfa sayısı: " + book2.pageNumb); 116 | 117 | //Veri girişi yaparak yeni bir kitap nesne olusturalım. 118 | Scanner input = new Scanner(System.in); 119 | 120 | System.out.println("\nBir kitap nesnesi olusturacağiz. Asagidaki bilgileri giriniz."); 121 | System.out.print("Kitap adı: " ); 122 | book3.name = input.nextLine(); 123 | System.out.print("Kitap yazarı: " ); 124 | book3.author = input.nextLine(); 125 | System.out.print("Kitap fiyatı: " ); 126 | book3.price = input.nextDouble(); 127 | System.out.print("Kitap sayfa sayısı: " ); 128 | book3.pageNumb = input.nextInt(); 129 | 130 | input.close(); 131 | 132 | System.out.println("\nOluşturduğunuz nesnemizin özellikleri: "); 133 | System.out.println("Kitap adı: " + book3.name 134 | + "\nKitap yazarı: " + book3.author 135 | + "\nKitap fiyatı: " + book3.price 136 | + "\nKitap sayfa sayısı: " + book3.pageNumb); 137 | 138 | System.exit(0); 139 | 140 | } 141 | 142 | } 143 | -------------------------------------------------------------------------------- /encapsulation_example.java: -------------------------------------------------------------------------------- 1 | package first_test; 2 | 3 | import java.util.Scanner; 4 | 5 | //kedi nesnemiz 6 | class Cat{ 7 | 8 | public String name; 9 | public String color; 10 | private int age; 11 | int hungry; 12 | 13 | void eatFood(int foodVal) { 14 | this.hungry += foodVal; 15 | } 16 | 17 | //main constructor 18 | public Cat() { 19 | this.name = "isim degeri girilmedi."; 20 | this.color = "renk degeri girilmedi."; 21 | this.age = 0; 22 | this.hungry = 0; 23 | } 24 | 25 | //parameters constructor 26 | public Cat(String name, String color, int age, int hungry) { 27 | this.name = name; 28 | this.color = color; 29 | this.age = age; 30 | this.hungry = hungry; 31 | } 32 | 33 | //getter age 34 | public int getAge() { 35 | return age; 36 | } 37 | 38 | //setter age 39 | public void setAge(int age) { 40 | this.age = age; 41 | } 42 | 43 | } 44 | 45 | //kitap nesnemiz 46 | class Book{ 47 | 48 | private String name; 49 | private String author; 50 | private double price; 51 | private int pageNumb; 52 | 53 | //main constructor 54 | public Book() { 55 | this.name = "name değeri girilmedi"; 56 | this.author = "yazar değeri girilmedi"; 57 | this.price = 0.0; 58 | this.pageNumb = 0; 59 | } 60 | 61 | //param constructor 62 | public Book(String name, String author, double price, int pageNumb) { 63 | this.name = name; 64 | this.author= author; 65 | this.price = price; 66 | this.pageNumb = pageNumb; 67 | } 68 | 69 | //getter all variable 70 | public String getName() { 71 | return name; 72 | } 73 | public String getAuthor() { 74 | return author; 75 | } 76 | public double getPrice() { 77 | return price; 78 | } 79 | public int getPageNumb() { 80 | return pageNumb; 81 | } 82 | 83 | //setter all element 84 | public void setName(String name) { 85 | this.name = name; 86 | } 87 | public void setAuthor(String author) { 88 | this.author = author; 89 | } 90 | public void setPrice(double price) { 91 | this.price = price; 92 | } 93 | public void setPageNumb(int pageNumb) { 94 | this.pageNumb = pageNumb; 95 | } 96 | 97 | //her nesne için tek tek yazdırma yapmak yerine, method belirleyerek 98 | //tek kod içerisinde birden fazla işlemi yapabiliriz. 99 | public void book_info_io() { 100 | System.out.println("Kitap adı: " + this.name 101 | + "\nKitap yazarı: " + this.author 102 | + "\nKitap fiyatı: " + this.price 103 | + "\nKitap sayfa sayısı: " + this.pageNumb ); 104 | } 105 | } 106 | 107 | //bilgisayar nesnemiz 108 | class Computer{ 109 | 110 | private String brand; 111 | private String processor_name; 112 | private double processor_ghz; 113 | private int ram; 114 | 115 | //main(default) constructor 116 | public Computer() { 117 | this.brand = "Marka değeri girilmedi"; 118 | this.processor_name = "İşlemci değeri girilmedi"; 119 | this.processor_ghz = 0.0; 120 | this.ram = 0; 121 | } 122 | 123 | //params constructor 124 | public Computer(String brand, String processor_name, double processor_ghz, int ram) { 125 | this.brand = brand; 126 | this.processor_name = processor_name; 127 | this.processor_ghz = processor_ghz; 128 | this.ram = ram; 129 | } 130 | 131 | //getter all element 132 | public String getBrand() { 133 | return brand; 134 | } 135 | public String getProcessorName() { 136 | return processor_name; 137 | } 138 | public double getProcessorGHZ() { 139 | return processor_ghz; 140 | } 141 | public int getRam() { 142 | return ram; 143 | } 144 | 145 | //setter all element 146 | public void setBrand(String brand) { 147 | this.brand = brand; 148 | } 149 | public void setProcessorName(String processor_name) { 150 | this.processor_name = processor_name; 151 | } 152 | public void setProcessorGHZ(double processor_ghz) { 153 | this.processor_ghz = processor_ghz; 154 | } 155 | public void setRam(int ram) { 156 | this.ram = ram; 157 | } 158 | 159 | //her nesne için tek tek yazdırma yapmak yerine, method belirleyerek 160 | //tek kod içerisinde birden fazla işlemi yapabiliriz. 161 | public void io_variable() { 162 | System.out.println("\nbook3 nesnesinin özellikleri: "); 163 | System.out.println(this.brand + "\n" 164 | + this.processor_name + "\n" 165 | + this.processor_ghz + "\n" 166 | + this.ram); 167 | } 168 | 169 | } 170 | 171 | public class encapsulation_example { 172 | 173 | public static void main(String[] args) { 174 | // TODO encapsulation examples 175 | 176 | //kedi nesnemiz 177 | Cat cat1 = new Cat(); 178 | 179 | cat1.setAge(2); 180 | 181 | System.out.println(cat1.getAge()); 182 | 183 | //kitap nesnemiz 184 | Book book1 = new Book(); 185 | Book book2 = new Book(); 186 | 187 | 188 | book1.setName("Fırat Suyu Kan Akıyor Baksana"); 189 | book1.setAuthor("Yaşar Kemal"); 190 | book1.setPrice(13.33); 191 | book1.setPageNumb(318); 192 | 193 | System.out.println("\nbook1 nesnesinin özellikleri:"); 194 | book1.book_info_io(); 195 | 196 | book2.setName("Devlet Ana"); 197 | book2.setAuthor("Kemal Tahir"); 198 | book2.setPrice(22.78); 199 | book2.setPageNumb(651); 200 | 201 | System.out.println("\nbook2 nesnesinin özellikleri:"); 202 | System.out.println("Kitap adı: " + book2.getName() + 203 | "\nKitap yazarı: " + book2.getAuthor() + 204 | "\nKitap fiyatı: " + book2.getPrice() + 205 | "\nKitap sayfa sayısı: " + book2.getPageNumb()); 206 | 207 | //bilgisayar nesnesi 208 | Computer comp1 = new Computer(); 209 | 210 | Scanner input = new Scanner(System.in); 211 | 212 | System.out.println("\nYeni bir bilgisayar nesnesi olusturabilmek için, aşağıdaki bilgileri giriniz."); 213 | System.out.print("Bilgisayar markasi: "); 214 | comp1.setBrand(input.nextLine()); 215 | System.out.print("Bilgisayar işlemci modeli: "); 216 | comp1.setProcessorName(input.nextLine()); 217 | System.out.print("İşlemci dönüş hızı: "); 218 | comp1.setProcessorGHZ(input.nextDouble()); 219 | System.out.print("ram boyutu: "); 220 | comp1.setRam(input.nextInt()); 221 | 222 | comp1.io_variable(); 223 | 224 | input.close(); 225 | 226 | System.exit(0); 227 | 228 | } 229 | 230 | } 231 | --------------------------------------------------------------------------------