OCA Java imtahan mövzuları

Creating final methods

Əgər biz parent classdakı hər hansı bir metodun child classda override olunmasını istəmiriksə, həmin metodu final edirik. Static metodlar üçün də bu qayda keçərlidir, final olan static metodu parent classda gizlədə bilmərik.

class Bear {

    protected final void wakeUp() {
        System.out.println("Bear wake up");
    }

    public static final void eat() {
        System.out.println("Bear is eating");
    }

    public void sleep() {
        System.out.println("Bear is slepping");
    }
}

public class Panda extends Bear {

    public void wakeUp() {     // DOES NOT COMPILE
        System.out.println("Panda wake up");
    }

    public static final void eat() {   // DOES NOT COMPILE
        System.out.println("Panda is eating");
    }

    public final void sleep() {
        System.out.println("Panda is slepping");
    }
}

 

[topics lang=az]

About the author

Mushfiq Mammadov

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.