Bölüm 9 Nesneye-Dayalı Programlama: Miras (Inheritance)

Benzer belgeler
Bölüm 10 Polimorfizm

Nesne tabanlı programlama nesneleri kullanan programlamayı içerir. Bir nesne farklı olarak tanımlanabilen gerçek dünyadaki bir varlıktır.

/*Aşağıda ki kodları doğru şekilde anlar ve kullanırsanız java da sınıfları biraz da olsa anlamış olursunuz.*/

Inheritance. Inheritance (turetim)

Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü

Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü

Sınıflar ve Yapılar Arasındaki Farklılıklar. Değer ve Referans Türde Olan Aktarımlar

Arayüz soyut metotların oluşturduğu bir koleksyondur. Bir sınıf arayüzü çalıştırırken arayüzün sahip olduğu soyut metotları da miras alır.

Java da Soyutlama ( Abstraction ) ve Çok-biçimlilik ( Polymorphism )

BMH-303 Nesneye Yönelik Programlama

MOBIL UYGULAMA GELIŞTIRME

Kalıtım (Inheritance)

Nesne Yönelimli Programlama

BMH-303 Nesneye Yönelik Programlama

Java Class Yapısında Finalize Metotunun Kullanımı

Sınıf üyeleri: değişkenler (fields) and metotlar (methods) Değişkenler: nesne içerisindeki değişkenlerdir Temel veri tipi veya sınıf tipi

7. HAFTA. Erişim Belirleyiciler

NESNE YÖNELİMLİ PROGRAMLAMA HAFTA # 9

Sınıf üyeleri: değişkenler (fields) and metotlar (methods) Değişkenler: nesne içerisindeki değişkenlerdir Temel veri tipi veya sınıf tipi

İçerik. Kapsülleme ( Encapsulation ) Java da Kalıtım: Örnek 2.1. Kalıtım ( Inheritance ) Tekrar Ziyaret. Java da Kalıtım: Örnek 2.2.

Kapsülleme ( Encapsulation ) BBS-515 Nesneye Yönelik Programlama

İki Yöntem. Komposizyon (Composition) Kalıtım (Inheritance)

Sınıfların Tekrardan Kullanılması. Altuğ B. Altıntaş 2003 Java ve Yazılım Tasarımı - Bölüm 5 1

Lab7 DOĞU AKDENİZ ÜNİVERSİTESİ BİLGİSAYAR VE TEKNOLOJİ YÜKSEKOKULU BİLGİSAYAR PROGRAMCILIĞI. BTEP212 Java. Uygulama1: package javaapplication58;

NESNEYE DAYALI PROGRAMLAMA 9. HAFTA UYGULAMA DERSİ

