Java'da Soyut Sınıf ve Soyut Yöntem nedir? Ne zaman kullanmalıyım? Eğitici Ekli
Yayınlanan: 2019-09-12
Birkaç gün önce Java'da Arayüz Nedir ve Nasıl Kullanıldığına dair temel Java Temelleri üzerine bir makale yazdım. Bu eğitim aynı zamanda temel Java temelli " Abstract Class and Abstract Method
" ile de ilgilidir.
Soyut Sınıf nedir?
Önce Soyut sınıfını anlamaya başlayalım, sonra Örnek üzerinden geçeceğiz.
- Soyut bir sınıf,
abstract
olarak bildirilen bir sınıftır. - Soyut sınıflar başlatılamaz
- Soyut sınıflar alt sınıflara ayrılabilir
- Soyut yöntemler içerebilir veya içermeyebilir
- Soyut bir sınıf
subclassed
, alt sınıf genellikle üst sınıfındaki tüm soyut yöntemler için uygulamalar sağlar. - Alt sınıf uygulamalar sağlamıyorsa, alt sınıf da
abstract
olarak bildirilmelidir.
Bu çok basic Java Interview Question
. Muhtemelen mülakat sırasında aldığınız 1 st Java Interview Question
.
Soyut bir yöntem eklemeden soyut bir sınıf tanımlayabilir miyim?
Tabii ki evet. Bir sınıf özeti bildirmek, yalnızca onun kendi başına somutlaştırılmasına izin vermediğiniz anlamına gelir. Soyut olmayan bir sınıfta soyut bir yönteminiz olamaz.
Soyut Yöntem Nedir?
-
abstract method
, bir uygulama olmadan bildirilen bir yöntemdir. - Sadece bir yöntem imzası var.
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 |
package crunchify . com . tutorial ; /** * @author Crunchify.com * Simple Abstract Class and Method Example with live result * */ public abstract class CrunchifyExam { public enum ExamStatus { PASSED , FAILED } private String examTime ; private ExamStatus status ; public CrunchifyExam ( String examTime , ExamStatus status ) { this . examTime = examTime ; this . status = status ; } public String getExamTime ( ) { return examTime ; } public void setExamTime ( String examTime ) { this . examTime = examTime ; } public void setExamStatus ( ExamStatus status ) { this . status = status ; } public ExamStatus getExamStatus ( ) { return status ; } abstract public void checkResult ( ) ; } |
Bir Örnekle başlayalım. Sorun Açıklaması:
-
checkResult()
adlı bir soyut yöntemi olanCrunchifyExam.java
sınıfını oluşturun - Soyut sınıf
Crunchify1stSchoolExamResult.java
genişleten Crunchify1stSchoolExamResult.java sınıfınıCrunchifyExam.java
- Soyut sınıf
Crunchify2ndSchoolExamResult.java
genişleten Crunchify2ndSchoolExamResult.java sınıfınıCrunchifyExam.java
- Şimdi yukarıdaki her iki sınıf da checkResult() yöntemi için uygulama sağlamalıdır.
- Her iki Okulun da kullanıcının PASSED
PASSED or FAILED
olup olmadığını öğrenmek için kendi farklıprocedure
veyanumber of checks
olabilir, kendicheckResult()
uygulamalarını kullanmakta özgürdürler.
CrunchifyExam
sınıfını genişlettiğinizde – Eclipse, Crunchify1stSchoolExamResult.java
belirtilen Soyut yöntemleri uygulamanızı CrunchifyExam.java
.

