JavaJava · Chapterبەش 10 · Lectureوانە 10.3

Inheritance and Polymorphismبۆماوە و فرەشێوەیی

Build new classes from old ones. extends and the is-a test; what a subclass inherits and what private and protected mean for it; super(...) and the constructor chain; overriding with @Override and how it differs from overloading; polymorphism, where one Vehicle variable holds a car, a bus or a motorbike and the object's own method runs; casting and instanceof; Object as the root of every class, with toString and equals; final classes and methods. Pattern matching and sealed classes appear as extras.

دروستکردنی نوێ لە کۆنەوە. extends و تاقیکردنەوەی ⁦is-a⁩؛ چی بە وەردەگرێت و private و protected چییان بۆ دەگەیەنن؛ super(...) و زنجیرەی ؛ بە @Override و جیاوازییەکەی لەگەڵ ؛ ، کە یەک Vehicle ئۆتۆمبێل، پاس یان ماتۆڕسکیل هەڵدەگرێت و خودی جێبەجێ دەبێت؛ و instanceof؛ Object وەک ڕەگی هەموو لەگەڵ toString و equals؛ و final. pattern matching و sealed وەک زیادە دێن.

34 min read29 خولەک خوێندنەوە · 28 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Write subclasses with extends, test the design with is-a, and explain what is and is not inherited.
  • Call superclass constructors with super(...) and fix the javac errors about them.
  • Override methods with @Override, use super.method(), and tell overriding from overloading.
  • Use polymorphism with superclass variables and lists, and cast safely with instanceof.
  • Override toString, equals and hashCode, and use final classes and methods.
  • بە extends بنووسیت، دیزاینەکە بە ⁦is-a⁩ تاقی بکەیتەوە و ڕوون بکەیتەوە چی بە دەدرێت و چی نا.
  • بە super(...) بانگی بکەیت و هەڵەکانی javac لەبارەیان چاک بکەیتەوە.
  • بە @Override دووپاتە پێناسە بکەیتەوە، super.method() بەکار بهێنیت و لە جیا بکەیتەوە.
  • بە و بەکار بهێنیت و بە instanceof بە سەلامەتی جۆر بگۆڕیت.
  • toString، equals و hashCode دووپاتە پێناسە بکەیتەوە و و final بەکار بهێنیت.

Why inheritance?بۆچی بۆماوە؟

A bus company in Hawler keeps cars, buses and motorbikes. Every vehicle has a number plate and a number of seats, and every vehicle should be able to describe itself. Three separate classes, Car, Bus and Motorbike, would repeat the same fields and the same describe() method three times, and every fix would have to be made three times too.

کۆمپانیایەکی گواستنەوە لە هەولێر ئۆتۆمبێل، پاس و ماتۆڕسکیلی هەیە. هەموو ئامرازێکی گواستنەوە ژمارەیەک ⁦(plate)⁩ و چەند کورسییەکی هەیە و هەمووشیان دەبێت بتوانن خۆیان وەسف بکەن. سێ جیاواز، Car، Bus و Motorbike، هەمان و هەمان describe() سێ جار دووپاتە دەکەنەوە و هەر چاککردنێکیش دەبێت سێ جار بکرێت.

Left, without inheritance: three class boxes Car, Bus and Motorbike, each repeating plate, seats and describe() (highlighted as copies) next to its own field doors, route or deliveryBox; a note says a change or a fix must be made three times. Right, with inheritance: one box Vehicle with plate, seats and describe(), and below it Car, Bus and Motorbike with only doors, route and deliveryBox, joined to Vehicle by one extends arrow.لای چەپ، بەبێ بۆماوە: سێ سندووقی کلاس، Car و Bus و Motorbike، هەر یەکەیان plate و seats و describe() دووپاتە دەکاتەوە (وەک کۆپی دیار کراون) لە تەنیشت تایبەتمەندییە تایبەتەکەی خۆی، doors یان route یان deliveryBox؛ تێبینییەک دەڵێت هەر گۆڕانکاری یان چاککردنێک دەبێت سێ جار بکرێت. لای ڕاست، بە بۆماوە: یەک سندووقی Vehicle بە plate و seats و describe() و لە خوارییەوە Car و Bus و Motorbike تەنیا بە doors و route و deliveryBox، کە بە یەک تیری extends بە Vehicleـەوە بەستراون.
Copy three times, or write once and extendسێ جار کۆپی بکە، یان یەک جار بنووسە و extends بکەCêIT

Inheritance solves this. You write the shared part once, in a class Vehicle, and the other classes extend it: class Car extends Vehicle. Vehicle is the superclass (also called the parent or base class); Car is a subclass (a child or derived class). The subclass receives the members of the superclass and adds only what is new. Java's own classes are built this way too: all exceptions form one family of classes (The exception family).

ئەم کێشەیە چارەسەر دەکات. بەشە هاوبەشەکە یەک جار لە بە ناوی Vehicle دەنووسیت و تر لێی extends دەکەن: class Car extends Vehicle. Vehicle و Car . ئەندامەکانی وەردەگرێت و تەنیا ئەوەی نوێیە زیاد دەکات. خودی Javaـش بەم شێوەیە دروست کراون: هەموو یەک خێزانی پێک دەهێنن (خێزانی هەڵاوێردەکان).

Definitionپێناسە

Making a new from an existing one: the new class (subclass) gets the fields and methods of the old one (superclass) and can add or change some. Dart allows single inheritance: a class extends only one class.

دروستکردنی نوێیە لە کلاسێکی هەبووەوە: کلاسە نوێیەکە ⁦(subclass)⁩ ستوون و میتۆدەکانی کلاسە کۆنەکە ⁦(superclass)⁩ وەردەگرێت و دەتوانێت هەندێکیان زیاد بکات یان بگۆڕێت. Dart تەنیا یەک بۆماوە ڕێگە دەدات: هەر کلاسێک تەنیا یەک کلاس extends دەکات.

extends: a subclass receives the superclass's membersextends: کلاسی منداڵ ئەندامەکانی کلاسی باوک وەردەگرێت

Here is the vehicle example in Java. Vehicle has two fields and a method. Car and Bus each add one field and write nothing else, yet a Car object has plate, seats and describe() as well:

ئەمە نموونەی ئامرازەکانی گواستنەوەیە بە Java. Vehicle دوو و هەیە. Car و Bus هەر یەکەیان تەنیا زیاد دەکەن و هیچی تر نانووسن، کەچی Car ئەمانەشی هەیە: plate، seats و describe():

Garage.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Car extends Vehicle {
    int doors;
}

class Bus extends Vehicle {
    String route;
}