NESNE TABANLI PROGRAMLAMA-1 DERS UYGULAMALARI (22 EYLÜL - 14 KASIM

İçerik. Temel Kavramlar. Nesne Nedir? 1. Nesne : Örnek. Nesne Nedir? 2. Geçen hafta: Bu hafta: BBS-515 Nesneye Yönelik Programlama

Temel Kavramlar BBS-515 Nesneye Yönelik Programlama

Görsel Programlama DERS 02. Görsel Programlama - Ders02/ 1

Uzaktan Eğitim Uygulama ve Araştırma Merkezi

BİL-141 Bilgisayar Programlama I (Java)

Java C.Thomas Wu 2004b kitabından Türkçeleştirilerek ve örneklendirilerek hazırlanmıştır.

Bölüm 8 Nesne-Tabanlı Programlama

Arayüz (Interface) Altuğ B. Altıntaş 2003 Java ve Yazılım Tasarımı - Bölüm 7 1

Bölüm 12. Nesne yönelimli programlama desteği

BMM3101 PROGRAMLAMA DİLLERİ

Yazılım Kodlama ve İ simlendirme Standartları v1.0

BTEP243 Ders 3. class Yazım Kuralı:

NESNE YÖNELİMLİ PROGRAMLAMA HAFTA # 7

BİL132 Bilgisayar Programlama II

C++ Dersi: Nesne Tabanlı Programlama

Java ile Nesneye Yönelik Programlama (Object Oriented Programming)

abstract Sınıflar 1 Sınıf sınıf1 new class Ama aşağıdaki şekilde referans alınabilir;

Java dili, aşağıdakiler de dahil olmak üzere çok çeşitli denetleyici türlerine sahiptir.

Kalıtım ( Inheritance ) BBS-515 Nesneye Yönelik Programlama

NESNE YÖNELİMLİ PROGRAMLAMA HAFTA # 4. Yrd.Doç.Dr.Hacer Karacan

Başlangıç Durumuna Getirme ve Temizlik. Altuğ B. Altıntaş 2003 Java ve Yazılım Tasarımı - Bölüm 3 1

NESNE YÖNELİMLİ PROGRAMLAMA HAFTA # 6

MAT214 BİLGİSAYAR PROGRAMLAMA II DERSİ Ders 11: Sınıf (Class) Yapıları (Örnekler)

BMÜ-112 ALGORİTMA VE PROGRAMLAMA-II LABORATUARI DENEY-2 FÖYÜ

MAT214 BİLGİSAYAR PROGRAMLAMA II DERSİ Ders 12: Grafik Kullanıcı Arayüzü (Graphical User Interface-GUI)

Bu parçalar arasındaki ilişkiyi kullanıp büyük çaplı programlar geliştirme tekniğine de nesne yönelimli programlama denir.

PAKET ERİŞİMLERİ SINIFLARIN YENİDEN KULLANIMI. BMU-112 Algoritma ve Programlama-II Yrd. Doç.Dr. İlhan AYDIN

Görsel Programlama DERS 03. Görsel Programlama - Ders03/ 1

Maltepe Üniversitesi Bilgisayar Mühendisliği Bölümü BİL 203 Veri Yapıları ve Algoritmalar I

BİL-142 Bilgisayar Programlama II

public static int Toplam int x, int y

Veri Yapıları ve Algoritmalar dönem

C++ Class larina baslangic. C++ Versus C Object-oriented Language C++ Structure dan Object Create etmek. Structure tanimlama.

1. Aşağıdaki program parçacığını çalıştırdığınızda result ve param değişkenlerinin aldığı en son değerleri ve programın çıktısını yazınız.

NESNE YÖNELİMLİ PROGRAMLAMA HAFTA # 3

Object-Oriented Programming Laboratuvar 11

3. Bölüm Soyut Sınıflar (Abstract Classes) Java ile Nesne Merkezli ve Fonksiyonel Programlama Akın Kaldıroğlu

Java da, tüm değişkenlerin kullanılmadan önce tanımlanması edilmesi gerekir. Bir değişken tanımlamanın temel gösterimi bu şekildedir:

Operator Aşırı Yükleme (Operator OverLoading)

Java Nitelemleri. Bölüm 1

Nesneye Dayalı Programlama

#ifndef COMPLEX_H #define COMPLEX_H

Nesne Yönelimli Programlama

Nesneye Dayalı Programlama

YZM 2105 Nesneye Yönelik Programlama

MAT214 BİLGİSAYAR PROGRAMLAMA II DERSİ Ders 8: Sınıf (Class) Yapılarına Giriş

Erişim konusunda iki taraf vardır:

Chain of Responsibility Tasarım Şablonu KurumsalJava.com

DATA STRUCTURES. Part III. Sınıflar, Nesneler ve İlgili Programlama Örnekleri

İNÖNÜ ÜNİVERSİTESİ MÜH. FAK. BİLGİSAYAR MÜH. BÖL. ALGORİTMA VE PROGRAMLAMA 2 DERSİ LAB. ÖDEVİ

SINIFLARIN TEKRAR KULLANILMASI

Java Programlama Dilinde Veri Tipleri. Yrd. Doç. Dr. Aybars UĞUR

Metodlar. C# İçindeki Program Modülleri

ANA SINIF TÜRETİLEN BİRİNCİ SINIF TÜRETİLEN İKİNCİ SINIF

Dizi ( Array ) ve Dizgi ( String )

İlgili Konular: Object.equals(java.lang.Object), Object.hashCode(), rehash(), Collection, Map, HashMap, TreeMap, Serialized Form

Paket Erişimleri. Altuğ B. Altıntaş 2003 Java ve Yazılım Tasarımı - Bölüm 4 1

Spring Framework Eğitimi

Object-Oriented Programming Lab Shape, TwoDShape, ThreeDShape, Disk, Square, Sphere, Cube class hiyerarşisi.

Bölüm 11. Soyut veri tipleri ve kapsülleme kavramları ISBN

Upgrading Internet Technology skills of Information and Communication Technologies (ICT) Professionals

Facade (Cephe) Tasarım Şablonu KurumsalJava.com

Sunum İçeriği. Programlamaya Giriş

BMÜ-111 ALGORİTMA VE PROGRAMLAMA AKIŞ KONTROLÜ YRD. DOÇ. DR. İLHAN AYDIN

HSancak Nesne Tabanlı Programlama I Ders Notları

Kılgıladığı arayüzler: Serializable, Cloneable, Map<K,V>, NavigableMap<K,V>, SortedMap<K,V>

Class LinkedHashMap. Class LinkedHashMap java.lang.object java.util.abstractmap java.util.hashmap java.util.linkedhashmap. Kılgıladığı arayüzler:

Java da İstemci Tarafı Uygulamalar

MAT214 BİLGİSAYAR PROGRAMLAMA II DERSİ Ders 18: Formatlı Grafik Kullanıcı Arayüzü Oluşturma

Görsel Programlama (Visual Programming) 2.Hafta

İsimler ve Kapsam. 24 Şubat Programlama Dilleri - Pamukkale Üniversitesi 1

Transkript:

Bölüm 9 Nesneye-Dayalı Programlama: Miras (Inheritance) Outline 9.1 Giriş 9.2 Super sınıflar ve Alt sınıflar 9.3 protected Üyeler 9.4 Süper sınıflar ile Alt sınıflar arasındaki ilişki 9.5 Örnek Çalışma: Üç düzey Miras Hiyerarşisi 9.6 Alt sınıflarda Yapılandırıcılar ve Sonlardırıcılar 9.7 Yazılım Mühendisliği ve Miras

9.1 Giriş Miras Yazılımı yeniden kullanabilirliği artırır. Varolan sınıflardan yeni sınıflar türetmeyi sağlar. Varolan sınıfın veri ve metodlarını kendi bünyesine alır. Üzerine yeni veri ve metodlar geliştirir. Altsınıf (subclass) süper sınıftan miras alır. Altsınıf Daha özellikli nesneler oluşturur. Süper sınıftan metodlar miras alınır Ve de modife edilebilir. Ek metodlar eklenebilir.

9.1 Giriş Sınıf hiyerarşisi Doğrudan süper sınıf Açık miras (tek seviyeli hiyerarşi) Dolaylı yoldan süper sınıf İki yada daha fazla seviyeden hiyerarşi Tekli miras Sadece bir süper sınıftan miras alınma Çoklu miras Birden fazla süper sınıftan miras alma Java çoklu mirası desteklemez

is-a vs. has-a is-a Miras 9.1 Giriş Altsınıf nesnesi süper sınıf nesnesi gibi davranır. Örnek: Car is a vehicle (Araba bir araçtır) has-a Aracın veri ve metodaları aynı zamanda arabaya da geçmiştir. Komposizyon Bir nesne başka sınıflardan oluşmuş nesneleri kendisinde barındırabilir. Örnek: Car has a steering wheel (Araba tekeri vardır.)

9.2 Süper Sınıflar ve Alt Sınıflar Süper sınıflar ve Alt sınıflar Bir sınıfın nesnesi diğer sınıfında nesnesidir. Örnek: Dikdörtgen bir dörtgendir. sınıfı sınıfından miras almıştır. Dörtgen: süper sınıf Dikdörtgen : alt sınıf Süper sınıfclass alt sınıftan daha fazla nesneyi kapsar. Örnek: Süper sınıf: Araç Arabalar, traktörler, botlar, bisikletler, Alt sınıf: Araba Daha küçük, daha fazla alt özeklliğe sahip araçlar...

9.2 Süper Sınıflar ve Alt Sınıflar(devam) Miras örnekleri Süper sınıf Öğrenci Şekil Loan Alt sınıflar YüksekLisansÖğrencisi, LisansÖğrencisi Çember, Üçgen, Dikdörtgen CarLoan, HomeImprovementLoan, MortgageLoan Faculty, Staff Employee BankAccount CheckingAccount, SavingsAccount Fig. 9.1 Inheritance examples.

9.2 Süper Sınıflar ve Alt Sınıflar(devam) Miras hiyerarşisi Miras ilişkileri: ağaç hiyerarşisine benzer Her bir sınıf Süper sınıf Başka sınıflara veri/metodlar sağlar veya Alt sınıf Başka sınıflardan veri/metodlar alır.

CommunityMember Employee Student Alumnus Faculty Staff Administrator Teacher Fig. 9.2 Inheritance hierarchy for university CommunityMembers.

Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Tetrahedron Fig. 9.3 Inheritance hierarchy for Shapes.

9.3 protected Üyeler protected erişim alanı public and private arasında bir erişim düzeyi vardır. protected üyeleri Süper sınıf üyelerine Alt sınıf üyelerine Aynı paket içindeki üyelere erişebilirler. Altsınıf süper sınıf üyesine erişebilir Anahtar kelime super ve nokta (.)

9.4 Süper Sınıflar ve Alt Sınıflar arasındaki İlişki Süper sınıf ve alt sınıf arasındaki ilişki Örnek: Nokta/çember miras hiyerarşisi Nokta (Point) x-y koordinat çifti Çember (Circle) x-y koordinat çifti Yarıçap

1 // Fig. 9.4: Point.java 2 // Point class declaration represents an x-y coordinate pair. 3 4 public class Point { 5 private int x; // x part of coordinate pair 6 private int y; // y part of coordinate pair 7 8 // no-argument constructor 9 public Point() 10 { 11 // implicit call to Object constructor occurs here 12 } 13 14 // constructor 15 public Point( int xvalue, int yvalue ) 16 { 17 // implicit call to Object constructor occurs here 18 x = xvalue; // no need for validation 19 y = yvalue; // no need for validation 20 } 21 22 // set x in coordinate pair 23 public void setx( int xvalue ) 24 { 25 x = xvalue; // no need for validation 26 } 27 Maintain x- and y- coordinates as private instance variables. Implicit call to Object constructor Point.java Lines 5-6 Maintain x- and y- coordinates as private instance variables. Line 11 Implicit call to Object constructor

28 // return x from coordinate pair 29 public int getx() 30 { 31 return x; 32 } 33 34 // set y in coordinate pair 35 public void sety( int yvalue ) 36 { 37 y = yvalue; // no need for validation 38 } 39 40 // return y from coordinate pair 41 public int gety() 42 { 43 return y; 44 } 45 46 // return String representation of Point object 47 public String tostring() 48 { 49 return "[" + x + ", " + y + "]"; 50 } 51 52 } // end class Point Override method tostring of class Object Point.java Lines 47-50 Override method tostring of class Object.

1 // Fig. 9.5: PointTest.java 2 // Testing class Point. 3 import javax.swing.joptionpane; 4 5 public class PointTest { 6 7 public static void main( String[] args ) 8 { 9 Point point = new Point( 72, 115 ); // create Point object 10 11 // get point coordinates 12 String output = "X coordinate is " + point.getx() + 13 "\ny coordinate is " + point.gety(); and y- coordinates 14 15 point.setx( 10 ); // set x-coordinate 16 point.sety( 20 ); // set y-coordinate 17 18 // get String representation of new point value 19 output += "\n\nthe new location of point is " + point; Change the value of point s x- 20 21 JOptionPane.showMessageDialog( null, output ); // display output 22 23 System.exit( 0 ); 24 25 } // end main 26 27 } // end class PointTest PointTest.java Instantiate Point object Line 9 Instantiate Point object Lines 15-16 Change the value of point s x- and y- Implicitly coordinates call point s tostring method Line 19 Implicitly call point s tostring method

1 // Fig. 9.6: Circle.java 2 // Circle class contains x-y coordinate pair and radius. 3 4 public class Circle { 5 private int x; // x-coordinate radiusof as Circle's privatecenter 6 private int y; // y-coordinate instance of variables. Circle's center 7 private double radius; // Circle's radius 8 9 // no-argument constructor 10 public Circle() 11 { 12 // implicit call to Object constructor occurs here 13 } 14 15 // constructor 16 public Circle( int xvalue, int yvalue, double radiusvalue ) 17 { 18 // implicit call to Object constructor occurs here 19 x = xvalue; // no need for validation 20 y = yvalue; // no need for validation 21 setradius( radiusvalue ); 22 } 23 24 // set x in coordinate pair 25 public void setx( int xvalue ) 26 { 27 x = xvalue; // no need for validation 28 } 29 Maintain x-y coordinates and Note code similar to Point code. Circle.java Lines 5-7 Maintain x- and y- coordinates and radius as private instance variables. Lines 25-28 Note code similar to Point code.

30 // return x from coordinate pair 31 public int getx() 32 { 33 return x; 34 } 35 36 // set y in coordinate pair 37 public void sety( int yvalue ) 38 { 39 y = yvalue; // no need for validation 40 } 41 42 // return y from coordinate pair 43 public int gety() 44 { 45 return y; 46 } 47 48 // set radius 49 public void setradius( double radiusvalue ) 50 { 51 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); 52 } 53 54 // return radius 55 public double getradius() 56 { 57 return radius; 58 } 59 Note code similar to Point code. Ensure non-negative value for radius. Circle.java Lines 31-47 Note code similar to Point code. Line 51 Ensure non-negative value for radius.

60 // calculate and return diameter 61 public double getdiameter() 62 { 63 return 2 * radius; 64 } 65 66 // calculate and return circumference 67 public double getcircumference() 68 { 69 return Math.PI * getdiameter(); 70 } 71 72 // calculate and return area 73 public double getarea() 74 { 75 return Math.PI * radius * radius; 76 } 77 78 // return String representation of Circle object 79 public String tostring() 80 { 81 return "Center = [" + x + ", " + y + "]; Radius = " + radius; 82 } 83 84 } // end class Circle Circle.java

1 // Fig. 9.7: CircleTest.java 2 // Testing class Circle. 3 import java.text.decimalformat; 4 import javax.swing.joptionpane; 5 6 public class CircleTest { 7 8 public static void main( String[] args ) 9 { 10 Circle circle = new Circle( 37, 43, 2.5 ); // create Circle object 11 12 // get Circle's initial x-y coordinates and radius 13 String output = "X coordinate is " + circle.getx() + 14 "\ny coordinate is " + circle.gety() + 15 "\nradius is " + circle.getradius(); 16 17 circle.setx( 35 ); // set new x-coordinate 18 circle.sety( 20 ); // set new y-coordinate 19 circle.setradius( 4.25 ); // set new radius 20 Explicitly call circle s 21 // get String representation of new tostring circle value method 22 output += "\n\nthe new location and radius of circle are\n" + 23 circle.tostring(); 24 25 // format floating-point values with 2 digits of precision 26 DecimalFormat twodigits = new DecimalFormat( "0.00" ); 27 Create Circle object. Use set methods to modify private instance variable. CircleTest.java Line 10 Create Circle object Lines 17-19 Use set methods to modify private instance variable Line 23 Explicitly call circle s tostring method

28 // get Circle's diameter 29 output += "\ndiameter is " + 30 twodigits.format( circle.getdiameter() ); 31 32 // get Circle's circumference 33 output += "\ncircumference is " + 34 twodigits.format( circle.getcircumference() ); 35 36 // get Circle's area 37 output += "\narea is " + twodigits.format( circle.getarea() ); 38 39 JOptionPane.showMessageDialog( null, output ); // display output 40 41 System.exit( 0 ); 42 43 } // end main 44 45 } // end class CircleTest CircleTest.java Use get methods to obtain Lines circle s 29-37 diameter, circumference and area. Use get methods to obtain circle s diameter, circumference and area.

1 // Fig. 9.8: Circle2.java 2 // Circle2 class inherits from Point. 3 4 public class Circle2 extends Point { 5 private double radius; // Circle2's radius 6 7 // no-argument constructor 8 public Circle2() 9 { 10 // implicit call to Point constructor occurs here 11 } 12 Class Circle2 extends class Point. Maintain private instance variable radius. 13 // constructor 14 public Circle2( int xvalue, int yvalue, double radiusvalue ) 15 { 16 // implicit call to Point constructor occurs here 17 x = xvalue; // not allowed: x private errors. in Point 18 y = yvalue; // not allowed: y private in Point 19 setradius( radiusvalue ); 20 } 21 22 // set radius 23 public void setradius( double radiusvalue ) 24 { 25 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); 26 } 27 Attempting to access superclass Point s private instance variables x and y results in syntax Circle2.java Line 4 Class Circle2 extends class Point. Line 5 Maintain private instance variable radius. Lines 17-18 Attempting to access superclass Point s private instance variables x and y results in syntax errors.

34 // calculate and return diameter 35 public double getdiameter() 36 { 37 return 2 * radius; 38 } 39 40 // calculate and return circumference 41 public double getcircumference() 42 { 43 return Math.PI * getdiameter(); 44 } 45 46 // calculate and return area 47 public double getarea() 48 { 49 return Math.PI * radius * radius; 50 } 51 52 // return String representation of Circle object 53 public String tostring() 54 { Attempting to access superclass Point s private instance variables x and y results in syntax errors. 55 // use of x and y not allowed: x and y private in Point 56 return "Center = [" + x + ", " + y + "]; Radius = " + radius; 57 } 58 59 } // end class Circle2 Circle2.java Line 56 Attempting to access superclass Point s private instance variables x and y results in syntax errors.

Circle2.java:17: x has private access in Point x = xvalue; // not allowed: x private in Point ^ Circle2.java:18: y has private access in Point y = yvalue; // not allowed: y private in Point ^ Circle2.java:56: x has private access in Point return "Center = [" + x + ", " + y + "]; Radius = " + radius; ^ Circle2.java:56: y has private access in Point return "Center = [" + x + ", " + y + "]; Radius = " + radius; ^ 4 errors Circle2.java output Attempting to access superclass Point s private instance variables x and y results in syntax errors. Attempting to access superclass Point s private instance variables x and y results in syntax errors.

1 // Fig. 9.9: Point2.java 2 // Point2 class declaration represents an x-y coordinate pair. 3 4 public class Point2 { 5 protected int x; // x part of coordinate pair 6 protected int y; // y part of coordinate pair 7 8 // no-argument constructor 9 public Point2() 10 { 11 // implicit call to Object constructor occurs here 12 } 13 14 // constructor 15 public Point2( int xvalue, int yvalue ) 16 { 17 // implicit call to Object constructor occurs here 18 x = xvalue; // no need for validation 19 y = yvalue; // no need for validation 20 } 21 22 // set x in coordinate pair 23 public void setx( int xvalue ) 24 { 25 x = xvalue; // no need for validation 26 } 27 Maintain x- and y- coordinates as protected instance variables, accessible to subclasses. Point2.java Lines 5-6 Maintain x- and y- coordinates as protected instance variables, accessible to subclasses.

28 // return x from coordinate pair 29 public int getx() 30 { 31 return x; 32 } 33 34 // set y in coordinate pair 35 public void sety( int yvalue ) 36 { 37 y = yvalue; // no need for validation 38 } 39 40 // return y from coordinate pair 41 public int gety() 42 { 43 return y; 44 } 45 46 // return String representation of Point2 object 47 public String tostring() 48 { 49 return "[" + x + ", " + y + "]"; 50 } 51 52 } // end class Point2 Point2.java

1 // Fig. 9.10: Circle3.java 2 // Circle3 class inherits from Point2 and has access to Point2 3 // protected members x and y. 4 5 public class Circle3 extends Point2 { 6 private double radius; // Circle3's radius 7 8 // no-argument constructor 9 public Circle3() 10 { 11 // implicit call to Point2 constructor occurs here 12 } 13 14 // constructor 15 public Circle3( int xvalue, int yvalue, double radiusvalue ) 16 { variables x and y, declared 17 // implicit call to Point2 constructor occurs here protected in superclass 18 x = xvalue; // no need for validation Point2. 19 y = yvalue; // no need for validation 20 setradius( radiusvalue ); 21 } 22 23 // set radius 24 public void setradius( double radiusvalue ) 25 { 26 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); 27 } 28 Class Circle3 inherits from Maintain class Point2. private instance variables radius. Implicitly calls superclass s default constructor. Modify inherited instance Circle3.java Line 5 Class Circle3 inherits from class Point2. Line 6 Maintain private instance variables radius. Lines 11 and 17 Implicitly call superclass s default constructor. Lines 18-19 Modify inherited instance variables x and y, declared protected in superclass Point2.

29 // return radius 30 public double getradius() 31 { 32 return radius; 33 } 34 35 // calculate and return diameter 36 public double getdiameter() 37 { 38 return 2 * radius; 39 } 40 41 // calculate and return circumference 42 public double getcircumference() 43 { 44 return Math.PI * getdiameter(); 45 } 46 47 // calculate and return area 48 public double getarea() 49 { 50 return Math.PI * radius * radius; 51 } 52 53 // return String representation of Circle3 object 54 public String tostring() 55 { Access inherited instance variables x and y, declared protected in superclass Point2. 56 return "Center = [" + x + ", " + y + "]; Radius = " + radius; 57 } 58 59 } // end class Circle3 Circle3.java Line 56 Access inherited instance variables x and y, declared protected in superclass Point2.

1 // Fig. 9.11: CircleTest3.java 2 // Testing class Circle3. 3 import java.text.decimalformat; 4 import javax.swing.joptionpane; 5 6 public class CircleTest3 { 7 8 public static void main( String[] args ) 9 { 10 // instantiate Circle object 11 Circle3 circle = new Circle3( 37, 43, 2.5 ); 12 13 // get Circle3's initial x-y coordinates and radius 14 String output = "X coordinate is " + circle.getx() + 15 "\ny coordinate is " + circle.gety() + 16 "\nradius is " + circle.getradius(); 17 18 circle.setx( 35 ); // set new x-coordinate 19 circle.sety( 20 ); // set new y-coordinate 20 circle.setradius( 4.25 ); // set new radius 21 modify inherited 22 // get String representation of new circle value Use Circle3 set method to 23 output += "\n\nthe new location and radius of circle are\n" + 24 circle.tostring(); 25 Circletest3.java Line 11 Create Circle3 object. Lines 14-15 Use inherited get methods to access inherited protected instance Use inherited variables get methods x and y. to Use access Circle3 inherited variables get method protected x and to y. Create Circle3 object. Use inherited set methods to protected data x and y. modify private data radius. access Line 16 instance private variables instance x and y. variables. Use Circle3 get method to access private instance variables. Lines 18-19 Use inherited set methods to modify inherited protected data x and y. Line 20 Use Circle3 set method to modify private data radius.

26 // format floating-point values with 2 digits of precision 27 DecimalFormat twodigits = new DecimalFormat( "0.00" ); 28 29 // get Circle's diameter 30 output += "\ndiameter is " + 31 twodigits.format( circle.getdiameter() ); 32 33 // get Circle's circumference 34 output += "\ncircumference is " + 35 twodigits.format( circle.getcircumference() ); 36 37 // get Circle's area 38 output += "\narea is " + twodigits.format( circle.getarea() ); 39 40 JOptionPane.showMessageDialog( null, output ); // display output 41 42 System.exit( 0 ); 43 44 } // end method main 45 46 } // end class CircleTest3 Circletest3.jav a

9.4 Süper Sınıflar ve Alt Sınıflar arasındaki İlişki (devam) protected değişken kullanırsak Avantajları Alt sınıflar değeri direk olarak değiştirebilirler. Performansta az bir yükseliş vardır. set/get fonksiyonlarını çağırmaktan kurtarır. Dezavantajları Geçerlilik kontrolü yapılmaz. Alt sınıflar yanlış değer atayabilir. Implementasyon bağımlı Alt sınıf metodları süper sınıf implementasyonuna bağımlı kalır. Süper sınıf implementasyonu alt sınıfta sonuçları değiştirebilir. Sonuç: Kolayca kırılabilen yazılım

1 // Fig. 9.12: Point3.java 2 // Point class declaration represents an x-y coordinate pair. 3 4 public class Point3 { 5 private int x; // x part of coordinate pair 6 private int y; // y part of coordinate pair 7 8 // no-argument constructor 9 public Point3() 10 { 11 // implicit call to Object constructor occurs here 12 } 13 14 // constructor 15 public Point3( int xvalue, int yvalue ) 16 { 17 // implicit call to Object constructor occurs here 18 x = xvalue; // no need for validation 19 y = yvalue; // no need for validation 20 } 21 22 // set x in coordinate pair 23 public void setx( int xvalue ) 24 { 25 x = xvalue; // no need for validation 26 } 27 Better software-engineering practice: private over protected when possible. Point3.java Lines 5-6 Better softwareengineering practice: private over protected when possible.

28 // return x from coordinate pair 29 public int getx() 30 { 31 return x; 32 } 33 34 // set y in coordinate pair 35 public void sety( int yvalue ) 36 { 37 y = yvalue; // no need for validation 38 } 39 40 // return y from coordinate pair 41 public int gety() 42 { 43 return y; 44 } 45 46 // return String representation of Point3 object 47 public String tostring() 48 { 49 return "[" + getx() + ", " + gety() + "]"; 50 } 51 52 } // end class Point3 Invoke public methods to access private instance variables. Point3.java Line 49 Invoke public methods to access private instance variables.

1 // Fig. 9.13: Circle4.java 2 // Circle4 class inherits from Point3 and accesses Point3's 3 // private x and y via Point3's public methods. 4 5 public class Circle4 extends Point3 { 6 7 private double radius; // Circle4's radius 8 9 // no-argument constructor 10 public Circle4() 11 { 12 // implicit call to Point3 constructor occurs here 13 } 14 15 // constructor 16 public Circle4( int xvalue, int yvalue, double radiusvalue ) 17 { 18 super( xvalue, yvalue ); // call Point3 constructor explicitly 19 setradius( radiusvalue ); 20 } 21 22 // set radius 23 public void setradius( double radiusvalue ) 24 { 25 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); 26 } 27 Class Circle4 inherits from class Point3. Maintain private instance variable radius. Circle4.java Line 5 Class Circle4 inherits from class Point3. Line 7 Maintain private instance variable radius.

28 // return radius 29 public double getradius() 30 { 31 return radius; 32 } 33 34 // calculate and return diameter 35 public double getdiameter() 36 { 37 return 2 * getradius(); 38 } 39 40 // calculate and return circumference 41 public double getcircumference() 42 { 43 return Math.PI * getdiameter(); 44 } 45 46 // calculate and return area 47 public double getarea() 48 { Invoke method getradius rather than directly accessing instance variable radius. 49 return Math.PI * getradius() * getradius(); 50 } Redefine class Point3 s 51 52 method tostring. // return String representation of Circle4 object 53 public String tostring() 54 { 55 return "Center = " + super.tostring() + "; Radius = " + getradius(); 56 } 57 58 } // end class Circle4 Circle4.java Line 37, 49 and 55 Invoke method getradius rather than directly accessing instance variable radius. Lines 53-56 Redefine class Point3 s method tostring.

1 // Fig. 9.14: CircleTest4.java 2 // Testing class Circle4. 3 import java.text.decimalformat; 4 import javax.swing.joptionpane; 5 6 public class CircleTest4 { 7 8 public static void main( String[] args ) 9 { 10 // instantiate Circle object 11 Circle4 circle = new Circle4( 37, 43, 2.5 ); 12 13 // get Circle4's initial x-y coordinates and radius 14 String output = "X coordinate is " + circle.getx() + 15 "\ny coordinate is " + circle.gety() + 16 "\nradius is " + circle.getradius(); 17 18 circle.setx( 35 ); // set new x-coordinate 19 circle.sety( 20 ); // set new y-coordinate 20 circle.setradius( 4.25 ); // set new radius 21 22 // get String representation of new circle value 23 output += "\n\nthe new location and radius of circle are\n" + 24 circle.tostring(); 25 Circletest4.java Line 11 Create Circle4 object. Lines 14 and 15 Use inherited get methods to access inherited Use inherited get private methods instance to access inherited variables private x and y. instance Line 16 Use Circle4 variables get x method and y. to Use Circle4 get access private method instance to access variable radius. private instance variable radius. Lines 18-19 Use inherited seta Create Circle4 object. Use inherited seta methods to methods to modify modify inherited private Use instance Circle4 variables set method x and y. to modify private instance variable radius. inherited private instance variables x and y. Line 20 Use Circle4 set method to modify private instance variable radius.

26 // format floating-point values with 2 digits of precision 27 DecimalFormat twodigits = new DecimalFormat( "0.00" ); 28 29 // get Circle's diameter 30 output += "\ndiameter is " + 31 twodigits.format( circle.getdiameter() ); 32 33 // get Circle's circumference 34 output += "\ncircumference is " + 35 twodigits.format( circle.getcircumference() ); 36 37 // get Circle's area 38 output += "\narea is " + twodigits.format( circle.getarea() ); 39 40 JOptionPane.showMessageDialog( null, output ); // display output 41 42 System.exit( 0 ); 43 44 } // end main 45 46 } // end class CircleTest4 Circletest4.jav a

9.5 Çalışma: Üç-Düzeyli Miras Hiyerarşisi Üç-düzey point(nokta)/circle(çember)/cylinder (silindir) hiyerarşi Point x-y koordinat çifti Circle x-y koordinat çifti Radius (yarıçap) Cylinder x-y koordinat çifti Radius (yarıçap) Height (yükseklik)

1 // Fig. 9.15: Cylinder.java 2 // Cylinder class inherits from Circle4. 3 4 public class Cylinder extends Circle4 { 5 private double height; // Cylinder's height 6 7 // no-argument constructor 8 public Cylinder() 9 { 10 // implicit call to Circle4 constructor occurs here 11 } 12 13 // constructor Maintain private instance variable height. Class Cylinder extends class Circle4. 14 public Cylinder( int xvalue, int yvalue, double radiusvalue, 15 double heightvalue ) 16 { 17 super( xvalue, yvalue, radiusvalue ); // call Circle4 constructor 18 setheight( heightvalue ); 19 } 20 21 // set Cylinder's height 22 public void setheight( double heightvalue ) 23 { 24 height = ( heightvalue < 0.0? 0.0 : heightvalue ); 25 } 26 Cylinder.java Line 4 Class Cylinder extends class Circle4. Line 5 Maintain private instance variable height.

27 // get Cylinder's height 28 public double getheight() 29 { 30 return height; 31 } Redefine superclass 32 Circle4 s method 33 // override Circle4 method getarea to calculate Invoke Cylinder superclass getarea to return area 34 public double getarea() Circle4 s getarea Cylinder surface area. method using keyword super. 35 { 36 return 2 * super.getarea() + getcircumference() * getheight(); 37 } 38 39 // calculate Cylinder volume 40 public double getvolume() 41 { 42 return super.getarea() * getheight(); 43 } 44 method tostring. 45 // return String representation of Cylinder Invoke objectsuperclass Redefine class Circle4 s Circle4 s tostring method using keyword super. 46 public String tostring() 47 { 48 return super.tostring() + "; Height = " + getheight(); 49 } 50 51 } // end class Cylinder Cylinder.java Line 34 and 42 Redefine superclass Circle4 s method getarea to return Cylinder surface area. Line 36 Invoke superclass Circle4 s getarea method using keyword super. Lines 46-49 Redefine class Circle4 s method tostring. Line 48 Invoke superclass Circle4 s tostring method using keyword super.

1 // Fig. 9.16: CylinderTest.java 2 // Testing class Cylinder. 3 import java.text.decimalformat; 4 import javax.swing.joptionpane; 5 6 public class CylinderTest { 7 8 public static void main( String[] args ) 9 { 10 // create Cylinder object 11 Cylinder cylinder = new Cylinder( 12, 23, 2.5, 5.7 ); 12 13 // get Cylinder's initial x-y coordinates, radius and height 14 String output = "X coordinate is " + cylinder.getx() + CylinderTest.java Lines 14 and 15 Invoke indirectly inherited Point3 get methods. Line 16 Invoke directly inherited Circle4 get method. Invoke indirectly inherited Point3 get methods. Invoke directly inherited Invoke Cylinder Line 16get method. Invoke Cylinder get Circle4 get method. method. Lines 18-19 Invoke indirectly inherited Point3 set Invoke indirectly inherited methods. Point3 set methods. Line 20 Invoke directly inherited Invoke directly inherited Circle4 set method. Circle4 set method. Invoke Cylinder set Line 21 method. Invoke Cylinder set method. Line 26 Invoke overridden Invoke overridden tostring method. tostring method. method. 15 "\ny coordinate is " + cylinder.gety() + "\nradius is " + 16 cylinder.getradius() + "\nheight is " + cylinder.getheight(); 17 18 cylinder.setx( 35 ); // set new x-coordinate 19 cylinder.sety( 20 ); // set new y-coordinate 20 cylinder.setradius( 4.25 ); // set new radius 21 cylinder.setheight( 10.75 ); // set new height 22 23 // get String representation of new cylinder value 24 output += 25 "\n\nthe new location, radius and height of cylinder are\n" + 26 cylinder.tostring(); 27

28 // format floating-point values with 2 digits of precision 29 DecimalFormat twodigits = new DecimalFormat( "0.00" ); 30 31 // get Cylinder's diameter 32 output += "\n\ndiameter is " + 33 twodigits.format( cylinder.getdiameter() ); 34 35 // get Cylinder's circumference 36 output += "\ncircumference is " + 37 twodigits.format( cylinder.getcircumference() ); 38 39 // get Cylinder's area 40 output += "\narea is " + twodigits.format( cylinder.getarea() ); 41 42 // get Cylinder's volume 43 output += "\nvolume is " + twodigits.format( cylinder.getvolume() ); 44 45 JOptionPane.showMessageDialog( null, output ); // display output 46 47 System.exit( 0 ); 48 49 } // end main 50 51 } // end class CylinderTest CylinderTest.ja va Line 40 Invoke overridden getarea method. Invoke overridden getarea method.

Alt sınıflarda Yapılandırıcılar ve Sonlandırıcılar

class Animal { public void makenoise ( ) { System.out.println ( I am an animal. ); } // of makenoise } // of Animal class Fish extends Animal { public void makenoise( ) { System.out.println ( Glug glug gurgle gurgle ); } // of makenoise } // of Fish class Bird extends Animal { public void makenoise( ) { System.out.println( Tweet tweet flap flap ); } // of makenoise } // of Bird class Dog extends Animal { public void makenoise ( ) { System.out.println( Sniff sniff woof woof ); } // of makenoise public void bark ( ) { System.out.println( Arf Arf ); } // of bark } // of Dog

class Animal { public int numlegs; public static int total = 0; private String strtype; public Animal (int numlegs){ this.numlegs = numlegs; strtype = Animal ; total++; } public void move ( ) { System.out.println ( I am an animal and am moving. ); } public String tostring(){ return strtype + with + numlegs + legs. ; } } // of Animal Bellekte Durum Nasıl? class Fish extends Animal { public Fish(int numlegs){ super(numlegs); strtype = Fish ; } public void move( ) { System.out.println ( Swim, swim, swim, swim! ); } } // of Fish class Bird extends Animal { public Bird(int numlegs){ super (numlegs); strtype = Bird ; } public void move( ) { System.out.println ( Flap, flap, flap again ); } } // of Bird class Dog extends Animal { public Dog(int numlegs){ super (numlegs); strtype = Dog ; } public void move ( ) { System.out.println ( Run, scratch, walk, run ); } public void bark ( ){ System.out.println ( Arf Arf ); } } // of Dog

Animal Sınıfı İçin Bellek Durumu class Animal { public int numlegs; public static int total; private String strtype; public Animal (int numlegs){ this.numlegs = numlegs; strtype = Animal ; } public void move ( ) { System.out.println ( I am an animal and am moving. ); } public String tostring(){ return strtype + with + numlegs + legs. ; } } // of Animal Animal int numlegs String strtype tostring(); move();

Animal Sınıfı İçin Bellek Durumu class Animal { public int numlegs; public static int total; private String strtype; public Animal (int numlegs){ this.numlegs = numlegs; strtype = Animal ; } public void move ( ) { System.out.println ( I am an animal and am moving. ); } public String tostring(){ return strtype + with + numlegs + legs. ; } } // of Animal Object Animal tostring() int numlegs String strtype tostring(); move();

Fish Sınıfı için Bellek Durumu class Animal { public int numlegs; public static int total; private String strtype; Fish sınıfı daha kompleks.animal sınıfından miras almıştı. public Animal (int numlegs){ this.numlegs = numlegs; strtype = Animal ; } public void move ( ) { System.out.println ( I am an animal and am moving. ); } public String tostring(){ return strtype + with + numlegs + legs. ; } } // of Animal class Fish extends Animal { public Fish(int numlegs){ super(numlegs); strtype = Fish ; } public void move( ) { System.out.println ( Swim, swim, swim, swim! ); } } // of Fish Object Animal tostring() int numlegs String strtype tostring(); move(); Fish move(); Bellekteki Fish sınıfı nı gösterir. a Fish IS-A Animal, and an Animal IS-A Object,

Fish Sınıfı için Bellek Durumu Eğer Fish sınıfından bir örnek obje oluştursak, Fish objesini gösteren referansımız şekildeki gibi gösterilebilir. Fish ftemp; ftemp = new Fish(0); ftemp Object Animal tostring() int numlegs = 0 String strtype tostring(); move(); Fish move();

Yapılandırıcı Zinciri Object 1 Object 2 Object 3 tostring() tostring() tostring() Animal int numlegs = 0 String strtype tostring(); move(); Fish move(); Animal int numlegs = 0 String strtype tostring(); move(); Fish move(); Animal int numlegs = 0 String strtype tostring(); move(); Fish move(); Fish ftemp = new Fish(0); To make a Fish, Java must first make an Animal. To make an Animal, Java must first make an Object. ftemp

9.6 Alt sınıflarda Yapılandırıcılar ve Sonlandırıcılar Yapılandırıcı çağırım zinciri Alt sınıf yapılandırıcıları süper sınıfların yapılandırıcılarını çağırır. Açıkça yada dolaylı olarak Miras hiyerarşisinin temeli Zincirdeki son yapılandırıcı Object yapılandırıcısını çağırır. Orjinal alt sınıf yapılandırıcısının gövde kodları en son biter. Örnek: Point3/Circle4/Cylinder hiyerarşisi Point3 yapılandırıcısı sondan ikinsici olarak çağrılır. (sonuncusu Object yapılandırıcısıdır) Point3 yapılandırıcısının gövde kodları ikinci sırada biter. (ilk olarak Object yapılandırıcısının biter)

9.6 Türetilmiş Sınıflarda Yapılandırıcı ve Yıkıcılar Alt sınıflarda çöp toplayıcısı (garbage collection) finalize metodun çağırım zinciri Yapılandırıcı zincirinin ters sırası işler. İlk önce alt sınıfın sonlandırıcısı çağrılır. Sırasıyla yukarıya doğru süper sınıfların sonlandırıcıları çağrılır. Bu iş en üst süper sınıfa kadar devam eder. Son süper sınıfın sonlanmasından sonra (Object), nesne bellekten silinir.

1 // Fig. 9.17: Point.java 2 // Point class declaration represents an x-y coordinate pair. 3 4 public class Point { 5 private int x; // x part of coordinate pair 6 private int y; // y part of coordinate pair 7 8 // no-argument constructor 9 public Point() 10 { 11 // implicit call to Object constructor occurs here 12 System.out.println( "Point no-argument constructor: " + this ); 13 } 14 15 // constructor 16 public Point( int xvalue, int yvalue ) 17 { 18 // implicit call to Object constructor occurs here 19 x = xvalue; // no need for validation 20 y = yvalue; // no need for validation 21 22 System.out.println( "Point constructor: " + this ); 23 } 24 25 // finalizer 26 protected void finalize() 27 { 28 System.out.println( "Point finalizer: " + this ); 29 } 30 Point.java Lines 12, 22 and 28 Constructor and finalizer output messages to demonstrate method call order. Constructor and finalizer output messages to demonstrate method call order.

31 // set x in coordinate pair 32 public void setx( int xvalue ) 33 { 34 x = xvalue; // no need for validation 35 } 36 37 // return x from coordinate pair 38 public int getx() 39 { 40 return x; 41 } 42 43 // set y in coordinate pair 44 public void sety( int yvalue ) 45 { 46 y = yvalue; // no need for validation 47 } 48 49 // return y from coordinate pair 50 public int gety() 51 { 52 return y; 53 } 54 55 // return String representation of Point4 object 56 public String tostring() 57 { 58 return "[" + getx() + ", " + gety() + "]"; 59 } 60 61 } // end class Point Point.java

1 // Fig. 9.18: Circle.java 2 // Circle5 class declaration. 3 4 public class Circle extends Point { 5 6 private double radius; // Circle's radius 7 8 // no-argument constructor 9 public Circle() 10 { 11 // implicit call to Point constructor occurs here 12 System.out.println( "Circle no-argument constructor: " + this ); 13 } 14 15 // constructor 16 public Circle( int xvalue, int yvalue, double radiusvalue ) 17 { 18 super( xvalue, yvalue ); // call Point constructor 19 setradius( radiusvalue ); 20 21 System.out.println( "Circle constructor: " + this ); 22 } 23 24 // finalizer 25 protected void finalize() 26 { 27 System.out.println( "Circle finalizer: " + this ); 28 29 super.finalize(); // call superclass finalize method 30 } 31 Circle.java Lines 12, 21 and 29 Constructor and finalizer output messages to demonstrate method Constructor and finalizer call order. output messages to demonstrate method call order.

32 // set radius 33 public void setradius( double radiusvalue ) 34 { 35 radius = ( radiusvalue < 0.0? 0.0 : radiusvalue ); 36 } 37 38 // return radius 39 public double getradius() 40 { 41 return radius; 42 } 43 44 // calculate and return diameter 45 public double getdiameter() 46 { 47 return 2 * getradius(); 48 } 49 50 // calculate and return circumference 51 public double getcircumference() 52 { 53 return Math.PI * getdiameter(); 54 } Circle.java

55 56 // calculate and return area 57 public double getarea() 58 { 59 return Math.PI * getradius() * getradius(); 60 } 61 62 // return String representation of Circle5 object 63 public String tostring() 64 { 65 return "Center = " + super.tostring() + "; Radius = " + getradius(); 66 } 67 68 } // end class Circle Circle.java

1 // Fig. 9.19: ConstructorFinalizerTest.java 2 // Display order in which superclass and subclass 3 // constructors and finalizers are called. 4 5 public class ConstructorFinalizerTest { 6 7 public static void main( String args[] ) 8 { 9 Point point; 10 Circle circle1, circle2; 11 12 point = new Point( 11, 22 ); 13 14 System.out.println(); 15 circle1 = new Circle( 72, 29, 4.5 ); 16 17 System.out.println(); 18 circle2 = new Circle( 5, 7, 10.67 ); 19 20 point = null; // mark for garbage collection 21 circle1 = null; // mark for garbage collection 22 circle2 = null; // mark for garbage collection 23 24 System.out.println(); 25 ConstructorFina lizertest.java Point object goes in and out Line 12 of scope immediately. Point object goes in and out of scope immediately. Instantiate two Circle objects to demonstrate Lines 15 order and 18 of subclass and superclass Instantiate two constructor/finalizer Circle method objects to calls. demonstrate order of subclass and superclass constructor/finalizer method calls.

26 System.gc(); // call the garbage collector 27 28 } // end main 29 30 } // end class ConstructorFinalizerTest Point constructor: [11, 22] Point constructor: Center = [72, 29]; Radius = 0.0 Circle constructor: Center = [72, 29]; Radius = 4.5 ConstructorFina lizertest.java Subclass Circle constructor body executes after superclass Point4 s constructor finishes execution. Point constructor: Center = [5, 7]; Radius = 0.0 Circle constructor: Center = [5, 7]; Radius = 10.67 Point finalizer: [11, 22] Circle finalizer: Center = [72, 29]; Radius = 4.5 Point finalizer: Center = [72, 29]; Radius = 4.5 Circle finalizer: Center = [5, 7]; Radius = 10.67 Point finalizer: Center = [5, 7]; Radius = 10.67 Finalizer for Circle object called in reverse order of constructors.

58 10.6 final in Miras ile Kullanılması final metodlar (metodu bindirmeden korur) Bindirilemezler. private metodlar zorunlu olarak final methodlardır. static methodlarda zorunlu final dır. final sınıfları (Sınıfı mirastan korur) Süper sınıf olamazlar. (Miras alınamazlar) final sınıfında tanımlı metodların hepsi zorunlu olarak final dır. String sınıfı gibi... 2003 Prentice Hall, Inc. All rights reserved.

class A { Outline 59 } final void meth() { System.out.println( Bu final metoddur ) } class B extends A{ } void meth () { // HATA! BİNDİRİLEMEZ System.out.println( yanliş ) } ****************************************************** final class A { //... } class B extends A { // HATA!! A nın alt sınıfı olamaz. } 2003 Prentice Hall, Inc. All rights reserved.

9.9 Miras ile Yazılım Mühendisliğine Bakış Varolan yazılımları ihtiyaca göre değiştirilebilir Varolan sınıflardan miras alınır. Yeni üyeler eklenir. Yeniden süper sınıf üyeleri tanımlanabilir. Süper sınıfın kaynak kodlarına erişmeye gerek yoktur. Object kodlarla bağlantı kurulur. Bağımsız yazılım sektörü (Independent software vendors) (ISVs) Tescilli satış/lisanslı kodlar geliştirilir. object-kod formatında erişilebilir. Kullanıcılar yeni sınıflar türetebilir. ISV kaynak koduna ulaşmadan üretebilirler.

-THE ENDif (getquestions()) answerthem(); else goodbye();