DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Bertil Muth
Bertil Muth

Posted on

Final classes in Java

There have been some discussions about the final keyword applied to classes in Java. To put it simple: when a class is declared final, you can't create a sub class of it.

An example would be helpful now.
Say this is your class:

public final class PopBand {
    public void hereIAm() {
        System.out.println("Here I am");
    }
    public void willYouSendMeAnAngel() {
        System.out.println("Will you send me an angel?");
    }
}
Enter fullscreen mode Exit fullscreen mode

If you try to create a sub class, the compiler will give you an error:

// This will cause a compile error
public class RockBand extends PopBand {
}
Enter fullscreen mode Exit fullscreen mode

What you can do, though, is extend the class by using composition.
That is: you create another class that contains an object of the original class. And then you delegate to the original (for the original's methods). And you add new methods. Like this:

public class PopRockBand {
    /**
     * Create an object of the class PopBand and assign it to containedBand
     */
    PopBand containedBand = new PopBand();

    /**
     * Delegate to methods of PopBand 
     */

    public void hereIAm() {
        containedBand.hereIAm();
    }

    public void willYouSendMeAnAngel() {
        containedBand.willYouSendMeAnAngel();
    }

    /**
     * This method is specific to PopRockBand class
     */
    public void rockYouLikeAHurricane() {
        System.out.println("Rock you like a hurricane");
    }
}
Enter fullscreen mode Exit fullscreen mode

And here's the Main class that proves composition works:

public class Gig {
    public static void main(String[] args) {
        PopRockBand scorpions = new PopRockBand();

        // Play "Send me an angel"
        scorpions.hereIAm();
        scorpions.willYouSendMeAnAngel();

        // Play "Rock you like a hurricane"
        scorpions.hereIAm();
        scorpions.rockYouLikeAHurricane();
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (8)

Collapse
 
amarlearning profile image
Amar Prakash Pandey

@bertilmuth Thanks for explaining Final and Composition with examples, this really helps a lot.

Collapse
 
firuzzz profile image
Jose Luis Romero

If the class is final, there's a reason for, composition pattern is not a workaround to weaken/break classes design.

Collapse
 
evanoman profile image
Evan Oman

With composition you are using the object exactly as intended, not sure how it could weaken or break the class design.

Collapse
 
firuzzz profile image
Jose Luis Romero

He stated "..is extend the class by using composition"
That's not extending anything.
The reason of this post is so basic (shouldn't even exist), to include a misconception like that

Thread Thread
 
rhymes profile image
rhymes

I think what he means is that you can extend its functionality by composition.

I don't think the post shouldn't exist, at least one person appreciated it...

In general basic content is useful for beginners, we're not all at the same stage in our careers or paths.

Collapse
 
tedhagos profile image
Ted Hagos

love the scorpion's reference

Collapse
 
evanoman profile image
Evan Oman

Plus you get to implement Effective Java Item 18: "Favor Composition Over Inheritance"

Collapse
 
jamesrgrinter profile image
James R Grinter

the Substitution principle is also very useful in practice, so It’s a nuisance if the original authors make a class final but don’t provide an interface for an alternate implementation to conform to.

Advice For Junior Developers

Advice from a career of 15+ years for new and beginner developers just getting started on their journey.