public class Garage {
    public static void main(String[] args) {
        Car car = new Car();
        car.plate = "HW-1234";
        car.seats = 5;
        car.doors = 4;
        car.describe();
        System.out.println("Doors: " + car.doors);

        Bus bus = new Bus();
        bus.plate = "DH-5678";
        bus.seats = 45;
        bus.route = "Hawler - Duhok";
        bus.describe();
        System.out.println("Route: " + bus.route);
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1-8

The superclass: the shared part, written once.

: بەشە هاوبەشەکە، یەک جار نووسراوە.

Lineدێڕی 10-11

extends Vehicle: Car receives plate, seats and describe(), and adds doors.

extends Vehicle: Car ئەمانە وەردەگرێت: plate، seats و describe()؛ doorsـیش زیاد دەکات.

Lineدێڕی 21

An inherited field, used like an own field.

بۆماوەگیراو، وەک خۆی بەکار دێت.

Lineدێڕی 24

The method was written only in Vehicle, but it works on a Car.

تەنیا لە Vehicleـدا نووسراوە، بەڵام لەسەر Carـیش کار دەکات.

A class diagram. At the top, the superclass Vehicle with String plate, int seats and void describe(). Below it, joined by one extends arrow with a hollow triangle, three subclasses: Car, Bus and Motorbike, each with a small picture. In each subclass the inherited members String plate, int seats and void describe() are grey, and its own field is highlighted: int doors, String route and boolean deliveryBox.هێڵکارییەکی کلاسەکان. لە سەرەوە کلاسی باوکەکە، Vehicle، بە String plate و int seats و void describe(). لە خوارییەوە سێ کلاسی منداڵ، Car و Bus و Motorbike، کە بە یەک تیری extends (سێگۆشەیەکی بەتاڵ) پێوەی بەستراون و هەر یەکەیان وێنەیەکی بچووکی هەیە. لە هەر کلاسی منداڵێکدا ئەندامە بۆماوەگیراوەکان، String plate و int seats و void describe()، خۆڵەمێشین و تایبەتمەندییە تایبەتەکەی دیار کراوە: int doors و String route و boolean deliveryBox.
One superclass, three subclassesیەک ، سێ CêIT

extends: a subclass receives the superclass's membersextends: کلاسی منداڵ ئەندامەکانی کلاسی باوک وەردەگرێت

The is-a testتاقیکردنەوەی ⁦is-a⁩

Use extends only when the sentence A ___ is a ___ is true. A car is a vehicle, so Car extends Vehicle fits. A bus is not a car, so Bus must not extend Car. When the true sentence is has a, use a field instead: a bus has a driver, so Bus gets a field Driver driver;, and it does not extend Driver.

extends تەنیا کاتێک بەکار بهێنە کە ڕستەی «⁦___⁩ جۆرێکە لە ⁦___⁩» ڕاست بێت. ئۆتۆمبێل جۆرێکە لە ئامرازی گواستنەوە، بۆیە Car extends Vehicle دەگونجێت. پاس جۆرێک نییە لە ئۆتۆمبێلی سواری، بۆیە نابێت Bus لە Car extends بکات. کاتێک ڕستە ڕاستەکە «...ی هەیە» بێت، لە جیاتی ئەوە بەکار بهێنە: پاس شۆفێرێکی هەیە، بۆیە Bus Driver driver; وەردەگرێت و لە Driver extends ناکات.

Left, under the heading is a, extends: four cards. A Car is a Vehicle: class Car extends Vehicle (tick). A Bus is a Vehicle: class Bus extends Vehicle (tick). A Bus is a Car? No: a Bus is a Vehicle, not a Car (cross). A Vehicle is a Car? Not always: it may be a bus (cross). Right, under has a, a field: a bus with a driver; A Bus has a Driver, a Driver is not a Bus, and the code class Bus extends Vehicle { Driver driver; }: a field, not extends.لای چەپ، لە ژێر سەردێڕی «جۆرێکە لە ← extends»: چوار کارت. Car جۆرێکە لە Vehicle: class Car extends Vehicle (✓). Bus جۆرێکە لە Vehicle: class Bus extends Vehicle (✓). Bus جۆرێکە لە Car؟ نەخێر: Bus جۆرێکە لە Vehicle، نەک لە Car (✗). Vehicle جۆرێکە لە Car؟ هەمیشە نا: لەوانەیە پاس بێت (✗). لای ڕاست، لە ژێر «هەیەتی ← تایبەتمەندییەک»: پاسێک بە شۆفێرەوە؛ Bus شۆفێرێکی هەیە، شۆفێر جۆرێک نییە لە Bus و کۆدی class Bus extends Vehicle { Driver driver; }: تایبەتمەندییەک، نەک extends.
is a: extends · has a: a fieldجۆرێکە لە: extends · ...ی هەیە: CêIT

Database designers ask the same question when they specialise an entity (Inheritance in E-R models), and Dart classes inherit in the same way (Inheritance in Dart).

دیزاینەرانی کاتێک تایبەت دەکەن هەمان پرسیار دەکەن (بۆماوە لە مۆدێلی ⁦E-R⁩) و Dart بە هەمان شێوە وەردەگرن (بۆماوە لە Dart).

extends: a subclass receives the superclass's membersextends: کلاسی منداڵ ئەندامەکانی کلاسی باوک وەردەگرێت

One superclass, many levelsیەک کلاسی باوک، چەندین ئاست

A subclass can itself be extended. SchoolBus extends Bus, and Bus extends Vehicle, so a school bus has the members of both:

خۆیشی دەتوانێت extends بکرێت. SchoolBus extends Bus و Bus extends Vehicle، بۆیە پاسی قوتابخانە ئەندامەکانی هەردووکیانی هەیە:

Levels.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Bus extends Vehicle {
    String route;
}

class SchoolBus extends Bus {
    String school;
}

public class Levels {
    public static void main(String[] args) {
        SchoolBus sb = new SchoolBus();
        sb.plate = "DH-3307";
        sb.seats = 30;
        sb.route = "Old town - school";
        sb.school = "School No. 5";
        sb.describe();
        System.out.println(sb.route + " (" + sb.school + ")");
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 14-15

Two levels: SchoolBus gets route from Bus and plate, seats and describe() from Vehicle.

دوو ئاست: SchoolBus route لە Busـەوە و plate، seats و describe() لە Vehicleـەوە وەردەگرێت.

But every Java class has exactly one direct superclass. class TaxiBus extends Car, Bus is not Java: javac stops with the short message '{' expected. A class combines abilities from several places with interfaces instead (Several interfaces).

بەڵام هەموو Java ڕێک یەک ڕاستەوخۆی هەیە. class TaxiBus extends Car, Bus Java نییە: javac بە پەیامی کورتی '{' expected دەوەستێت. بە توانا لە چەند شوێنێکەوە کۆ دەکاتەوە (چەند نێوبەستەیەک).

Left, many levels: yes. A chain Vehicle (plate, seats; describe()), Bus (route) and SchoolBus (school), each pointing up to the one above. A SchoolBus has plate, seats and describe() from Vehicle, route from Bus and school of its own. Right, two superclasses: no. A box TaxiBus with dashed arrows to both Car and Bus, crossed out; the code class TaxiBus extends Car, Bus { and the javac message error: '{' expected.لای چەپ، چەند ئاستێک: بەڵێ. زنجیرەی Vehicle (plate و seats؛ describe()) و Bus (route) و SchoolBus (school)، هەر یەکەیان ئاماژە بۆ ئەوەی سەرەوەی دەکات. SchoolBus ئەمانەی هەیە: plate و seats و describe() لە Vehicleـەوە، route لە Busـەوە و school کە هی خۆیەتی. لای ڕاست، دوو کلاسی باوک: نەخێر. سندووقی TaxiBus بە دوو تیری پچڕپچڕ بۆ Car و Bus کە خەتیان بەسەردا هاتووە؛ کۆدی class TaxiBus extends Car, Bus { و پەیامی javac: error: '{' expected.
Many levels, but one direct superclassچەند ئاستێک، بەڵام یەک ڕاستەوخۆCêIT

What a subclass inherits, and what notکلاسی منداڵ چی بە بۆماوە وەردەگرێت و چی نا

A subclass inherits the public and protected members of its superclass, and the members with no modifier when both classes are in the same package. Two things are not inherited: private members and constructors. A private field still exists inside every object; only the superclass's own code may name it. Here km is private, yet the inherited methods drive() and getKm() work with it for Car:

ئەندامە public و protectedـەکانی بە وەردەگرێت، هەروەها ئەندامە بێ دیاریکەرەکان کاتێک هەردوو لە هەمان بن. دوو شت بە نادرێن: ئەندامە privateـەکان و . private هێشتا لە ناو هەموو هەیە؛ تەنیا کۆدی خودی دەتوانێت ناوی بهێنێت. لێرەدا km privateـە، کەچی بۆماوەگیراوەکان، drive() و getKm()، بۆ Car لەگەڵیدا کار دەکەن:

Odometer.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    private int km;          // only Vehicle's own code may use it

    void drive(int distance) {
        km = km + distance;
    }

    int getKm() {
        return km;
    }
}

class Car extends Vehicle {
    void trip() {
        drive(120);
        System.out.println(plate + " has driven " + getKm() + " km");
    }
}

public class Odometer {
    public static void main(String[] args) {
        Car car = new Car();
        car.plate = "HW-1234";
        car.trip();
        car.trip();
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

Private: only code inside Vehicle may use km.

private: تەنیا کۆدی ناو Vehicle دەتوانێت km بەکار بهێنێت.

Lineدێڕی 15-17

Car calls the inherited public methods; they change km for it.

Car بانگی بۆماوەگیراوەکان دەکات؛ ئەوان km بۆ ئەو دەگۆڕن.

A method in Car that writes km = 0; does not compile: km has private access in Vehicle. That is encapsulation at work (Encapsulation): even a subclass goes through the superclass's methods.

لە Carـدا کە km = 0; بنووسێت کۆمپایل نابێت: km has private access in Vehicle. ئەمە لە کاردا (کەپسوولکردن): تەنانەت بە تێدەپەڕێت.

A Car object drawn as a box with two parts. The Vehicle part (inherited) holds plate = "HW-1234", private int km = 240 with a padlock, void drive(int distance) and int getKm(). The Car part holds void trip() and void resetKm(). Arrows from trip() to drive() and getKm() are ticked: trip() may call them, and they work with km. A dashed arrow from resetKm() to km is crossed, with error: km has private access in Vehicle. Notes: private members are not inherited by name; constructors are not inherited at all (a subclass calls them with super).ئۆبجێکتێکی Car وەک سندووقێکی دوو بەشی. بەشی Vehicle (بۆماوەگیراو): plate = "HW-1234" و private int km = 240 بە قفڵێکەوە و void drive(int distance) و int getKm(). بەشی Car: void trip() و void resetKm(). تیرەکانی trip() بۆ drive() و getKm() نیشانەی ✓یان هەیە: trip() دەتوانێت بانگیان بکات و ئەوان لەگەڵ km کار دەکەن. تیرێکی پچڕپچڕ لە resetKm()ـەوە بۆ km خەتی بەسەردا هاتووە، لەگەڵ error: km has private access in Vehicle. تێبینییەکان: ئەندامی private بە ناو بە بۆماوە نادرێن؛ دروستکەرەکان هەرگیز بە بۆماوە نادرێن (کلاسی منداڵ بە super بانگیان دەکات).
private: inside the object, but not for Carprivate: لە ناو ، بەڵام بۆ Car ناCêIT

What a subclass inherits, and what notکلاسی منداڵ چی بە بۆماوە وەردەگرێت و چی نا

protected: open to subclassesprotected: کراوەیە بۆ کلاسەکانی منداڵ

protected lies between package access and public (Access modifiers): classes of the same package may use the member, and so may a subclass in any package. Here Vehicle is in the package vehicles (Your own package), and Taxi in the package app extends it:

protected لە نێوان دەستڕاگەیشتنی و publicـدایە (دیاریکەرەکانی دەستڕاگەیشتن): هەمان دەتوانن ئەندامەکە بەکار بهێنن، هەروەها لە هەر بێت. لێرەدا Vehicle لە vehiclesـدایە (پاکێجەکەی خۆت) و Taxi لە app لێی extends دەکات:

vehicles/Vehicle.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
package vehicles;

public class Vehicle {
    protected String plate;

    public void describe() {
        System.out.println("Vehicle " + plate);
    }
}
app/Taxi.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
package app;

import vehicles.Vehicle;

public class Taxi extends Vehicle {
    public Taxi(String plate) {
        this.plate = plate;        // a subclass: allowed
    }

    public void sign() {
        System.out.println("TAXI " + plate);
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 7

Taxi is a subclass, so it may use the protected field, although it is in another package.

Taxi ، بۆیە دەتوانێت protectedـەکە بەکار بهێنێت، هەرچەندە لە ترە.

app/Main.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
package app;

import vehicles.Garage;

public class Main {
    public static void main(String[] args) {
        Taxi t = new Taxi("SL-9012");
        t.sign();
        Garage.check(t);
        System.out.println(t.plate);
    }
}

Main is in another package and is not a subclass, so t.plate is refused. A good habit: keep fields private and make protected only what subclasses really need, often a method rather than a field.

Main لە ترە و نییە، بۆیە t.plate ڕەت دەکرێتەوە. خووی باش: private بهێڵەوە و تەنیا ئەوە بکە بە protected کە بە ڕاستی پێویستیانە، زۆرجار نەک .

Two packages. In package vehicles: the class Vehicle with protected String plate (highlighted) and public void describe(), and the class Garage with check(Vehicle v): v.plate, ticked: same package, allowed. In package app: Taxi extends Vehicle with this.plate = plate; and sign(): "TAXI " + plate, ticked: a subclass, allowed; and Main with System.out.println(t.plate);, crossed: not a subclass, another package, with error: plate has protected access in Vehicle. An inheritance arrow goes from Taxi to Vehicle.دوو پاکێج. لە پاکێجی vehicles: کلاسی Vehicle بە protected String plate (دیارکراو) و public void describe() و کلاسی Garage بە check(Vehicle v): v.plate، بە نیشانەی ✓: هەمان پاکێج، ڕێگەپێدراوە. لە پاکێجی app: Taxi extends Vehicle بە this.plate = plate; و sign(): "TAXI " + plate، بە نیشانەی ✓: کلاسی منداڵ، ڕێگەپێدراوە؛ و Main بە System.out.println(t.plate);، بە نیشانەی ✗: کلاسی منداڵ نییە و لە پاکێجێکی ترە، لەگەڵ error: plate has protected access in Vehicle. تیرێکی بۆماوە لە Taxiـەوە بۆ Vehicle دەچێت.
protected: the same package and subclassesprotected: هەمان و CêIT

super(...): the superclass part is built first⁦super(...)⁩: سەرەتا بەشی کلاسی باوک دروست دەکرێت

Constructors are not inherited, so Car must write its own. When Vehicle has a constructor Vehicle(String plate, int seats), the first statement of the Car constructor is super(plate, 5);: it runs the superclass constructor, which fills the Vehicle part of the new object. Then Car fills its own part:

بە نادرێن، بۆیە Car دەبێت هی خۆی بنووسێت. کاتێک Vehicle Vehicle(String plate, int seats)ـی هەیە، یەکەم Car بریتییە لە super(plate, 5);: جێبەجێ دەکات کە بەشی Vehicleـی نوێیەکە پڕ دەکاتەوە. پاشان Car بەشەکەی خۆی پڕ دەکاتەوە:

Build.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
        System.out.println("Vehicle part ready: " + plate);
    }
}

class Car extends Vehicle {
    int doors;

    Car(String plate, int doors) {
        super(plate, 5);
        this.doors = doors;
        System.out.println("Car part ready: " + doors + " doors");
    }
}

public class Build {
    public static void main(String[] args) {
        Car car = new Car("HW-1234", 4);
        System.out.println(car.plate + ", " + car.seats + " seats, " + car.doors + " doors");
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 16

The plate and 5 seats go up to the Vehicle constructor.

ژمارەکە و 5 کورسی دەچن بۆ Vehicle.

Lineدێڕی 17

Only then does Car set its own field.

تەنیا ئەوکات Car خۆی دادەنێت.

The line Car car = new Car("HW-1234", 4); at the top. Two code panels: the superclass constructor Vehicle(String plate, int seats) { this.plate = plate; this.seats = seats; } and the subclass constructor Car(String plate, int doors) { super(plate, 5); this.doors = doors; }. An arrow carries "HW-1234", 5 from super(plate, 5); up to the Vehicle constructor. On the right, the object car with plate "HW-1234" and seats 5 marked 1 (the Vehicle part is filled first) and doors 4 marked 2 (then the Car part), and the output Vehicle part ready: HW-1234, Car part ready: 4 doors.دێڕی Car car = new Car("HW-1234", 4); لە سەرەوە. دوو پانێڵی کۆد: دروستکەری کلاسی باوکەکە، Vehicle(String plate, int seats) { this.plate = plate; this.seats = seats; } و دروستکەری کلاسی منداڵەکە، Car(String plate, int doors) { super(plate, 5); this.doors = doors; }. تیرێک "HW-1234" و 5 لە super(plate, 5);ـەوە بۆ سەرەوە دەبات بۆ دروستکەری Vehicle. لای ڕاست، ئۆبجێکتی car بە plate "HW-1234" و seats 5 کە ژمارە 1یان لەسەرە (سەرەتا بەشی Vehicle پڕ دەکرێتەوە) و doors 4 کە ژمارە 2ی لەسەرە (پاشان بەشی Car) و دەرخستەی Vehicle part ready: HW-1234 و Car part ready: 4 doors.
super(...) fills the superclass part first⁦super(...)⁩ سەرەتا بەشی پڕ دەکاتەوەCêIT

super(...): the superclass part is built first⁦super(...)⁩: سەرەتا بەشی کلاسی باوک دروست دەکرێت

No super(...)? javac adds super()⁦super(...)⁩ نییە؟ javac خۆی ⁦super()⁩ زیاد دەکات

Every constructor begins by building the superclass part. If you write no super(...) (and no this(...)), javac puts a hidden super(); in, a call with no arguments. That is why the classes of the last lessons worked: their superclass is Object, which has a no-argument constructor (The default constructor). But this Vehicle has only Vehicle(String, int):

هەموو بە دروستکردنی بەشی دەست پێ دەکات. ئەگەر هیچ super(...) (یان this(...)) نەنووسیت، javac super();ـێکی شاراوە دادەنێت، بانگکردنێک بێ . هەر بۆیە وانەکانی پێشوو کاریان کرد: Objectـە کە بێ هەیە (دروستکەری بنەڕەتی). بەڵام ئەم Vehicleـە تەنیا Vehicle(String, int)ـی هەیە:

Build.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }
}

class Car extends Vehicle {
    int doors;

    Car(String plate, int doors) {
        this.plate = plate;
        this.doors = doors;
    }
}

public class Build {
    public static void main(String[] args) {
        Car car = new Car("HW-1234", 4);
    }
}

The message points at the Car constructor itself: the hidden super() finds no matching constructor. Fix it by writing super(plate, 5); as the first line, or by giving Vehicle a no-argument constructor too.

پەیامەکە ئاماژە بۆ خودی Car دەکات: super()ـە شاراوەکە هیچ گونجاو نادۆزێتەوە. بە نووسینی super(plate, 5); وەک یەکەم دێڕ چاکی بکەرەوە، یان بێ بدە بە Vehicle.

Left, what you write: Car(String plate, int doors) { this.plate = plate; this.doors = doors; }. An arrow labelled javac leads to what javac compiles: the same constructor with super(); added as its first line (highlighted): super() means call Vehicle() with no arguments. Below, the class Vehicle has only the constructor Vehicle(String plate, int seats): there is no Vehicle(). The error box: constructor Vehicle in class Vehicle cannot be applied to given types; required: String,int; found: no arguments. Fix: make super(plate, 5); the first line of the Car constructor.لای چەپ، ئەوەی تۆ دەینووسیت: Car(String plate, int doors) { this.plate = plate; this.doors = doors; }. تیرێک بە ناوی javac دەچێت بۆ ئەوەی javac کۆمپایلی دەکات: هەمان دروستکەر کە super(); وەک یەکەم دێڕ زیاد کراوە (دیارکراو): super() واتە بانگکردنی Vehicle() بێ ئارگیومێنت. لە خوارەوە، کلاسی Vehicle تەنیا دروستکەری Vehicle(String plate, int seats)ـی هەیە: Vehicle() نییە. سندووقی هەڵەکە: constructor Vehicle in class Vehicle cannot be applied to given types; required: String,int; found: no arguments. چارەسەر: super(plate, 5); بکە بە یەکەم دێڕی دروستکەری Car.
The hidden super() needs a Vehicle() that does not exist⁦super()⁩ـی شاراوە ⁦Vehicle()⁩ـێکی دەوێت کە بوونی نییەCêIT

super(...): the superclass part is built first⁦super(...)⁩: سەرەتا بەشی کلاسی باوک دروست دەکرێت

super(...) must be the first statement⁦super(...)⁩ دەبێت یەکەم ڕستە بێت

Like this(...) (this(...)), super(...) must be the first statement of a constructor, so the superclass part is ready before the subclass uses it:

وەک this(...) (⁦this(...)⁩)، super(...) دەبێت یەکەم بێت، بۆ ئەوەی بەشی پێش ئەوەی بەکاری بهێنێت ئامادە بێت:

Build.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }
}

class Car extends Vehicle {
    int doors;

    Car(String plate, int doors) {
        this.doors = doors;
        super(plate, 5);
    }
}

public class Build {
    public static void main(String[] args) {
        Car car = new Car("HW-1234", 4);
    }
}

super(...): the superclass part is built first⁦super(...)⁩: سەرەتا بەشی کلاسی باوک دروست دەکرێت

The constructor chainزنجیرەی دروستکەرەکان

With more levels, every constructor first calls the one above it, all the way up to Object. The calls climb up; then the constructor bodies run from the top down. So the Vehicle part is ready first, then the Bus part, and the SchoolBus part last:

بە ئاستی زیاتر، هەر سەرەتا بانگی ئەوەی سەرووی خۆی دەکات، تا دەگاتە Object. بانگکردنەکان بۆ سەرەوە دەچن؛ پاشان جەستەی لە سەرەوە بۆ خوارەوە جێبەجێ دەبن. بۆیە سەرەتا بەشی Vehicle ئامادە دەبێت، پاشان بەشی Bus و لە کۆتاییدا بەشی SchoolBus:

Chain.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    Vehicle(String plate) {
        System.out.println("1. Vehicle: plate " + plate);
    }
}

class Bus extends Vehicle {
    Bus(String plate, String route) {
        super(plate);
        System.out.println("2. Bus: route " + route);
    }
}

class SchoolBus extends Bus {
    SchoolBus(String plate) {
        super(plate, "Old town - school");
        System.out.println("3. SchoolBus: ready");
    }
}

public class Chain {
    public static void main(String[] args) {
        new SchoolBus("DH-3307");
    }
}
Animation in seven steps. Three constructors are stacked: Vehicle(String plate) at the top, Bus(String plate, String route) with super(plate); in the middle, SchoolBus(String plate) with super(plate, "Old town - school"); at the bottom, and Object() above them all. 1: new SchoolBus("DH-3307") starts the SchoolBus constructor, whose first line is super(...). 2: it jumps up to Bus, whose first line is super(plate). 3: up to Vehicle, whose hidden super() calls Object() first. 4: the bodies run from the top: Vehicle prints 1. Vehicle: plate DH-3307 and the Vehicle part of the new object is filled. 5: back in Bus, 2. Bus: route Old town - school, and the Bus part is filled. 6: SchoolBus finishes with 3. SchoolBus: ready. Last frame: calls go up, bodies run from the top, the superclass part is ready first.
Animation in seven steps. Three constructors are stacked: Vehicle(String plate) at the top, Bus(String plate, String route) with super(plate); in the middle, SchoolBus(String plate) with super(plate, "Old town - school"); at the bottom, and Object() above them all. 1: new SchoolBus("DH-3307") starts the SchoolBus constructor, whose first line is super(...). 2: it jumps up to Bus, whose first line is super(plate). 3: up to Vehicle, whose hidden super() calls Object() first. 4: the bodies run from the top: Vehicle prints 1. Vehicle: plate DH-3307 and the Vehicle part of the new object is filled. 5: back in Bus, 2. Bus: route Old town - school, and the Bus part is filled. 6: SchoolBus finishes with 3. SchoolBus: ready. Last frame: calls go up, bodies run from the top, the superclass part is ready first.
Calls go up, bodies run from the top downبانگکردن بۆ سەرەوە، جەستەکان لە سەرەوە بۆ خوارەوەCêIT

Overriding: a new version of an inherited methodپێناسەکردنەوەی میتۆد: وەشانێکی نوێی میتۆدێکی بۆماوەگیراو

An inherited method does not always fit. Vehicle's describe() prints the seats, but a car should mention its doors and a bus its route. A subclass overrides a method by writing a method with the same name and the same parameters; for its objects the new version replaces the inherited one. Mark it with @Override:

بۆماوەگیراو هەمیشە گونجاو نییە. describe()ـی Vehicle کورسییەکان چاپ دەکات، بەڵام ئۆتۆمبێل دەبێت دەرگاکانی باس بکات و پاس ڕێگاکەی. بە نووسینی بە هەمان ناو و هەمان ئەو دووپاتە پێناسە دەکاتەوە؛ بۆ ئەو، وەشانە نوێیەکە جێگەی وەشانە بۆماوەگیراوەکە دەگرێتەوە. بە @Override نیشانەی بکە:

Overriding.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }

    void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Car extends Vehicle {
    int doors;

    Car(String plate, int doors) {
        super(plate, 5);
        this.doors = doors;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a car with " + doors + " doors");
    }
}

class Bus extends Vehicle {
    String route;

    Bus(String plate, int seats, String route) {
        super(plate, seats);
        this.route = route;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a bus on " + route);
    }
}

class Motorbike extends Vehicle {
    Motorbike(String plate) {
        super(plate, 2);
    }
}

public class Overriding {
    public static void main(String[] args) {
        Car car = new Car("HW-1234", 4);
        Bus bus = new Bus("DH-5678", 45, "Hawler - Duhok");
        Motorbike bike = new Motorbike("SL-9012");
        car.describe();
        bus.describe();
        bike.describe();
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 23-25

Car's own describe(): same name, same (empty) parameter list.

describe()ـی خودی Car: هەمان ناو و هەمان لیستی (بەتاڵ).

Lineدێڕی 43-45

Motorbike overrides nothing: it keeps Vehicle's describe().

Motorbike هیچ دووپاتە پێناسە ناکاتەوە: describe()ـی Vehicle دەهێڵێتەوە.

The class Vehicle with void describe(), which prints plate: seats seats. Below it, Car and Motorbike. In Car the inherited void describe() is struck through (replaced) and @Override void describe() is highlighted; car.describe(); points to it and prints HW-1234: a car with 4 doors. Motorbike keeps the inherited void describe() (used as it is); bike.describe(); prints SL-9012: 2 seats.کلاسی Vehicle بە void describe() کە plate: seats seats چاپ دەکات. لە خوارییەوە Car و Motorbike. لە Carـدا void describe()ـی بۆماوەگیراو خەتی بەسەردا هاتووە (گۆڕدراوە) و @Override void describe() دیار کراوە؛ car.describe(); ئاماژەی بۆ دەکات و HW-1234: a car with 4 doors چاپ دەکات. Motorbike هەمان void describe()ـی بۆماوەگیراو دەهێڵێتەوە (وەک خۆی)؛ bike.describe(); SL-9012: 2 seats چاپ دەکات.
Car overrides describe(); Motorbike keeps Vehicle'sCar ⁦describe()⁩ دووپاتە پێناسە دەکاتەوە؛ Motorbike هی Vehicle دەهێڵێتەوەCêIT
Definitionپێناسە

Writing a new version of an inherited in a , with the same name. In Dart the line above it is marked @override; the new version can still call the old one with super.

نووسینی وەشانێکی نوێی بۆماوەگیراو لە ، بە هەمان ناو. لە دێڕی سەرەوەی بە @override نیشانە دەکرێت؛ وەشانە نوێیەکە هێشتا دەتوانێت بە super بانگی وەشانە کۆنەکە بکات.

Overriding: a new version of an inherited methodپێناسەکردنەوەی میتۆد: وەشانێکی نوێی میتۆدێکی بۆماوەگیراو

super.method(): reuse the old version⁦super.method()⁩: وەشانە کۆنەکە بەکار بهێنەوە

An override does not have to start from nothing. Inside it, super.describe() runs the superclass's version, and the override adds its own lines after it:

پێویست ناکات لە سفرەوە دەست پێ بکات. لە ناویدا، super.describe() وەشانەکەی جێبەجێ دەکات و وەشانە نوێیەکە دێڕەکانی خۆی لە دوایەوە زیاد دەکات:

SuperCall.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }

    void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Car extends Vehicle {
    int doors;

    Car(String plate, int doors) {
        super(plate, 5);
        this.doors = doors;
    }

    @Override
    void describe() {
        super.describe();                               // Vehicle's version first
        System.out.println("  and " + doors + " doors");  // then the car's extra line
    }
}

public class SuperCall {
    public static void main(String[] args) {
        Car car = new Car("HW-1234", 4);
        car.describe();
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 25

Runs Vehicle's describe() for this object.

describe()ـی Vehicle بۆ ئەم جێبەجێ دەکات.

Two code panels. Vehicle's version: void describe() { System.out.println(plate + ": " + seats + " seats"); }. Car's override: @Override void describe() { super.describe(); System.out.println(" and " + doors + " doors"); }. An arrow marked 1 goes from super.describe(); up to the println of Vehicle. The output panel shows HW-1234: 5 seats (1: from super.describe()) and and 4 doors (2: Car's own line).دوو پانێڵی کۆد. وەشانەکەی Vehicle: void describe() { System.out.println(plate + ": " + seats + " seats"); }. پێناسەکردنەوەکەی Car: @Override void describe() { super.describe(); System.out.println(" and " + doors + " doors"); }. تیرێک بە ژمارە 1 لە super.describe();ـەوە بۆ printlnـی Vehicle دەچێت. پانێڵی دەرخستە HW-1234: 5 seats (1: لە super.describe()ـەوە) و and 4 doors (2: دێڕەکەی Car خۆی) پیشان دەدات.
First the superclass's lines, then the subclass'sسەرەتا دێڕەکانی ، پاشان هی CêIT

Overriding: a new version of an inherited methodپێناسەکردنەوەی میتۆد: وەشانێکی نوێی میتۆدێکی بۆماوەگیراو

@Override: let javac check you⁦@Override⁩: با javac بتپشکنێت

@Override is an annotation, a note for javac: it asks javac to make sure that the method really overrides an inherited one. A typo in the name then stops the compile at once:

@Override تێبینییەکە بۆ javac (annotation): داوا لە javac دەکات دڵنیا ببێتەوە کە ئەو بە ڕاستی بۆماوەگیراو دووپاتە پێناسە دەکاتەوە. ئەوکات هەڵەیەکی نووسین لە ناوەکەدا دەستبەجێ کۆمپایلکردن دەوەستێنێت:

Typo.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }

    void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Car extends Vehicle {
    int doors;

    Car(String plate, int doors) {
        super(plate, 5);
        this.doors = doors;
    }

    @Override
    void descibe() {
        System.out.println(plate + ": a car with " + doors + " doors");
    }
}

public class Typo {
    public static void main(String[] args) {
        Car car = new Car("HW-1234", 4);
        car.describe();
    }
}

Without @Override the same typo compiles without a warning: descibe() becomes a new, separate method, and car.describe() still runs Vehicle's old version:

بەبێ @Override هەمان هەڵەی نووسین بەبێ ئاگادارکردنەوە کۆمپایل دەبێت: descibe() دەبێتە نوێی جیاواز و car.describe() هێشتا وەشانە کۆنەکەی Vehicle جێبەجێ دەکات:

The same program without @Overrideهەمان بەرنامە بەبێ ⁦@Override⁩
$ java Typo
HW-1234: 5 seats
Left, without @Override: void descibe() { ... } with the typo highlighted; a typo makes a NEW method called descibe; javac gives no error and no warning; car.describe(); prints HW-1234: 5 seats: Vehicle's old version ran, a silent bug (cross). Right, with @Override: @Override above the same line; error: method does not override or implement a method from a supertype; javac finds the typo at once; fix the name: describe() (tick).لای چەپ، بەبێ @Override: void descibe() { ... } کە هەڵە نووسینەکە دیار کراوە؛ هەڵەی نووسین میتۆدێکی نوێ بە ناوی descibe دروست دەکات؛ javac نە هەڵە دەدات و نە ئاگادارکردنەوە؛ car.describe(); HW-1234: 5 seats چاپ دەکات: وەشانە کۆنەکەی Vehicle جێبەجێ بوو، باگێکی بێدەنگ (✗). لای ڕاست، بە @Override: @Override لە سەرووی هەمان دێڕەوە؛ error: method does not override or implement a method from a supertype؛ javac دەستبەجێ هەڵە نووسینەکە دەدۆزێتەوە؛ ناوەکە چاک بکە: describe() (✓).
@Override turns a silent bug into a compile error⁦@Override⁩ بێدەنگ دەکات بە CêIT

So write @Override on every override. The same check caught a tostring() typo in Printing an object.

بۆیە لە سەرووی هەموو @Override بنووسە. هەمان پشکنین هەڵەی نووسینی tostring()ـی لە چاپکردنی ئۆبجێکتێک گرت.

Overriding: a new version of an inherited methodپێناسەکردنەوەی میتۆد: وەشانێکی نوێی میتۆدێکی بۆماوەگیراو

The rules for overridingیاساکانی پێناسەکردنەوەی میتۆد

javac checks every override against these rules; each broken rule has its own message:

javac هەموو بەپێی ئەم یاسایانە دەپشکنێت؛ هەر یاسایەکی شکاو پەیامی خۆی هەیە:

RuleیاساIf you break itئەگەر بیشکێنیت
The same name and the same parameter typesهەمان ناو و هەمان جۆری It is a new method (an overload), not an override نوێیە ()، نەک
The same return type (or a subclass of it)هەمان (یان ئەو)return type String is not compatible with void
The same access or wider: public stays publicهەمان دەستڕاگەیشتن یان فراوانتر: public هەر public دەمێنێتەوەattempting to assign weaker access privileges; was public
The inherited method is not final بۆماوەگیراوەکە final نییەoverridden method is final
Only instance methods are overriddenتەنیا دووپاتە پێناسە دەکرێنەوەprivate methods are not inherited; a static method with the same signature only hides the other one privateـەکان بە نادرێن؛ static بە هەمان تەنیا ئەوی تر دەشارێتەوە
Weaker.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;

    public void describe() {
        System.out.println("Vehicle " + plate);
    }
}

class Car extends Vehicle {
    void describe() {
        System.out.println("Car " + plate);
    }
}

public class Weaker {
    public static void main(String[] args) {
        new Car().describe();
    }
}

Overriding or overloading?پێناسەکردنەوەی میتۆد یان فرەپێناسەکردنی میتۆد؟

The two words sound alike but mean different things. Overloading (One name, several methods) gives one class several methods with the same name and different parameters; javac picks one from the arguments. Overriding gives a subclass a method with the same name and parameters as an inherited one; the object picks it while the program runs. One class can do both:

ئەم دوو وشەیە لە یەک دەچن، بەڵام واتای جیاوازیان هەیە. (یەک ناو، چەند میتۆدێک) چەند بە هەمان ناو و جیاواز دەدات بە یەک ؛ javac بەپێی یەکێکیان هەڵدەبژێرێت. بە هەمان ناو و هەمان بۆماوەگیراو دەدات بە ؛ لە کاتی کارکردنی هەڵیدەبژێرێت. یەک دەتوانێت هەردووکیان بکات:

Both.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }

    void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Bus extends Vehicle {
    String route;

    Bus(String plate, int seats, String route) {
        super(plate, seats);
        this.route = route;
    }

    @Override
    void describe() {                          // overriding: same signature
        System.out.println(plate + ": a bus on " + route);
    }

    void describe(int passengers) {            // overloading: another parameter list
        System.out.println(plate + ": " + (seats - passengers) + " seats free");
    }
}

public class Both {
    public static void main(String[] args) {
        Bus bus = new Bus("DH-5678", 45, "Hawler - Duhok");
        bus.describe();
        bus.describe(38);
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 10

Overriding: the same signature as in Vehicle.

: هەمان وەک لە Vehicleـدا.

Lineدێڕی 28

Overloading: the same name with another parameter list.

: هەمان ناو بە لیستێکی تری .

CompareبەراوردOverloadingOverriding
Whereلە کوێThe same class (or an inherited name)هەمان (یان ناوێکی بۆماوەگیراو)A subclass
ParametersDifferentجیاوازThe sameهەمان
Who choosesکێ هەڵدەبژێرێتjavac, from the argumentsjavac، بەپێی The object, while the program runs، لە کاتی کارکردنی
@Override@OverrideNoنەخێرYesبەڵێ
Left, overriding: Vehicle with void describe() and below it Bus with @Override void describe(): same name and same parameters, in a subclass; the object decides, while the program runs. Right, overloading: one class Bus with void describe() and void describe(int passengers): same name, different parameters; javac decides from the arguments. Below: bus.describe(); prints DH-5678: a bus on Hawler - Duhok and bus.describe(38); prints DH-5678: 7 seats free.لای چەپ، پێناسەکردنەوەی میتۆد: Vehicle بە void describe() و لە خوارییەوە Bus بە @Override void describe(): هەمان ناو و هەمان پارامیتەرەکان، لە کلاسی منداڵێکدا؛ ئۆبجێکتەکە بڕیار دەدات، لە کاتی جێبەجێکردندا. لای ڕاست، فرەپێناسەکردنی میتۆد: یەک کلاسی Bus بە void describe() و void describe(int passengers): هەمان ناو، پارامیتەرەکانی جیاواز؛ javac بەپێی ئارگیومێنتەکان بڕیار دەدات. لە خوارەوە: bus.describe(); DH-5678: a bus on Hawler - Duhok چاپ دەکات و bus.describe(38); DH-5678: 7 seats free.
Overriding: same signature, a subclass · Overloading: other parameters: هەمان ، · : ترCêIT

Polymorphism: one call, many formsفرەشێوەیی: یەک بانگکردن، چەندین شێوە

A variable of a superclass type may refer to an object of any subclass, because a car is a vehicle: Vehicle v = new Car(...). When you call v.describe(), Java runs the version of the object that v refers to at that moment, not the version of the variable's type. This is polymorphism ('many forms'); the choice made while the program runs is called dynamic dispatch:

بە جۆری دەتوانێت ئاماژە بۆ هەر بکات، چونکە ئۆتۆمبێل جۆرێکە لە ئامرازی گواستنەوە: Vehicle v = new Car(...). کاتێک v.describe() بانگ دەکەیت، Java وەشانەکەی ئەو جێبەجێ دەکات کە v لەو ساتەدا ئاماژەی بۆ دەکات، نەک وەشانەکەی جۆری . ئەمە ـە («چەندین شێوە»)؛ ئەو هەڵبژاردنەی لە کاتی کارکردنی دەکرێت پێی دەوترێت dynamic dispatch:

Dispatch.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }

    void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Car extends Vehicle {
    int doors;

    Car(String plate, int doors) {
        super(plate, 5);
        this.doors = doors;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a car with " + doors + " doors");
    }
}

class Bus extends Vehicle {
    String route;

    Bus(String plate, int seats, String route) {
        super(plate, seats);
        this.route = route;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a bus on " + route);
    }
}

class Motorbike extends Vehicle {
    Motorbike(String plate) {
        super(plate, 2);
    }
}

public class Dispatch {
    public static void main(String[] args) {
        Vehicle v = new Car("HW-1234", 4);
        v.describe();

        v = new Bus("DH-5678", 45, "Hawler - Duhok");
        v.describe();

        v = new Motorbike("SL-9012");
        v.describe();
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 51

A Vehicle variable, a Car object.

Vehicle، Car.

Lineدێڕی 54

The same variable now refers to a Bus.

هەمان ئێستا ئاماژە بۆ Busـێک دەکات.

Animation in seven steps. On the left the six lines of Dispatch.java and the variable v of type Vehicle; on the right a Car object, a Bus object and a Motorbike object appear one by one. 1: Vehicle v = new Car("HW-1234", 4); v refers to the Car object. 2: v.describe(); runs Car's own describe() and prints HW-1234: a car with 4 doors. 3: v = new Bus(...); the arrow moves to the Bus object. 4: v.describe(); runs Bus's describe(): DH-5678: a bus on Hawler - Duhok. 5: v = new Motorbike("SL-9012"); 6: v.describe(); Motorbike has no describe() of its own, so the version from Vehicle runs: SL-9012: 2 seats. Last frame: one variable, three objects, the object chooses the method.
Animation in seven steps. On the left the six lines of Dispatch.java and the variable v of type Vehicle; on the right a Car object, a Bus object and a Motorbike object appear one by one. 1: Vehicle v = new Car("HW-1234", 4); v refers to the Car object. 2: v.describe(); runs Car's own describe() and prints HW-1234: a car with 4 doors. 3: v = new Bus(...); the arrow moves to the Bus object. 4: v.describe(); runs Bus's describe(): DH-5678: a bus on Hawler - Duhok. 5: v = new Motorbike("SL-9012"); 6: v.describe(); Motorbike has no describe() of its own, so the version from Vehicle runs: SL-9012: 2 seats. Last frame: one variable, three objects, the object chooses the method.
The object, not the variable, chooses the method، نەک ، هەڵدەبژێرێتCêIT
Definitionپێناسە

One call that behaves differently for different kinds of . A variable of a type can hold an object of any ; when you call a method that the subclass overrides, Java runs the real object's version, so shape.area() works for a circle and for a square.

یەک بانگکردن کە بۆ جیاواز بە شێوەی جیاواز کار دەکات. بە جۆری دەتوانێت ئۆبجێکتی هەر هەڵبگرێت؛ کاتێک بانگ دەکەیت کە لە دووپاتە پێناسە کراوە، Java وەشانەکەی ئۆبجێکتە ڕاستەقینەکە جێبەجێ دەکات، بۆیە shape.area() بۆ بازنە و بۆ چوارگۆشە کار دەکات.

Polymorphism: one call, many formsفرەشێوەیی: یەک بانگکردن، چەندین شێوە

What may I call? Which version runs?چی دەتوانم بانگ بکەم؟ کام وەشان جێبەجێ دەبێت؟

Two separate checks happen. javac looks only at the variable's type: a Vehicle has no route, so v.route does not compile, even though v holds a Bus. When the program runs, the JVM looks at the object and runs its version of the method:

دوو پشکنینی جیاواز ڕوو دەدەن. javac تەنیا سەیری جۆری دەکات: Vehicle هیچ routeـێکی نییە، بۆیە v.route کۆمپایل نابێت، هەرچەندە v Bus هەڵدەگرێت. کاتێک کار دەکات، سەیری دەکات و وەشانەکەی ئەو لە جێبەجێ دەکات:

NoRoute.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }

    void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Car extends Vehicle {
    int doors;

    Car(String plate, int doors) {
        super(plate, 5);
        this.doors = doors;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a car with " + doors + " doors");
    }
}

class Bus extends Vehicle {
    String route;

    Bus(String plate, int seats, String route) {
        super(plate, seats);
        this.route = route;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a bus on " + route);
    }
}

class Motorbike extends Vehicle {
    Motorbike(String plate) {
        super(plate, 2);
    }
}

public class NoRoute {
    public static void main(String[] args) {
        Vehicle v = new Bus("DH-5678", 45, "Hawler - Duhok");
        v.describe();
        System.out.println(v.route);
    }
}
The line Vehicle v = new Bus("DH-5678", 45, "Hawler - Duhok"); with Vehicle and new Bus(...) highlighted. Left, 1. javac checks the variable type, Vehicle: does Vehicle have this member? v.describe() and v.plate are ticked; v.route is crossed with cannot find symbol: Vehicle has no route. Right, 2. java runs the object's version, Bus: v.describe() leads to Bus.describe() and prints DH-5678: a bus on Hawler - Duhok. Bottom: the variable type decides what you may call; the object decides which version runs.دێڕی Vehicle v = new Bus("DH-5678", 45, "Hawler - Duhok"); کە Vehicle و new Bus(...) دیار کراون. لای چەپ، 1. javac جۆری گۆڕاوەکە دەپشکنێت، Vehicle: ئایا Vehicle ئەم ئەندامەی هەیە؟ v.describe() و v.plate نیشانەی ✓یان هەیە؛ v.route بە ✗ و cannot find symbol: Vehicle هیچ routeـێکی نییە. لای ڕاست، 2. java وەشانەکەی ئۆبجێکتەکە جێبەجێ دەکات، Bus: v.describe() دەگاتە Bus.describe() و DH-5678: a bus on Hawler - Duhok چاپ دەکات. لە خوارەوە: جۆری گۆڕاوەکە دیاری دەکات چی بانگ بکرێت؛ ئۆبجێکتەکە دیاری دەکات کام وەشان جێبەجێ بێت.
javac checks the variable; java follows the objectjavac دەپشکنێت؛ java بەدوای دەچێتCêIT

Polymorphism: one call, many formsفرەشێوەیی: یەک بانگکردن، چەندین شێوە

A list of vehiclesلیستێک لە Vehicle

Polymorphism pays off with lists. An ArrayList<Vehicle> (Creating an ArrayList) holds cars, buses and motorbikes together, one for-each loop calls describe() on each, and every object answers in its own way. A method with a Vehicle parameter accepts every kind of vehicle as well:

لەگەڵ سوودی خۆی پیشان دەدات. ArrayList<Vehicle> (دروستکردنی ArrayList) ئۆتۆمبێل، پاس و ماتۆڕسکیل پێکەوە هەڵدەگرێت، یەک بانگی describe() لەسەر هەر یەکێکیان دەکات و هەموو بە شێوەی خۆی وەڵام دەداتەوە. بە Vehicleـیش هەموو جۆرە ئامرازێکی گواستنەوە وەردەگرێت:

Fleet.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
import java.util.ArrayList;

class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }

    void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Car extends Vehicle {
    int doors;

    Car(String plate, int doors) {
        super(plate, 5);
        this.doors = doors;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a car with " + doors + " doors");
    }
}

class Bus extends Vehicle {
    String route;

    Bus(String plate, int seats, String route) {
        super(plate, seats);
        this.route = route;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a bus on " + route);
    }
}

class Motorbike extends Vehicle {
    Motorbike(String plate) {
        super(plate, 2);
    }
}

public class Fleet {
    static void inspect(Vehicle v) {
        System.out.print("Inspecting ");
        v.describe();
    }

    public static void main(String[] args) {
        ArrayList<Vehicle> fleet = new ArrayList<>();
        fleet.add(new Car("HW-1234", 4));
        fleet.add(new Bus("DH-5678", 45, "Hawler - Duhok"));
        fleet.add(new Motorbike("SL-9012"));

        int seats = 0;
        for (Vehicle v : fleet) {
            v.describe();
            seats = seats + v.seats;
        }
        System.out.println(fleet.size() + " vehicles, " + seats + " seats");
        inspect(fleet.get(1));
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 64-66

One loop for every kind of vehicle; seats is inherited, so every element has it.

یەک بۆ هەموو جۆرە ئامرازێک؛ seats بۆماوەگیراوە، بۆیە هەموو توخمێک هەیەتی.

The variable fleet, an ArrayList<Vehicle>, points to a list with slots 0, 1 and 2. Slot 0 refers to a Car object (plate "HW-1234", doors 4), slot 1 to a Bus object (plate "DH-5678", route "Hawler - Duhok"), slot 2 to a Motorbike object (plate "SL-9012"), each with a small picture. Below, the loop for (Vehicle v : fleet) { v.describe(); } and its output: HW-1234: a car with 4 doors, DH-5678: a bus on Hawler - Duhok, SL-9012: 2 seats. Note: one loop, three different describe().گۆڕاوی fleet، کە ArrayList<Vehicle>ـە، ئاماژە بۆ لیستێک دەکات بە شوێنی 0 و 1 و 2. شوێنی 0 ئاماژە بۆ ئۆبجێکتێکی Car دەکات (plate "HW-1234" و doors 4)، شوێنی 1 بۆ ئۆبجێکتێکی Bus (plate "DH-5678" و route "Hawler - Duhok") و شوێنی 2 بۆ ئۆبجێکتێکی Motorbike (plate "SL-9012")، هەر یەکەیان بە وێنەیەکی بچووکەوە. لە خوارەوە خولەکە (for (Vehicle v : fleet) { v.describe(); }) و دەرخستەکەی: HW-1234: a car with 4 doors و DH-5678: a bus on Hawler - Duhok و SL-9012: 2 seats. تێبینی: یەک خول، سێ describe()ـی جیاواز.
One list, one loop, three kinds of vehicleیەک ، یەک ، سێ جۆر ئامرازCêIT

Casting between superclass and subclassگۆڕینی جۆر لە نێوان کلاسی باوک و کلاسی منداڵ

Going up is automatic: every Car is a Vehicle, so Vehicle v = car; needs nothing (an upcast). Going down needs a cast in brackets, as with numbers (Narrowing): Car c = (Car) v; (a downcast). Without the cast javac stops with incompatible types: Vehicle cannot be converted to Car. With a cast, javac trusts you, and the JVM checks the real object while the program runs:

گۆڕین بۆ سەرەوە خۆکارە: هەموو Carـێک Vehicleـە، بۆیە Vehicle v = car; پێویستی بە هیچ نییە (upcast). گۆڕین بۆ خوارەوە پێویستی بە نووسینی جۆرەکە لە ناو کەوانەدایە، وەک ژمارەکان (تەسککردنەوەی جۆر): Car c = (Car) v; (downcast). بەبێ ئەم گۆڕینە javac بە incompatible types: Vehicle cannot be converted to Car دەوەستێت. بە گۆڕینەکە، javac بڕوات پێ دەکات و لە کاتی کارکردنی ڕاستەقینەکە دەپشکنێت:

Cast.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }

    void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Car extends Vehicle {
    int doors;

    Car(String plate, int doors) {
        super(plate, 5);
        this.doors = doors;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a car with " + doors + " doors");
    }
}

class Bus extends Vehicle {
    String route;

    Bus(String plate, int seats, String route) {
        super(plate, seats);
        this.route = route;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a bus on " + route);
    }
}

class Motorbike extends Vehicle {
    Motorbike(String plate) {
        super(plate, 2);
    }
}

public class Cast {
    public static void main(String[] args) {
        Vehicle v = new Car("HW-1234", 4);   // upcast: automatic
        Car c = (Car) v;                       // downcast: you write (Car)
        System.out.println("Doors: " + c.doors);

        Bus b = (Bus) v;                       // compiles, but v is a Car
        System.out.println(b.route);
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 52

Correct: v really holds a Car.

دروستە: v بە ڕاستی Carـێک هەڵدەگرێت.

Lineدێڕی 55

Compiles, but the object is a Car, not a Bus.

کۆمپایل دەبێت، بەڵام Carـە، نەک Bus.

Four rows. Up, every Car is a Vehicle: Vehicle v = new Car("HW-1234", 4); automatic (tick). Down, you write the type: Car c = (Car) v; v really holds a Car (tick). Down without (Car), compile-time error: Car c = v; incompatible types: Vehicle cannot be converted to Car (cross). Down to the wrong class, run-time error: Bus b = (Bus) v; ClassCastException: class Car cannot be cast to class Bus (cross). On the right, the variables v (Vehicle) and c (Car) point to one Car object, while a dashed arrow from b (Bus) is crossed: a Bus variable cannot point to a Car.چوار ڕیز. بۆ سەرەوە، هەموو Carـێک Vehicleـە: Vehicle v = new Car("HW-1234", 4); خۆکار (✓). بۆ خوارەوە، جۆرەکە خۆت دەنووسیت: Car c = (Car) v; v بە ڕاستی Carـێک هەڵدەگرێت (✓). بۆ خوارەوە بەبێ (Car)، هەڵەی کاتی کۆمپایلکردن: Car c = v; incompatible types: Vehicle cannot be converted to Car (✗). بۆ کلاسێکی هەڵە، هەڵەی کاتی جێبەجێکردن: Bus b = (Bus) v; ClassCastException: class Car cannot be cast to class Bus (✗). لای ڕاست، گۆڕاوەکانی v (Vehicle) و c (Car) ئاماژە بۆ یەک ئۆبجێکتی Car دەکەن، بەڵام تیرێکی پچڕپچڕ لە b (Bus)ـەوە خەتی بەسەردا هاتووە: گۆڕاوێکی Bus ناتوانێت ئاماژە بۆ Car بکات.
Up is free; down needs a cast and the right objectبۆ سەرەوە خۆکارە؛ بۆ خوارەوە گۆڕین و دروستی دەوێتCêIT

Casting between superclass and subclassگۆڕینی جۆر لە نێوان کلاسی باوک و کلاسی منداڵ

instanceof: check before you castinstanceof: پێش گۆڕینی جۆر بیپشکنە

v instanceof Bus is true when the object in v is a Bus (or a subclass of Bus), and false otherwise, also when v is null. Test first, then cast, and a ClassCastException cannot happen:

v instanceof Bus کاتێک trueـە کە ناو v Bus بێت (یان Bus)، ئەگینا falseـە، تەنانەت کاتێک v null بێت. سەرەتا تاقی بکەرەوە، پاشان جۆرەکە بگۆڕە، ئیتر ClassCastException ڕوو نادات:

Check.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
import java.util.ArrayList;

class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }

    void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Car extends Vehicle {
    int doors;

    Car(String plate, int doors) {
        super(plate, 5);
        this.doors = doors;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a car with " + doors + " doors");
    }
}

class Bus extends Vehicle {
    String route;

    Bus(String plate, int seats, String route) {
        super(plate, seats);
        this.route = route;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a bus on " + route);
    }
}

class Motorbike extends Vehicle {
    Motorbike(String plate) {
        super(plate, 2);
    }
}

public class Check {
    public static void main(String[] args) {
        ArrayList<Vehicle> fleet = new ArrayList<>();
        fleet.add(new Car("HW-1234", 4));
        fleet.add(new Bus("DH-5678", 45, "Hawler - Duhok"));
        fleet.add(new Motorbike("SL-9012"));

        for (Vehicle v : fleet) {
            if (v instanceof Bus) {
                Bus b = (Bus) v;
                System.out.println(b.plate + " runs on " + b.route);
            } else {
                System.out.println(v.plate + " is not a bus");
            }
        }

        Vehicle none = null;
        System.out.println(none instanceof Vehicle);
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 59-60

The cast runs only when the test said yes.

تەنیا کاتێک جێبەجێ دەبێت کە تاقیکردنەوەکە وتی بەڵێ.

Lineدێڕی 68

null is never an instance of anything.

null هەرگیز نموونەی هیچ نییە.

A flowchart. A car (HW-1234), a bus (DH-5678) and a motorbike (SL-9012) enter a diamond that asks v instanceof Bus ?. Yes: Bus b = (Bus) v; println(b.plate + " runs on " + b.route); prints DH-5678 runs on Hawler - Duhok. No: println(v.plate + " is not a bus"); prints HW-1234 is not a bus and SL-9012 is not a bus. Note: no cast happens unless the answer is yes, so no ClassCastException.هێڵکارییەکی ڕەوت. ئۆتۆمبێلێک (HW-1234)، پاسێک (DH-5678) و ماتۆڕسکیلێک (SL-9012) دەچنە ناو چوارگۆشەیەکی لاری بڕیار کە دەپرسێت v instanceof Bus ?. بەڵێ: Bus b = (Bus) v; println(b.plate + " runs on " + b.route); چاپ دەکات DH-5678 runs on Hawler - Duhok. نەخێر: println(v.plate + " is not a bus"); چاپ دەکات HW-1234 is not a bus و SL-9012 is not a bus. تێبینی: تا وەڵامەکە بەڵێ نەبێت جۆر ناگۆڕدرێت، بۆیە ClassCastException ڕوو نادات.
Test with instanceof, then castبە instanceof تاقی بکەرەوە، پاشان جۆرەکە بگۆڕەCêIT

Object: the root of every classObject: ڕەگی هەموو کلاسێک

A class without extends still has a superclass: java.lang.Object. class Vehicle { } means class Vehicle extends Object { }. So every chain of superclasses ends at Object, and every object has Object's methods, such as toString(), equals(), hashCode() and getClass(). getClass() can even show the chain:

کە extendsـی نەبێت هێشتا هەیە: java.lang.Object. class Vehicle { } واتە class Vehicle extends Object { }. بۆیە هەموو زنجیرەیەکی بە Object کۆتایی دێت و هەموو Objectـی هەیە، وەک toString()، equals()، hashCode() و getClass(). getClass() تەنانەت دەتوانێت زنجیرەکە پیشان بدات:

Root.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }

    void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Car extends Vehicle {
    int doors;

    Car(String plate, int doors) {
        super(plate, 5);
        this.doors = doors;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a car with " + doors + " doors");
    }
}

class Bus extends Vehicle {
    String route;

    Bus(String plate, int seats, String route) {
        super(plate, seats);
        this.route = route;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a bus on " + route);
    }
}

class Motorbike extends Vehicle {
    Motorbike(String plate) {
        super(plate, 2);
    }
}

public class Root {
    public static void main(String[] args) {
        Car car = new Car("HW-1234", 4);
        System.out.println(car.getClass().getSimpleName());
        System.out.println(car.getClass().getSuperclass());
        System.out.println(Vehicle.class.getSuperclass());
        System.out.println("Hawler".getClass().getSuperclass());
        System.out.println(Object.class.getSuperclass());

        Object thing = car;                    // every object is an Object
        System.out.println(thing instanceof Vehicle);
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 54

Vehicle has no extends, so its superclass is Object.

Vehicle هیچ extendsـی نییە، بۆیە Objectـە.

Lineدێڕی 56

Object is the root: it has no superclass, so null.

Object ڕەگەکەیە: هیچ نییە، بۆیە null.

A class tree with Object at the top, from java.lang, listing String toString(), boolean equals(Object o), int hashCode() and Class getClass(). Below it Vehicle, String, Student and Scanner; below Vehicle: Car, Bus and Motorbike. Notes: class Vehicle { } means class Vehicle extends Object { }; Object.class.getSuperclass() gives null: Object is the root, with no superclass.دارێکی کلاسەکان کە Object لە سەرەوەیەتی، لە java.langـەوە، بە String toString() و boolean equals(Object o) و int hashCode() و Class getClass(). لە خوارییەوە Vehicle و String و Student و Scanner؛ لە خوار Vehicleـەوە: Car و Bus و Motorbike. تێبینییەکان: class Vehicle { } واتە class Vehicle extends Object { }؛ Object.class.getSuperclass() نرخی null دەدات: Object ڕەگەکەیە و هیچ کلاسی باوکێکی نییە.
Every chain of superclasses ends at Objectهەموو زنجیرەیەکی بە Object کۆتایی دێتCêIT

Object: the root of every classObject: ڕەگی هەموو کلاسێک

toString(): one method for the whole family⁦toString()⁩: یەک میتۆد بۆ هەموو خێزانەکە

toString() comes from Object, which prints the class name and a hash code until a class overrides it (Printing an object). Override it once in Vehicle and every subclass inherits the new version. getClass().getSimpleName() gives the name of the object's real class, so one method writes Car or Bus correctly, and Bus adds its route with super.toString():

toString() لە Objectـەوە دێت کە ناوی و چاپ دەکات، تا ئەوکاتەی دووپاتە پێناسەی بکاتەوە (چاپکردنی ئۆبجێکتێک). یەک جار لە Vehicleـدا دووپاتە پێناسەی بکەرەوە و هەموو وەشانە نوێیەکە وەردەگرێت. getClass().getSimpleName() ناوی ڕاستەقینەکەی دەدات، بۆیە یەک Car یان Bus بە دروستی دەنووسێت و Bus بە super.toString() ڕێگاکەی خۆی زیاد دەکات:

Names.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
import java.util.ArrayList;

class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + " " + plate;
    }
}

class Car extends Vehicle {
    Car(String plate) {
        super(plate, 5);
    }
}

class Bus extends Vehicle {
    String route;

    Bus(String plate, int seats, String route) {
        super(plate, seats);
        this.route = route;
    }

    @Override
    public String toString() {
        return super.toString() + " (" + route + ")";
    }
}

public class Names {
    public static void main(String[] args) {
        ArrayList<Vehicle> fleet = new ArrayList<>();
        fleet.add(new Car("HW-1234"));
        fleet.add(new Bus("DH-5678", 45, "Hawler - Duhok"));
        System.out.println(fleet.get(0));
        System.out.println(fleet);
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 14

getClass() is the real class of the object, so this one line works for every subclass.

getClass() ڕاستەقینەکەی ، بۆیە ئەم یەک دێڕە بۆ هەموو کار دەکات.

Lineدێڕی 34

Bus's override reuses Vehicle's text.

وەشانە نوێیەکەی Bus دەقەکەی Vehicle دووپاتە بەکار دەهێنێتەوە.

Object: the root of every classObject: ڕەگی هەموو کلاسێک

equals(): the same plate, the same vehicle⁦equals()⁩: هەمان ژمارە، هەمان ئامراز

== asks whether two references point to the same object (Two variables, one object). Object's equals() does exactly the same, so two Car objects with the plate HW-1234 are not equal, and a list's contains(), which uses equals(), cannot find the second one: that run prints false three times. Override equals(Object) to say what equal means for your class, here the same plate:

== دەپرسێت ئایا دوو ئاماژە بۆ هەمان دەکەن (دوو گۆڕاو، یەک ئۆبجێکت). equals()ـی Object ڕێک هەمان کار دەکات، بۆیە دوو Car بە ژمارەی ⁦HW-1234⁩ یەکسان نین و contains()ـی ، کە equals() بەکار دەهێنێت، دووەمیان نادۆزێتەوە: ئەو جێبەجێکردنە سێ جار false چاپ دەکات. equals(Object) دووپاتە پێناسە بکەرەوە بۆ ئەوەی بڵێیت یەکسان بۆ چی دەگەیەنێت، لێرەدا هەمان ژمارە:

Same.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
import java.util.ArrayList;

class Vehicle {
    String plate;

    Vehicle(String plate) {
        this.plate = plate;
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof Vehicle)) {
            return false;
        }
        Vehicle v = (Vehicle) other;
        return plate.equals(v.plate);
    }

    @Override
    public int hashCode() {
        return plate.hashCode();
    }
}

class Car extends Vehicle {
    Car(String plate) {
        super(plate);
    }
}

public class Same {
    public static void main(String[] args) {
        Car a = new Car("HW-1234");
        Car b = new Car("HW-1234");
        System.out.println(a == b);
        System.out.println(a.equals(b));

        ArrayList<Vehicle> parked = new ArrayList<>();
        parked.add(a);
        System.out.println(parked.contains(b));
    }
}
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 11-19

The parameter is Object; instanceof also returns false for null.

Objectـە؛ instanceof بۆ nullـیش false دەگەڕێنێتەوە.

Lineدێڕی 23-24

Equal vehicles must give equal hash codes.

ئامرازە یەکسانەکان دەبێت هەمان بدەن.

Java's rule: when you override equals, override hashCode too, so that equal objects give equal hash codes. HashMap and HashSet depend on it (Your own class as a key).

یاسای Java: کاتێک equals دووپاتە پێناسە دەکەیتەوە، hashCodeـیش دووپاتە پێناسە بکەرەوە، بۆ ئەوەی یەکسان یەکسان بدەن. HashMap و HashSet پشتی پێ دەبەستن (کلاسی خۆت وەک کلیل).

The variables a and b point to two separate Car objects, both with plate "HW-1234": two objects, the same plate. Four results: a == b is false (two different objects); a.equals(b) is false without your equals (Object's equals uses ==); a.equals(b) is true with your equals (the plates are compared); parked.contains(b) is true (contains() calls equals() for you). Note: override hashCode() together with equals(); equal objects must give equal hash codes.گۆڕاوەکانی a و b ئاماژە بۆ دوو ئۆبجێکتی جیاوازی Car دەکەن، هەردووکیان plate "HW-1234": دوو ئۆبجێکت، هەمان ژمارەی ئۆتۆمبێل. چوار ئەنجام: a == b نرخی false دەدات (دوو ئۆبجێکتی جیاوازن)؛ a.equals(b) بەبێ equalsـی خۆت false دەدات (equalsـی Object == بەکار دەهێنێت)؛ a.equals(b) بە equalsـی خۆت true دەدات (ژمارەی ئۆتۆمبێلەکان بەراورد دەکرێن)؛ parked.contains(b) true دەدات (contains() بۆ تۆ بانگی equals() دەکات). تێبینی: hashCode() لەگەڵ equals() پێکەوە پێناسە بکەرەوە؛ ئۆبجێکتەکانی یەکسان دەبێت هەمان نرخی هاش بدەن.
== compares references; equals compares what you decide== بەراورد دەکات؛ equals ئەوەی تۆ بڕیاری دەدەیتCêIT

Object: the root of every classObject: ڕەگی هەموو کلاسێک

Common mistake: equals(Vehicle)هەڵەی باو: ⁦equals(Vehicle)⁩

final classes and methodsکلاسەکان و میتۆدەکانی final

final closes a door. A final class cannot be extended, and a final method cannot be overridden. Use it when a change in a subclass could break something important. String, Integer and Math are final classes, so nobody can make a fake String. (A final field, which gets its value once, is another use of the same word: final fields.)

final دەرگایەک دادەخات. final extends ناکرێت و final دووپاتە پێناسە ناکرێتەوە. بەکاری بهێنە کاتێک گۆڕانکارییەک لە لەوانەیە شتێکی گرنگ تێک بدات. String، Integer و Math finalـن، بۆیە کەس ناتوانێت Stringـێکی ساختە دروست بکات. ( final، کە تەنیا یەک جار وەردەگرێت، بەکارهێنانێکی تری هەمان وشەیە: تایبەتمەندییەکانی final.)

Electric.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

final class Motorbike extends Vehicle {
}

class ElectricMotorbike extends Motorbike {
}

public class Electric {
    public static void main(String[] args) {
        new ElectricMotorbike().describe();
    }
}

A final method is protected the same way. final String getPlate() in Vehicle cannot be overridden in Car: getPlate() in Car cannot override getPlate() in Vehicle ... overridden method is final.

final بە هەمان شێوە پارێزراوە. final String getPlate() لە Vehicleـدا لە Carـدا دووپاتە پێناسە ناکرێتەوە: getPlate() in Car cannot override getPlate() in Vehicle ... overridden method is final.

Left, a final class: final class Motorbike with a padlock, and a dashed class ElectricMotorbike that tries to extend it (cross): error: cannot inherit from final Motorbike. Right, a final method: Vehicle with final String getPlate() and a padlock, and Car with @Override String getPlate() struck through (cross): error: getPlate() in Car cannot override getPlate() in Vehicle, overridden method is final. Bottom: final classes in the Java API: String, Integer, Math.لای چەپ، کلاسێکی final: final class Motorbike بە قفڵێکەوە و کلاسێکی پچڕپچڕی ElectricMotorbike کە هەوڵ دەدات لێی extends بکات (✗): error: cannot inherit from final Motorbike. لای ڕاست، میتۆدێکی final: Vehicle بە final String getPlate() و قفڵێک و Car بە @Override String getPlate() کە خەتی بەسەردا هاتووە (✗): error: getPlate() in Car cannot override getPlate() in Vehicle، overridden method is final. لە خوارەوە: کلاسەکانی final لە APIـی Javaـدا: String و Integer و Math.
final: no subclass, no overridefinal: نە ، نە CêIT

Common mistakesهەڵە باوەکان

Every message below is the real text from javac or java 17 for the programs of this lesson.

هەموو پەیامێکی خوارەوە دەقی ڕاستەقینەی javac یان ⁦java 17⁩ـە بۆ ئەم وانەیە.

MistakeهەڵەWhat javac or java saysjavac یان java چی دەڵێتFixچارەسەر
Two superclasses: extends Car, Busدوو : extends Car, Bus'{' expectedExtend one class; combine abilities with interfacesلە یەک extends بکە؛ توانا بە کۆ بکەرەوە
A subclass uses a private field of its superclass privateـی بەکار دەهێنێتkm has private access in VehicleUse a public or protected method of the superclass public یان protectedـی بەکار بهێنە
A protected field from another package, not a subclass protected لە ترەوە، بێ ئەوەی بێتplate has protected access in VehicleCall a public method insteadلە جیاتی ئەوە public بانگ بکە
No super(...), and the superclass has no no-argument constructorsuper(...) نییە و بێ نییەconstructor Vehicle in class Vehicle cannot be applied to given types; required: String,int found: no argumentsCall super(plate, 5); as the first statementsuper(plate, 5); وەک یەکەم بانگ بکە
super(...) after another statementsuper(...) دوای ترcall to super must be first statement in constructorMove it to the first lineبیبە بۆ یەکەم دێڕ
A typo in an override, without @Overrideهەڵەی نووسین لە ، بەبێ @OverrideNo error: the old describe() still runsWrite @Override on every overrideلە سەرووی هەموو @Override بنووسە
The same typo with @Overrideهەمان هەڵەی نووسین بە @Overridemethod does not override or implement a method from a supertypeFix the name or the parametersناو یان چاک بکە
An override with less access بە دەستڕاگەیشتنی کەمترdescribe() in Car cannot override describe() in Vehicle; attempting to assign weaker access privileges; was publicKeep the same access, or a wider oneهەمان دەستڕاگەیشتن بهێڵەوە، یان فراوانتر
An override with another return type بە ترdescribe() in Car cannot override describe() in Vehicle; return type String is not compatible with voidUse the same return typeهەمان بەکار بهێنە
Overriding a final methodدووپاتە پێناسەکردنەوەی finalgetPlate() in Car cannot override getPlate() in Vehicle; overridden method is finalUse the method as it is وەک خۆی بەکار بهێنە
Extending a final classextendsکردنی finalcannot inherit from final MotorbikeUse the class as it is, or hold one in a field (has-a) وەک خۆی بەکار بهێنە، یان لە هەڵیبگرە ⁦(has-a)⁩
A subclass member through a superclass variableئەندامێکی لە ڕێگەی cannot find symbol ... location: variable v of type VehicleCheck with instanceof, then castبە instanceof بیپشکنە، پاشان جۆرەکە بگۆڕە
A downcast without the castگۆڕین بۆ خوارەوە بەبێ incompatible types: Vehicle cannot be converted to CarWrite (Car) v(Car) v بنووسە
A downcast to the wrong classگۆڕین بۆ خوارەوە بۆ هەڵەClassCastException: class Car cannot be cast to class BusTest with instanceof firstسەرەتا بە instanceof تاقی بکەرەوە
equals(Vehicle) instead of equals(Object)equals(Vehicle) لە جیاتی equals(Object)No error: contains() returns falseWrite equals(Object) with @Override, and hashCode() tooequals(Object) بە @Override بنووسە، hashCode()ـیش

Practiceڕاهێنان

ExerciseڕاهێنانShapes and their areasشێوەکان و ڕووبەرەکانیان

Write a class Shape with a method double area() that returns 0. Write Circle (a radius) and Square (a side) that extend it and override area(). Put a circle of radius 1, a square of side 2 and a circle of radius 0.5 in an ArrayList<Shape>, print each area with two decimals (Rounding) and the total.

Shape بە double area() بنووسە کە 0 بگەڕێنێتەوە. Circle (نیوەتیرە) و Square (لا) بنووسە کە لێی extends بکەن و area() دووپاتە پێناسە بکەنەوە. بازنەیەک بە نیوەتیرەی 1، چوارگۆشەیەک بە لای 2 و بازنەیەک بە نیوەتیرەی ⁦0.5⁩ بخەرە ناو ArrayList<Shape>، ڕووبەری هەر یەکەیان بە دوو ژمارەی دوای خاڵ (خڕکردنەوە) و کۆکەی گشتی چاپ بکە.

Hintئاماژە

Use Math.PI * radius * radius for the circle.

بۆ بازنە Math.PI * radius * radius بەکار بهێنە.

Show answerوەڵامەکە پیشان بدە
ShapeAreas.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
import java.util.ArrayList;

class Shape {
    double area() {
        return 0;
    }
}

class Circle extends Shape {
    double radius;

    Circle(double radius) {
        this.radius = radius;
    }

    @Override
    double area() {
        return Math.PI * radius * radius;
    }
}

class Square extends Shape {
    double side;

    Square(double side) {
        this.side = side;
    }

    @Override
    double area() {
        return side * side;
    }
}

public class ShapeAreas {
    public static void main(String[] args) {
        ArrayList<Shape> shapes = new ArrayList<>();
        shapes.add(new Circle(1));
        shapes.add(new Square(2));
        shapes.add(new Circle(0.5));

        double total = 0;
        for (Shape s : shapes) {
            double a = Math.round(s.area() * 100) / 100.0;   // two decimals
            System.out.println(s.getClass().getSimpleName() + ": " + a);
            total = total + s.area();
        }
        System.out.println("Total: " + Math.round(total * 100) / 100.0);
    }
}

Shape's area() returns 0, which means nothing for a shape in general. The next lesson fixes this with an abstract class (Abstract classes).

area()ـی Shape 0 دەگەڕێنێتەوە کە بۆ «شێوەیەکی گشتی» هیچ واتایەکی نییە. وانەی داهاتوو بە چاکی دەکات (کلاسە ڕووتەکان).

ExerciseڕاهێنانMonthly payمووچەی مانگانە

An Employee has a name and a monthly salary in Iraqi dinars, and monthlyPay() returns the salary. A Teacher earns 15,000 IQD more for each extra lesson, a Driver 10,000 IQD more for each trip. Override monthlyPay() in both with super.monthlyPay(), and print the pay of Lana (900,000), Soran (1,000,000, 6 extra lessons) and Karwan (700,000, 12 trips) from one ArrayList<Employee>.

Employee ناو و مووچەیەکی مانگانەی بە دیناری عێراقی هەیە و monthlyPay() مووچەکە دەگەڕێنێتەوە. Teacher بۆ هەر وانەیەکی زیادە ⁦15000⁩ دیناری زیاتر وەردەگرێت و Driver بۆ هەر گەشتێک ⁦10000⁩ دیناری زیاتر. monthlyPay() لە هەردووکیاندا بە super.monthlyPay() دووپاتە پێناسە بکەرەوە و مووچەی ⁦Lana (900000)⁩، ⁦Soran (1000000⁩، 6 وانەی زیادە) و ⁦Karwan (700000⁩، ⁦12⁩ گەشت) لە یەک ArrayList<Employee>ـەوە چاپ بکە.

Show answerوەڵامەکە پیشان بدە
Payroll.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
import java.util.ArrayList;

class Employee {
    String name;
    int salary;          // Iraqi dinars per month

    Employee(String name, int salary) {
        this.name = name;
        this.salary = salary;
    }

    int monthlyPay() {
        return salary;
    }
}

class Teacher extends Employee {
    int extraLessons;

    Teacher(String name, int salary, int extraLessons) {
        super(name, salary);
        this.extraLessons = extraLessons;
    }

    @Override
    int monthlyPay() {
        return super.monthlyPay() + extraLessons * 15000;
    }
}

class Driver extends Employee {
    int trips;

    Driver(String name, int salary, int trips) {
        super(name, salary);
        this.trips = trips;
    }

    @Override
    int monthlyPay() {
        return super.monthlyPay() + trips * 10000;
    }
}

public class Payroll {
    public static void main(String[] args) {
        ArrayList<Employee> staff = new ArrayList<>();
        staff.add(new Employee("Lana", 900000));
        staff.add(new Teacher("Soran", 1000000, 6));
        staff.add(new Driver("Karwan", 700000, 12));

        int total = 0;
        for (Employee e : staff) {
            System.out.println(e.name + ": " + e.monthlyPay() + " IQD");
            total = total + e.monthlyPay();
        }
        System.out.println("Total: " + total + " IQD");
    }
}
ExerciseڕاهێنانPredict the outputدەرخستە مەزەندە بکە

With the Vehicle, Car, Bus and Motorbike classes of this lesson, what does this main print? Decide first, then compare with the real run.

بە Vehicle، Car، Bus و Motorbikeـی ئەم وانەیە، ئەم mainـە چی چاپ دەکات؟ سەرەتا بڕیار بدە، پاشان لەگەڵ جێبەجێکردنی ڕاستەقینەدا بەراوردی بکە.

JAVA
Vehicle a = new Bus("DH-5678", 45, "Hawler - Duhok");
Vehicle b = new Motorbike("SL-9012");
a.describe();
b.describe();
System.out.println(a instanceof Car);
System.out.println(b instanceof Vehicle);
System.out.println(a.seats + b.seats);
Show answerوەڵامەکە پیشان بدە
Predict.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }

    void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Car extends Vehicle {
    int doors;

    Car(String plate, int doors) {
        super(plate, 5);
        this.doors = doors;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a car with " + doors + " doors");
    }
}

class Bus extends Vehicle {
    String route;

    Bus(String plate, int seats, String route) {
        super(plate, seats);
        this.route = route;
    }

    @Override
    void describe() {
        System.out.println(plate + ": a bus on " + route);
    }
}

class Motorbike extends Vehicle {
    Motorbike(String plate) {
        super(plate, 2);
    }
}

public class Predict {
    public static void main(String[] args) {
        Vehicle a = new Bus("DH-5678", 45, "Hawler - Duhok");
        Vehicle b = new Motorbike("SL-9012");
        a.describe();
        b.describe();
        System.out.println(a instanceof Car);
        System.out.println(b instanceof Vehicle);
        System.out.println(a.seats + b.seats);
    }
}

a holds a Bus, so Bus's describe() runs; b holds a Motorbike, which uses Vehicle's version. a instanceof Car is false (a Bus is not a Car), b instanceof Vehicle is true, and 45+2=4745 + 2 = 47.

a Busـێک هەڵدەگرێت، بۆیە describe()ـی Bus جێبەجێ دەبێت؛ b Motorbikeـێک هەڵدەگرێت کە وەشانەکەی Vehicle بەکار دەهێنێت. a instanceof Car falseـە (پاس جۆرێک نییە لە ئۆتۆمبێلی سواری)، b instanceof Vehicle trueـە و 45+2=4745 + 2 = 47.

ExerciseڕاهێنانFix the two errorsدوو هەڵەکە چاک بکە

javac rejects this delivery motorbike twice. Read both messages and fix the class Motorbike.

javac دوو جار ئەم ماتۆڕسکیلی گەیاندنە ڕەت دەکاتەوە. هەردوو پەیامەکە بخوێنەرەوە و Motorbike چاک بکە.

Delivery.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }

    public void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Motorbike extends Vehicle {
    boolean deliveryBox;

    Motorbike(String plate, boolean deliveryBox) {
        this.plate = plate;
        this.deliveryBox = deliveryBox;
    }

    void describe() {
        System.out.println(plate + ": delivery box " + deliveryBox);
    }
}

public class Delivery {
    public static void main(String[] args) {
        Vehicle v = new Motorbike("SL-9012", true);
        v.describe();
    }
}
Show answerوەڵامەکە پیشان بدە
Delivery.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
class Vehicle {
    String plate;
    int seats;

    Vehicle(String plate, int seats) {
        this.plate = plate;
        this.seats = seats;
    }

    public void describe() {
        System.out.println(plate + ": " + seats + " seats");
    }
}

class Motorbike extends Vehicle {
    boolean deliveryBox;

    Motorbike(String plate, boolean deliveryBox) {
        super(plate, 2);
        this.deliveryBox = deliveryBox;
    }

    @Override
    public void describe() {
        System.out.println(plate + ": delivery box " + deliveryBox);
    }
}

public class Delivery {
    public static void main(String[] args) {
        Vehicle v = new Motorbike("SL-9012", true);
        v.describe();
    }
}

Error 1: the constructor had no super(...), and Vehicle has no Vehicle(); call super(plate, 2); first. Error 2: describe() was public in Vehicle, so the override must be public too.

هەڵەی 1: super(...)ـی نەبوو و Vehicle هیچ Vehicle()ـێکی نییە؛ سەرەتا super(plate, 2); بانگ بکە. هەڵەی 2: describe() لە Vehicleـدا publicـە، بۆیە وەشانە نوێیەکەش دەبێت public بێت.

ExerciseڕاهێنانEqual studentsقوتابیی یەکسان

Two Student objects are the same student when their student numbers (id) are equal, even if the name is written differently. Override equals and hashCode, then test contains() with a new object for number 2041.

دوو Student هەمان قوتابین کاتێک ژمارەی قوتابییەکەیان (id) یەکسان بێت، تەنانەت ئەگەر ناوەکە بە شێوەیەکی تر نووسرابێت. equals و hashCode دووپاتە پێناسە بکەرەوە، پاشان contains() بە نوێ بۆ ژمارە ⁦2041⁩ تاقی بکەرەوە.

Show answerوەڵامەکە پیشان بدە
Register.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
import java.util.ArrayList;

class Student {
    int id;
    String name;

    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof Student)) {
            return false;
        }
        Student s = (Student) other;
        return id == s.id;
    }

    @Override
    public int hashCode() {
        return Integer.hashCode(id);
    }

    @Override
    public String toString() {
        return id + " " + name;
    }
}

public class Register {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student(2041, "Lana"));
        list.add(new Student(2042, "Rebin"));

        Student again = new Student(2041, "Lana K.");
        System.out.println(list.contains(again));
        System.out.println(again.equals(list.get(1)));
        System.out.println(list);
    }
}
Exerciseڕاهێنانis-a or has-a?⁦is-a⁩ یان ⁦has-a⁩؟

For each pair, choose extends (is-a) or a field (has-a): (a) Laptop and Computer, (b) Car and Engine, (c) Teacher and Employee, (d) Classroom and Student, (e) Square and Shape.

بۆ هەر جووتێک، extends ⁦(is-a)⁩ یان ⁦(has-a)⁩ هەڵبژێرە: ⁦(a) Laptop⁩ و Computer، ⁦(b) Car⁩ و Engine، ⁦(c) Teacher⁩ و Employee، ⁦(d) Classroom⁩ و Student، ⁦(e) Square⁩ و Shape.

Show answerوەڵامەکە پیشان بدە
PairجووتAnswerوەڵامWhyبۆچی
(a)class Laptop extends Computera laptop is a computer جۆرێکە لە
(b)Engine engine;a car has an engineئۆتۆمبێل بزوێنەرێکی هەیە
(c)class Teacher extends Employeea teacher is an employeeمامۆستا جۆرێکە لە فەرمانبەر
(d)ArrayList<Student> students;a classroom has studentsپۆل قوتابیی هەیە
(e)class Square extends Shapea square is a shapeچوارگۆشە جۆرێکە لە شێوە

Summaryکورتە

  • class Car extends Vehicle: the subclass Car inherits the members of the superclass Vehicle; use it only when "a Car is a Vehicle" is true (otherwise a field: has-a).
  • One direct superclass, as many levels as needed; every chain ends at Object.
  • private members and constructors are not inherited; protected opens a member to subclasses.
  • super(...) is the first statement of a subclass constructor; without it javac adds super().
  • Overriding: the same signature in a subclass, marked @Override; super.method() reuses the old version. Overloading is different: other parameters.
  • Polymorphism: a superclass variable may hold any subclass object; javac checks the variable type, the object's version runs.
  • Upcasts are automatic; downcasts need (Type) and an instanceof check, or a ClassCastException comes.
  • Override toString(), and equals(Object) together with hashCode(); final stops extending and overriding.
  • class Car extends Vehicle: Car ئەندامەکانی Vehicle وەردەگرێت؛ تەنیا کاتێک بەکاری بهێنە کە «Car جۆرێکە لە Vehicle» ڕاست بێت (ئەگینا : ⁦has-a⁩).
  • یەک ڕاستەوخۆ، هەر چەند ئاستێک کە پێویست بێت؛ هەموو زنجیرەیەک بە Object کۆتایی دێت.
  • ئەندامە privateـەکان و بە نادرێن؛ protected ئەندامێک بۆ دەکاتەوە.
  • super(...) یەکەم ؛ بەبێ ئەو javac super() زیاد دەکات.
  • : هەمان لە ، بە نیشانەی @Override؛ super.method() وەشانە کۆنەکە بەکار دەهێنێتەوە. جیاوازە: تر.
  • : دەتوانێت هەر هەڵبگرێت؛ javac جۆری دەپشکنێت و وەشانەکەی جێبەجێ دەبێت.
  • گۆڕین بۆ سەرەوە خۆکارە؛ گۆڕین بۆ خوارەوە (Type) و پشکنینی instanceofـی دەوێت، ئەگینا ClassCastException دێت.
  • toString() دووپاتە پێناسە بکەرەوە، هەروەها equals(Object) لەگەڵ hashCode()؛ final ڕێگە لە extends و دووپاتە پێناسەکردنەوە دەگرێت.
Review questionsپرسیارەکانی پێداچوونەوە
What does a subclass not inherit?کلاسی منداڵ چی بە بۆماوە وەرناگرێت؟

Private members (it cannot name them) and constructors (it calls them with super).

ئەندامە privateـەکان (ناتوانێت ناویان بهێنێت) و (بە super بانگیان دەکات).

What is the difference between overriding and overloading?جیاوازیی پێناسەکردنەوەی میتۆد و فرەپێناسەکردنی میتۆد چییە؟

Overriding: the same signature in a subclass, chosen by the object at run time. Overloading: the same name with other parameters, chosen by javac from the arguments.

: هەمان لە ، لە کاتی جێبەجێکردندا هەڵیدەبژێرێت. : هەمان ناو بە تر، javac بەپێی هەڵیدەبژێرێت.

Vehicle v = new Bus(...); Why can you not call v.route?⁦Vehicle v = new Bus(...);⁩ بۆچی ناتوانیت ⁦v.route⁩ بانگ بکەیت؟

javac checks the variable type Vehicle, which has no route. Check with instanceof and cast to Bus first.

javac جۆری Vehicle دەپشکنێت کە routeـی نییە. سەرەتا بە instanceof بیپشکنە و بیگۆڕە بۆ Bus.

Why override hashCode with equals?بۆچی hashCode لەگەڵ equals دووپاتە پێناسە دەکرێتەوە؟

Equal objects must have equal hash codes, or HashMap and HashSet cannot find them.

یەکسان دەبێت یەکسانیان هەبێت، ئەگینا HashMap و HashSet نایاندۆزنەوە.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Abstract Classes, Interfaces, Inner Classes and Enumsکلاسە ڕووتەکان، نێوبەستەکان، کلاسە ناوەکییەکان و ئینەمەکان

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.

Appearanceڕووکار

Modeدۆخ
Styleشێواز
Coloursڕەنگەکان

Saved on this device only.تەنیا لەسەر ئەم ئامێرە پاشەکەوت دەکرێت.