The document discusses the Scanner class in Java, which is used to get user input from the keyboard. It describes several methods of the Scanner class like nextInt(), nextFloat(), nextLine() that can be used to read integer, float and string values from the user. It also provides an example program that uses the Scanner class to take integer, float and string inputs from the user and print them.
Superclasses, and Subclasses, Overriding and Hiding Methods, Polymorphism, Inheritance Hierarchies, Super keyword, Final Classes and Methods, Abstract,
Classes and Methods, Nested classes & Inner Classes,
finalization and garbage collection.
This document compares and contrasts abstract classes and interfaces in Java. It defines abstract classes as partially abstract reusable code that can contain both abstract and concrete methods, while interfaces are fully abstract reusable contracts that can only contain abstract methods. The document provides examples of abstract class and interface syntax and usage, highlighting that abstract classes can have constructors and non-public members while interfaces cannot. It concludes that abstract classes are not completely abstract but interfaces are.
Java vs. C#
The document compares Java and C# programming languages. It discusses some key differences:
1. Syntax differences such as main method signatures, print statements, and array declarations are slightly different between the two languages.
2. Some concepts are modified in C# from Java, such as polymorphism requiring the virtual keyword, operator overloading restrictions, and switch statements allowing string cases.
3. C# introduces new concepts not in Java like enumerations, foreach loops, properties to encapsulate fields, pointers in unsafe contexts, and passing arguments by reference.
String is a non-primitive and immutable data type in Java that represents a sequence of characters. It is stored in the String Constant Pool in the heap memory. Methods like equals(), concat(), contains(), indexOf() etc. are used to perform operations on strings. String is immutable to prevent unexpected behavior if the contents of a string are changed.
Lambda expressions, default methods in interfaces, and the new date/time API are among the major new features in Java 8. Lambda expressions allow for functional-style programming by treating functionality as a method argument or anonymous implementation. Default methods add new capabilities to interfaces while maintaining backwards compatibility. The date/time API improves on the old Calendar and Date APIs by providing immutable and easier to use classes like LocalDate.
A thread is an independent path of execution within a Java program. The Thread class in Java is used to create threads and control their behavior and execution. There are two main ways to create threads - by extending the Thread class or implementing the Runnable interface. The run() method contains the code for the thread's task and threads can be started using the start() method. Threads have different states like New, Runnable, Running, Waiting etc during their lifecycle.
The document discusses the Scanner class in Java, which is used to get user input from the keyboard. It describes several methods of the Scanner class like nextInt(), nextFloat(), nextLine() that can be used to read integer, float and string values from the user. It also provides an example program that uses the Scanner class to take integer, float and string inputs from the user and print them.
Superclasses, and Subclasses, Overriding and Hiding Methods, Polymorphism, Inheritance Hierarchies, Super keyword, Final Classes and Methods, Abstract,
Classes and Methods, Nested classes & Inner Classes,
finalization and garbage collection.
This document compares and contrasts abstract classes and interfaces in Java. It defines abstract classes as partially abstract reusable code that can contain both abstract and concrete methods, while interfaces are fully abstract reusable contracts that can only contain abstract methods. The document provides examples of abstract class and interface syntax and usage, highlighting that abstract classes can have constructors and non-public members while interfaces cannot. It concludes that abstract classes are not completely abstract but interfaces are.
Java vs. C#
The document compares Java and C# programming languages. It discusses some key differences:
1. Syntax differences such as main method signatures, print statements, and array declarations are slightly different between the two languages.
2. Some concepts are modified in C# from Java, such as polymorphism requiring the virtual keyword, operator overloading restrictions, and switch statements allowing string cases.
3. C# introduces new concepts not in Java like enumerations, foreach loops, properties to encapsulate fields, pointers in unsafe contexts, and passing arguments by reference.
String is a non-primitive and immutable data type in Java that represents a sequence of characters. It is stored in the String Constant Pool in the heap memory. Methods like equals(), concat(), contains(), indexOf() etc. are used to perform operations on strings. String is immutable to prevent unexpected behavior if the contents of a string are changed.
Lambda expressions, default methods in interfaces, and the new date/time API are among the major new features in Java 8. Lambda expressions allow for functional-style programming by treating functionality as a method argument or anonymous implementation. Default methods add new capabilities to interfaces while maintaining backwards compatibility. The date/time API improves on the old Calendar and Date APIs by providing immutable and easier to use classes like LocalDate.
A thread is an independent path of execution within a Java program. The Thread class in Java is used to create threads and control their behavior and execution. There are two main ways to create threads - by extending the Thread class or implementing the Runnable interface. The run() method contains the code for the thread's task and threads can be started using the start() method. Threads have different states like New, Runnable, Running, Waiting etc during their lifecycle.
This presentation provides an overview of the Java programming language. It discusses what Java is, where it is used, its platforms and editions. Key features of Java like being object-oriented, platform independent and having a virtual machine are explained. The concepts of object-oriented programming like objects, classes, inheritance and polymorphism are also summarized. Data types in Java and different types of variables are briefly covered. Advantages of Java like being simple, not using pointers and the ability to write programs that can be executed on the web are highlighted.
This document discusses object-oriented programming concepts like inheritance, polymorphism, overloading, and overriding. It provides examples of inheritance hierarchies for different types of birds. Inheritance allows subclasses to inherit attributes and methods from parent classes. Polymorphism means an object can have different implementations of the same method depending on its type. Overloading and overriding relate to creating multiple methods with the same name that differ in parameters or implementation.
Abstract classes and interfaces allow for abstraction and polymorphism in object-oriented design. Abstract classes can contain both abstract and concrete methods, while interfaces only contain abstract methods. Abstract classes are used to provide a common definition for subclasses through inheritance, while interfaces define a contract for implementing classes to follow. Both increase complexity, so their use should provide clear benefits to functionality.
Java comments:
→ Java comments are statements that are not executed by the compiler and interpreter.
→ The comments can be used to provide information about each line of code.
→ It can also be used to hide program code for specific time.
Types of Java Comments:
→ Single Line Comment:
→ The single line comment is used to comment only one line.
//This is single line comment
→ Multi Line Comment:
→ The multi line comment is used to comment multiple lines of code.
/*
This is
multi line comment
*/
→ Documentation Comment:
→ The documentation comment is used to create documentation API.
/**
This is
documentation comment
*/
-------------
Java concurrency allows applications to make use of multiple processors and handle asynchronous operations through the use of threads. While concurrency provides benefits like improved performance, it also introduces risks like race conditions and deadlocks if threads access shared resources unsafely. The Java threading APIs provide tools for safely managing concurrent operations through mechanisms like synchronization, locks, and concurrent collections.
The document discusses key concepts of object-oriented programming including objects, classes, inheritance, polymorphism, encapsulation, and abstraction. It provides examples of constructors, method overloading and overriding, interfaces, and packages in Java.
The document discusses objects and classes in Java. It defines a class as a template for creating objects with common properties and behaviors. A class contains fields to store data and methods to perform actions on that data. The document uses the Circle class as an example, defining fields like radius and center coordinates, and methods like circumference() and area(). It explains how to define classes, create objects from classes using the new keyword, access object fields and methods, and use nested classes, inner classes, local classes and anonymous classes in Java.
This document contains the detailed description that what vararg method is? Why it came in existence? What is it's advantage? When it is appropriate to use vararg method? and most of the important that the cases that should be considered before using vararg methods.
String objects are stored in the constant string pool and are immutable. StringBuffer and StringBuilder objects are stored in the heap and are mutable. StringBuffer is thread-safe while StringBuilder is non-thread-safe but faster than StringBuffer. The key differences are that String objects cannot be modified, while StringBuffer and StringBuilder can modify their character sequences via append and insert methods. String concatenation involves more steps than StringBuffer concatenation via the append method.
OCA Java SE 8 Exam Chapter 5 Class Designİbrahim Kürce
Inheritance is the process by which the new child subclass automatically includes any public or protected primitives, objects, or methods defined in the parent class.
We refer to any class that inherits from another class as a child class, or a descendent of that class.
We refer to the class that the child inherits from as the parent class, or an ancestor of the class.
This document provides an overview of object-oriented programming (OOP) concepts in C#, including classes, objects, inheritance, encapsulation, and polymorphism. It defines key terms like class and object, and explains how C# supports OOP principles such as defining classes with methods and properties, extending classes through inheritance, hiding implementation through encapsulation, and allowing polymorphic behavior through function overloading and overriding. Abstract classes and sealed modifiers are also covered. The document is intended to help explain basic OOP concepts in C# to readers.
The document provides an overview of core Java concepts including:
- Java is an object-oriented programming language and platform that runs on a virtual machine. It is used to create desktop, web, enterprise, mobile and other applications.
- Core Java concepts include objects, classes, inheritance, polymorphism, abstraction and encapsulation. The document also discusses variables and data types, OOP principles, object creation, method overloading and constructors.
- It provides examples of Hello World programs and explains Java memory areas like stack and heap. Key topics like static keyword, method vs constructor and method overloading are also summarized.
The document discusses object-oriented programming (OOP) concepts in Java, including classes, objects, inheritance, abstraction, encapsulation, and polymorphism. It provides definitions and examples for each concept. Classes create blueprints for objects and group similar entities, while objects are instances of classes that have state and behavior. OOP uses these concepts to create reusable applications with clearer structure and less code through concepts like inheritance, abstraction of unnecessary details, encapsulation of data within classes, and polymorphism to have variables take on multiple forms based on context.
The Spring Framework has always embraced testing as a first class citizen. Spring-based components should be modular, easy to wire together via dependency injection, and therefore easy to test. In fact, when well designed following a POJO programming model, a component in a Spring application can be unit tested without using Spring at all. And when you take the step toward developing integration tests, Spring's testing support is there to make your job easy.
Join Spring Test component lead Sam Brannen in this talk to learn about the basics for Spring's unit and integration testing support. This talk will provide attendees an overview of the following topics: unit testing without Spring, integration testing with Spring, loading application contexts (with and without context hierarchies), injecting dependencies into tests, transaction management for tests, SQL script execution, testing Spring MVC and REST web applications, and more.
This presentation provides an overview of the Java programming language. It discusses what Java is, where it is used, its platforms and editions. Key features of Java like being object-oriented, platform independent and having a virtual machine are explained. The concepts of object-oriented programming like objects, classes, inheritance and polymorphism are also summarized. Data types in Java and different types of variables are briefly covered. Advantages of Java like being simple, not using pointers and the ability to write programs that can be executed on the web are highlighted.
This document discusses object-oriented programming concepts like inheritance, polymorphism, overloading, and overriding. It provides examples of inheritance hierarchies for different types of birds. Inheritance allows subclasses to inherit attributes and methods from parent classes. Polymorphism means an object can have different implementations of the same method depending on its type. Overloading and overriding relate to creating multiple methods with the same name that differ in parameters or implementation.
Abstract classes and interfaces allow for abstraction and polymorphism in object-oriented design. Abstract classes can contain both abstract and concrete methods, while interfaces only contain abstract methods. Abstract classes are used to provide a common definition for subclasses through inheritance, while interfaces define a contract for implementing classes to follow. Both increase complexity, so their use should provide clear benefits to functionality.
Java comments:
→ Java comments are statements that are not executed by the compiler and interpreter.
→ The comments can be used to provide information about each line of code.
→ It can also be used to hide program code for specific time.
Types of Java Comments:
→ Single Line Comment:
→ The single line comment is used to comment only one line.
//This is single line comment
→ Multi Line Comment:
→ The multi line comment is used to comment multiple lines of code.
/*
This is
multi line comment
*/
→ Documentation Comment:
→ The documentation comment is used to create documentation API.
/**
This is
documentation comment
*/
-------------
Java concurrency allows applications to make use of multiple processors and handle asynchronous operations through the use of threads. While concurrency provides benefits like improved performance, it also introduces risks like race conditions and deadlocks if threads access shared resources unsafely. The Java threading APIs provide tools for safely managing concurrent operations through mechanisms like synchronization, locks, and concurrent collections.
The document discusses key concepts of object-oriented programming including objects, classes, inheritance, polymorphism, encapsulation, and abstraction. It provides examples of constructors, method overloading and overriding, interfaces, and packages in Java.
The document discusses objects and classes in Java. It defines a class as a template for creating objects with common properties and behaviors. A class contains fields to store data and methods to perform actions on that data. The document uses the Circle class as an example, defining fields like radius and center coordinates, and methods like circumference() and area(). It explains how to define classes, create objects from classes using the new keyword, access object fields and methods, and use nested classes, inner classes, local classes and anonymous classes in Java.
This document contains the detailed description that what vararg method is? Why it came in existence? What is it's advantage? When it is appropriate to use vararg method? and most of the important that the cases that should be considered before using vararg methods.
String objects are stored in the constant string pool and are immutable. StringBuffer and StringBuilder objects are stored in the heap and are mutable. StringBuffer is thread-safe while StringBuilder is non-thread-safe but faster than StringBuffer. The key differences are that String objects cannot be modified, while StringBuffer and StringBuilder can modify their character sequences via append and insert methods. String concatenation involves more steps than StringBuffer concatenation via the append method.
OCA Java SE 8 Exam Chapter 5 Class Designİbrahim Kürce
Inheritance is the process by which the new child subclass automatically includes any public or protected primitives, objects, or methods defined in the parent class.
We refer to any class that inherits from another class as a child class, or a descendent of that class.
We refer to the class that the child inherits from as the parent class, or an ancestor of the class.
This document provides an overview of object-oriented programming (OOP) concepts in C#, including classes, objects, inheritance, encapsulation, and polymorphism. It defines key terms like class and object, and explains how C# supports OOP principles such as defining classes with methods and properties, extending classes through inheritance, hiding implementation through encapsulation, and allowing polymorphic behavior through function overloading and overriding. Abstract classes and sealed modifiers are also covered. The document is intended to help explain basic OOP concepts in C# to readers.
The document provides an overview of core Java concepts including:
- Java is an object-oriented programming language and platform that runs on a virtual machine. It is used to create desktop, web, enterprise, mobile and other applications.
- Core Java concepts include objects, classes, inheritance, polymorphism, abstraction and encapsulation. The document also discusses variables and data types, OOP principles, object creation, method overloading and constructors.
- It provides examples of Hello World programs and explains Java memory areas like stack and heap. Key topics like static keyword, method vs constructor and method overloading are also summarized.
The document discusses object-oriented programming (OOP) concepts in Java, including classes, objects, inheritance, abstraction, encapsulation, and polymorphism. It provides definitions and examples for each concept. Classes create blueprints for objects and group similar entities, while objects are instances of classes that have state and behavior. OOP uses these concepts to create reusable applications with clearer structure and less code through concepts like inheritance, abstraction of unnecessary details, encapsulation of data within classes, and polymorphism to have variables take on multiple forms based on context.
The Spring Framework has always embraced testing as a first class citizen. Spring-based components should be modular, easy to wire together via dependency injection, and therefore easy to test. In fact, when well designed following a POJO programming model, a component in a Spring application can be unit tested without using Spring at all. And when you take the step toward developing integration tests, Spring's testing support is there to make your job easy.
Join Spring Test component lead Sam Brannen in this talk to learn about the basics for Spring's unit and integration testing support. This talk will provide attendees an overview of the following topics: unit testing without Spring, integration testing with Spring, loading application contexts (with and without context hierarchies), injecting dependencies into tests, transaction management for tests, SQL script execution, testing Spring MVC and REST web applications, and more.
Effective Java - Madde 1: Yapılandırıcılar yerine statik fabrika(factory) met...İbrahim Kürce
Normal şekilde, sınıfı kullanan kişi, o sınıftan bir örnek oluşturabilme iznini public(herkese açık) yapılandırıcı sayesinde elde eder. Her programcının bilmesi gereken başka bir teknik daha vardır. Bir sınıf, kendi örneğini döndüren statik fabrika metodu ile de örneğini oluşturabilir. Boolean(basit boolean tipinin kutulanmış sınıf hali) sınıfından basit bir örnek.
Sınıf ve nesne kullanımında bilinmesi gerekli temel kavramlar tanıtıldıktan sonra, Java tarafından desteklenen sınıflara örnek olarak karakter katarı sınıfları (String ve StringBuilder) gösteriliyor. Sunum Java'daki dizi desteğinin tanıtımıyla sonlanıyor.
NOT: Sunum içindeki bağlantılara tıklayarak daha ayrıntılı bilgilerin verildiği makalelere ulaşabilirsiniz.
2. Konular
● Reserve Edilmiş Kelimeler
● Package nedir? Ne işe yarar?
● Sınıflar(Class), Nesneler(Object) ve
Özellikler(Property)
● Constructor ve Methodlar
● Bellek Kullanımı (Heap ve Stack Alanlar)
● Access Modifiers (Erişim Değiştiricileri)
● Static ve Final Özelliği
● Encapsulation(Getter and Setter)
● Inheritance(Kalıtım)
● Polymorphism(Çok biçimcilik)
● Interface
● Abstraction(Soyutlama)
● Object Casting
3. Rezerve Edilmiş Kelimeler
Bu kelimeler Java kütüphanesindedir ve bir operasyonu gerçekleştirebilmek için
kullanılan kelimelerdir. Bu kelimeleri tanımlayıcı olarak kullanamayız.
4. Package nedir? Ne işe yarar?
Package: Anlamsal olarak aynı işi tanımlayan class gruplarını, interface gruplarını
veya alt package ları bir araya alıp kapsayan mekanizmadır. Örnek package ismi:
● Tanımlarken package com.sirketadi.projeadi;
● Kullanırken import java.util.*;
Neden package kullanmalıyız? Ne işe yarar?
● İsim karmaşasını önlemek için.
● Proje içerisinde aradığımız bir dosyayı(class, interface, enum vb.) rahatça bulup
erişebilmek için.
● Kontrollü erişimi sağlayabilmek için. Bu durum bir projede birden fazla package
varsa kullanılır.
5. Sınıflar ve Nesneler
Sınıf(Class): Nesne oluşturmak, veri tipi ve method tanımlamalarında
kullanılabilecek şablon veya kalıplardır.
● Sınıf isimleri singular(tekil) olmalıdır.
Nesne(Object): Mevcut veri içerisinde çalışan veri ve prosedürlerdir. Her nesne bir
duruma ve bir davranışa sahiptir.
● Bu durumlar properties(özellikler(değişkenler)) olarak mevcuttur.
● Davranışlar ise o nesneye ait methodlardır.
Not: Nesneler oluşturulurken new sözcüğü ile oluşturulur.
6. Constructor, Methodlar ve Property
● Constructor, bir class içerisinde default(varsayılan) olarak bulunan inşaatçı
methodlardır.
● Methodlar, bir class tan oluşan nesnenin davranışını belirten özellikleridir.
○ Geri dönüş tipleri olabilir(return statement(ifade)) veya olmayabilir(void statement(ifade)).
○ Methodlar tanımlamaları bir fiil(eylem) ile başlamalıdır ve sonuçta bir eylem belirtmelidir.
● Property (Özellik), primitive type (ilkel tip) veya herhangi bir Nesne(Object)
olabilirler. Bunun amacı sahip olduğumuz class’tan oluşacak nesne için özellik
tanımlamaktır.
○ Örnek property: private int color; private WheelModel wheel;
7. Bellek Kullanımı
Heap Memory: Java Runtime tarafından oluşturulan nesnelere ve JRE(Java Runtime
Environment) tarafından oluşturulan classlar hafızanın bu bölümünde tutulur.
Stack Memory: Method içerisinde tanımlanan değişkenler hafızanın bu bölümünde
tutulur. Ayrıca Heap Memory içerisinde bulunan nesnelerin referansları da bu
bölümde tutulur. Heap ile karşılaştırıldığında oldukça az bir boyutu vardır.
Buradaki önemli nokta ise Garbage Collector(Çöp Toplayıcı) adını verdiğimiz işlem
herhangi bir referansa ait olmayan nesneleri temizlemek için Heap Memory
üzerinde çalışır. Kısaca bize Runtime boyunca hafızada yer açar.
8. Heap Memory vs Stack Memory
● Heap Memory’deki değişkenler tüm uygulama tarafından ulaşılabilirken, Stack Memory’dekilere
sadece aynı Thread içerisinden ulaşılabilir.
● Stack Memory’ deki bellek yönetimi basit ve LIFO(Last In First Out) yani son giren ilk çıkar mantığı ile
çalışırken, Heap Memory’deki bellek yönetimi daha büyük ve karmaşık olduğu için içerisinde Garbage
Collector adını verdiğimiz bir işlem çalışır.
● Stack Memory kısa ömürlüdür, Heap Memory ise uygulamanın başından sonuna kadar tutulur.
● Stack Memory hem boyut olarak hem de bellek yönetimi olarak basit olduğundan Heap Memory’ e
göre daha hızlı hareket eder.(Değişkene erişim hızı olarak düşünebiliriz)
● Stack Memory dolarsa alınacak olan hata:
● Heap Memory dolarsa alınacak olan hata:
10. Access Modifiers
Bir classın, constructorın, değişkenin (variable), methodun veya bir datanın(verinin)
ulaşılabilecek kapsamını kısıtlamaya yardımcı olur.
Java’da kullanılan 4 tip access modifers mevcuttur.
1. Default - Herhangi bir kelime yok.
2. Private
3. Protected
4. Public
11. Access Modifiers Devam
default private protected public
Aynı Class Evet Evet Evet Evet
Aynı Package
SubClass
Evet Hayır Evet Evet
Aynı Package
SubClass Değil
Evet Hayır Evet Evet
Farklı Package
SubClass
Hayır Hayır Evet Evet
Farklı Package
SubClass Değil
Hayır Hayır Hayır Evet
Erişim durumları gösterilmiştir
12. Static Özelliği
Static:
● Class’tan nesnesi üretilmeden ulaşılabilen method ve değişkenler yapmaya
yarar.
● O class oluşan her bir nesne için sürekli aynı özelliği gösterir. Öğrenci class’ının
içinde “mevcut” isminde bir static property olsun ve constructor’da +1 artsın. Bu
bize her öğrenci oluşturduğumuzda toplamda öğrenci class’ımızdan kaç
öğrenci oluşturduğumuzu verecektir.
● Static değişkenler Runtime boyunca memory’de tutulur.
● Static class’lar Java’da ana class olarak tanımlanamazlar. Fakat ana class
içinde class tanımlanırsa o class static tanımlanabilir.
13. Final Özelliği
final kelimesinin Java’da 4 alanda kullanımı mevcuttur.
1. Class içindeki final değişkenleri:
a. Bu değişkenlere sadece bir kez atama yapılabilir ve Runtime’da değiştirilemez. Bu atama işlemi
o sınıf içinde yapılır. Örnek olarak Pİ sayısının değerini bilindiği için doğrudan atama yapılabilir.
2. Method’a gönderilen final parametresi:
a. Bunun amacı ise fonksiyona veya methoda gönderdiğimiz değişkenin değiştirilmemesini
istiyorsak kullanırız.
3. Final Methodlar:
a. Üst sınıftaki methodun ondan türeyen subclass’lar içerisinde override edilmesini engeller.
4. Final Class’lar:
a. Final tanımlı class’lardan yeni bir class türetilebilmesini engeller.
14. Encapsulation(Getter and Setter)
Encapsulation işlemi bir class’ımız içindeki değişkenlere diğer class’lar tarafından
doğrudan müdahale edilmesini engellemek için kullanılır.
Eğer bir property’e encapsulation uygulayacaksak onu private yapıp o değişkeni
kotrol eden public bir get ve bir set methodu tanımlamamız yeterli olacaktır.
Encapsulation ile yapılabilecekler ise:
● Data Hiding,
● Read Only, Write Only property
15. Inheritance(Kalıtım)
● extends kelimesi kullanılarak gerçekleştirilir.
● Bir class birden fazla class’tan türetilemez. Ancak B classı A’dan türesin ve C
class’ı da B class’ından türeyebilir. Bir nevi Dede(A)->Baba(B)->Oğul(C) gibi.
● Bir class’tan birden fazla class türetilebilir.
● Reusability’i kod içerisinde sağlar. Aynı değişkenler tanımlanmaz. Üst
sınıflardan aktarılır.
● OOP’nin mihenk taşlarından diyebiliriz.
16. Polymorphism(Çok Biçimcilik)
Polymorphism bir class’tan farklı biçimlerde nesneler üretebilmektir.
Java’da polymorphism 2 şekilde olabilir.
1. Overloading(Aşırı yükleme)
a. Compiletime Polymorphism olarak adlandırılır.
i. Sebebi daha program çalışmadan hangi methodun çağrılması gerektiği bilinir.
2. Overriding(Ağır basma veya ezme)
a. Runtime Polymorphism olarak adalandırılır.
i. Sebebi daha program çalışmadan hangi methodu çalıştıracağını bilmez. Bunun sebebi
interface kullanımından veya abstract methodlardan kaynaklanmaktadır.
17. Overloading Polymorphism
Bu yöntem bir class’a ait constructor sayısını artırarak gerçekleştirilir.
Buradaki önemli nokta bu constructor’lar farklı sayıda parametreye sahip olmalı
veya farklı tiplerde olmalıdır.
Siz aynı zamanda o class’a ait diğer method’lara da overload yapabilirsiniz.
18. Overriding Polymorphism
Bu yöntem inheritance özelliği kullanılarak yapılmaktadır.
Örnek verirsek: Shape(Şekil) Class’ınız olsun ve bundan türeyen bir Circle(Daire)
Class’ınız ve Square(Kare) Class’ınız olsun. Son olarak bu üç class’ınızın içerisinde
de draw() isminde bir methodunuz olsun. Amacımız bu methoda polymorphism
kullanarak ulaşmak.
19. Interface
Interface’lerde aynı classlar gibi değişkenlere ve methodlara sahip olabilirler.
Aradaki temel farklar:
● class sözcüğü yerine interface sözcüğü yazılarak kullanılır.
● Interface’ler isimlendirilirken genelde “I” harfi ilk harf olur.
● interface’ler içerisindeki methodların body denilen kısımları yazılmaz. Bu durum default
abstract(soyut) olmalarından gelir. Ayrıca public’tirler.
● değişkenler initialize edilmelidir. Bu durum interface içerisinde kullanılan değişkenlerin
default final olarak tanımlanmasından gelir.
● Tanımlanan değişkenler default olarak public, static ve final türündedir.
● Java 8’ den beri interface’lerde static ve default anahtar sözcükleri ile kullanılan
methodların body’si olmalıdır.
● Interface’lerde final anahtar sözcüğü kullanılmaz.
20. Interface Devam
Java’da Interface ile ilgili diğer bilmemiz gerekenler ise şunlardır:
● implements anahtar sözcüğü ile bir class’a eklenirler.
● Interface’ler new anahtar sözcüğü ile kullanılırsa anonymous bir class’ a
implemente olurlar.
● Interface’ler class’lara implement olup o class’ın sözleşmesi niteliği taşırlar.
● Bir class bir interface’i implement eder ise o interface’e dair tüm method’ları
implement etmek zorundadır.
● Interface’ler kendi aralarında inheritance’ daki kurallara uyarak kalıtım
gerçekleştirebilirler.
● Bir class birden fazla interface’i implemente edebilir.
● Polymorphism interface’ler kullanılarak ta yapılabilmektedir.
21. Abstraction(Soyutlama)
abstract anahtar kelimesi ile oluşturulur.
İsminden anlaşılacağı üzere amaç class içerisindeki methodları class dışı çağrılırken
soyutlayabilmektir. Bu olayı şu şekilde özetleyebiliriz.
Bir arabanız var gaz ve fren pedalı var. Siz sadece o ikisini bilirsiniz. Fakat araba siz
o ikisinden birine basınca ne yapacağını bilir. Hızı artırır veya azaltır.
● Abstract ile yapılabilecek tüm senaryoları interface kullanarak ta yapabilirsiniz.
● Abstract kullanmak kodun complexity(karmaşıklık)’sini artıracaktır.
● Eğer bir class abstract class ise içerisinde abstract method tanımı yapılabilir ancak body’ si olamaz.
● Abstract class’lar implements değil extends edilirler.
● Ana class’ta abstract bir method var ise kalıtım alan sınıf bu methodu override etmek zorundadır.
● Abstract class’lar kullanılarak ta polymorphism yapılabilir
22. Object Casting
Java’da casting işlemi cast yapılmak istene objenin class isminin parantezler
içerisine yazılıp atanması ile gerçekleşir.
Java’da aralarında inheritance ilişkisi bulunan classlardan oluşan nesneler
birbirlerine upcasting veya downcasting yapabilirler.
Bu duruma örnek vermek gerekirse Hayvan isimli bir sınıfımız var ve bundan kalıtım
alan Kedi isimli bir sınıfımız olsun. Oluşturulan Hayvan nesnesine doğrudan Kedi
nesnesi herhangi bir cast işlemi yapılmadan ataması yapılabilir. Bu duruma
upcasting denir.
23. Object Casting Devam
Yine bir önceki örnekten gidelim. Bu seferde Hayvan sınıfına ait bir nesneye yeni bir
Kedi oluşturup atayalım. Ardından Kedi sınıfımızın içindeki miyav() isimli methodu
çağırmaya çalışalım. miyav() methodunu çağırabilmemiz için Hayvan nesnemizi Cat
nesnemize downcasting yapıp çağırabiliriz.