Crunchify1 . SchoolExamResult.java
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 |
package crunchify . com . tutorial ; /** * @author Crunchify.com */ public class Crunchify1stSchoolExamResult extends CrunchifyExam { public Crunchify1stSchoolExamResult ( String examTime , ExamStatus status ) { super ( examTime , status ) ; // TODO Auto-generated constructor stub } @Override public void checkResult ( ) { String studentName = "Crunchify1" ; String studentResult = "85%" ; // School NO-1 will provide all their formula to find if user is passed or failed. // After detailed calculation let's say student's grade is "PASSED". System . out . println ( "Hey.. this is user " + studentName + " with grade " + studentResult + " - " + getExamStatus ( ) + ", ExamTime: " + getExamTime ( ) ) ; } } |
Crunchify2 ve SchoolExamResult.java
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 |
package crunchify . com . tutorial ; import java . text . DateFormat ; import java . text . SimpleDateFormat ; import java . util . Date ; /** * @author Crunchify.com */ public class Crunchify2ndSchoolExamResult extends CrunchifyExam { public Crunchify2ndSchoolExamResult ( String examTime , ExamStatus status ) { super ( examTime , status ) ; } @Override public void checkResult ( ) { String studentName = "Crunchify2" ; String studentResult = "45%" ; // School NO-2 will provide all their formula to find if user is passed or failed. // After detailed calculation let's say student's grade is "FAILED". log ( "Hey.. this is user " + studentName + " with grade " + studentResult + " - " + getExamStatus ( ) + ", ExamTime: " + getExamTime ( ) ) ; } public static void main ( String args [ ] ) { DateFormat dateFormat = new SimpleDateFormat ( "yyyy/MM/dd HH:mm:ss" ) ; // 1st School's checkResult() Date date = new Date ( ) ; String examTime = dateFormat . format ( date ) ; log ( "Initializing 1st School object at time " + examTime ) ; // We are setting up time and Result for 1st School Crunchify1stSchoolExamResult object = new Crunchify1stSchoolExamResult ( examTime , ExamStatus . PASSED ) ; object . checkResult ( ) ; // Let's wait 5 seconds wait to see time difference in console log try { Thread . sleep ( 5000 ) ; } catch ( InterruptedException e ) { e . printStackTrace ( ) ; } // 2nd School's checkResult() date = new Date ( ) ; examTime = dateFormat . format ( date ) ; log ( "\nInitializing 2nd School object at time " + examTime ) ; // We are setting up time and Result for 2nd School Crunchify2ndSchoolExamResult object2 = new Crunchify2ndSchoolExamResult ( examTime , ExamStatus . FAILED ) ; object2 . checkResult ( ) ; } // Simple log method private static void log ( String value ) { System . out . println ( value ) ; } } |
Eclipse Konsolu Sonucu:
Aşağıdaki sonucu görmek için Crunchify2ndSchoolExamResult.java
sağ tıklayın ve Java Application
olarak çalıştırın.

1 2 3 4 5 |
Initializing 1st School object at time 2016 / 11 / 30 14 : 24 : 37 Hey . . this is user Crunchify1 with grade 85 % - PASSED ExamTime : 2016 / 11 / 30 14 : 24 : 37 Initializing 2nd School object at time 2016 / 11 / 30 14 : 24 : 42 Hey . . this is user Crunchify2 with grade 45 % - FAILED ExamTime : 2016 / 11 / 30 14 : 24 : 42 |
Şimdi bir sorunuz olabilir
Neden Abstract Method
ve Class
yerine Arayüz kullanamıyorum ve Arayüz olarak CrunchifyExam'e sahip olamıyorum?
Pekala – Sure you could
– ancak getExamTime
(), setExamTime
(), getExamTime
(), setExamTime
() yöntemlerini de uygulamanız gerekir.
Soyut sınıfları kullanarak, diğer (soyut olmayan) yöntemlerin uygulamasını devralabilirsiniz. Bunu arayüzlerle yapamazsınız – bir arayüz herhangi bir yöntem uygulaması cannot provide
.
Java'da soyut ve son sınıf oluşturmak mümkün müdür?
- Soyut ve nihai, birbirini dışlayan kavramlardır.
- Yani cevap HAYIR, Java'da soyut bir sınıf veya yöntem finali yapamayız.
- Son sınıf tam sınıftır ve daha fazla uzatılamaz.
- Soyut sınıfa eksik sınıf denir ve yalnızca diğer somut sınıflar tarafından genişletilebilir ve tüm soyut yöntemleri uygulamanız gerekir.
- Ayrıca, bir final sınıfında soyut bir yönteme sahip olmanın yolu yoktur ve mümkün değildir.
Java'da birden çok soyut sınıftan miras almak mümkün müdür?
- Java, çoklu kalıtımı desteklemez.
- Java'da yalnızca tek bir sınıfı genişletebiliriz.
- Java'da birçok kaynaktan Arayüzler uygulayabiliriz.